//
//  QSLLoadingView.swift
//  QuickSearchLocation
//
//  Created by Destiny on 2025/10/30.
//

import UIKit

class QSLLoadingView: UIView {

    private let statusLabel = UILabel()
    private let progressLabel = UILabel()
    private let progressLayer = CAShapeLayer()
    private let gradientLayer = CAGradientLayer()

    var progress: CGFloat = 0 {
        didSet {
            updateProgress()
        }
    }

    var completion: (() -> Void)?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupView()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupView() {
        self.gradientBackgroundColor(color1: .hexStringColor(hexString: "#D3FFF6"), color2: .white, width: 170.rpx, height: 170.rpx, direction: .horizontal)
        self.layer.cornerRadius = 16.rpx

        statusLabel.textAlignment = .center
        statusLabel.textColor = .red
        statusLabel.font = UIFont.boldSystemFont(ofSize: 12)
        statusLabel.translatesAutoresizingMaskIntoConstraints = false
        self.addSubview(statusLabel)

        progressLabel.textAlignment = .center
        progressLabel.textColor = .black
        progressLabel.font = UIFont.boldSystemFont(ofSize: 20)
        progressLabel.translatesAutoresizingMaskIntoConstraints = false
        self.addSubview(progressLabel)

        let circularPath = UIBezierPath(arcCenter: CGPoint(x: 85.rpx, y: 85.rpx), radius: 44.rpx, startAngle: -CGFloat.pi / 2, endAngle: 3 * CGFloat.pi / 2, clockwise: true)
        progressLayer.path = circularPath.cgPath
        progressLayer.strokeColor = UIColor.gray.cgColor
        progressLayer.fillColor = UIColor.clear.cgColor
        progressLayer.lineWidth = 8
        progressLayer.lineCap = .round
        self.layer.addSublayer(progressLayer)

        gradientLayer.colors = [UIColor.hexStringColor(hexString: "#19D9B1").cgColor, UIColor.hexStringColor(hexString: "#19BFD9").cgColor]
        gradientLayer.startPoint = CGPoint(x: 0, y: 0.5)
        gradientLayer.endPoint = CGPoint(x: 1, y: 0.5)
        gradientLayer.frame = self.bounds
        gradientLayer.mask = progressLayer
        self.layer.addSublayer(gradientLayer)

        NSLayoutConstraint.activate([
            statusLabel.topAnchor.constraint(equalTo: self.topAnchor, constant: 20),
            statusLabel.centerXAnchor.constraint(equalTo: self.centerXAnchor),

            progressLabel.centerXAnchor.constraint(equalTo: self.centerXAnchor),
            progressLabel.centerYAnchor.constraint(equalTo: self.centerYAnchor)
            
            
        ])
    }

    private func updateProgress() {
        progressLayer.strokeEnd = progress / 100
        progressLabel.text = "\(Int(progress))%"
        print("progress:::\(progress)")
        if progress < 40 {
            statusLabel.text = "匹配中"
            statusLabel.textColor = .hexStringColor(hexString: "#F94848")
        } else if progress < 99 {
            statusLabel.text = "处理中"
            statusLabel.textColor = .hexStringColor(hexString: "#4886F9")
        } else if progress >= 100 {
            statusLabel.text = "处理完成"
            statusLabel.textColor = .hexStringColor(hexString: "#1DBA5E")
        }
    }
}
