//
//  GradientBorderView.swift

//
//  Created by Leon on 2025/7/22.
//

import UIKit

/// 渐变边框容器
class GradientBorderView: UIView {
    private var gradientLayer: CAGradientLayer?
    private var colors: [UIColor] = []
    private var gradientType: GradientType = .leftToRight // Assuming GradientType is an enum; default to horizontal
    private var innerBackgroundColor: UIColor = .clear
    
    // MARK: - Initialization
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    required init?(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        fatalError("init(nibName:bundle:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    convenience init(colors: [UIColor], gradientType: GradientType) {
        self.init(frame: .zero)
        self.backgroundColor = .clear
        self.colors = colors
        self.gradientType = gradientType
        self.gBorderWidth = 1.0 // Assuming DP(1) maps to 1.0 point
        self.innerBackgroundColor = .clear
    }
    
    // MARK: - Lifecycle
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        gradientLayer?.removeFromSuperlayer()
        
        if !colors.isEmpty {
            let cgColors = colors.map { $0.cgColor }
            addGradientBorder(with: cgColors, width: gBorderWidth)
        }
    }
    
    // MARK: - Public Properties
    
    var gBorderWidth: CGFloat = 0{
        didSet {
            setNeedsLayout()
        }
    }
    
    // MARK: - Private Methods
    
    private func addGradientBorder(with colors: [CGColor], width: CGFloat) {
        // 创建一个渐变层
        let gradientLayer = CAGradientLayer()
        
        gradientLayer.frame = bounds
        gradientLayer.colors = colors
        gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.5)
        gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.5)
        let step = 1.0 / Double(colors.count)
        var locations: [NSNumber] = []
        var locationValue: Double = 0
        
        for _ in 0..<colors.count {
            locations.append(NSNumber(value: locationValue))
            locationValue += step
        }
        
        gradientLayer.locations = locations
        
        // 创建一个形状层
        let shapeLayer = CAShapeLayer()
        let insetRect = bounds.insetBy(dx: width / 2, dy: width / 2)
        if layer.cornerRadius > 0 {
            shapeLayer.path = UIBezierPath(roundedRect: insetRect, cornerRadius: layer.cornerRadius).cgPath
        } else {
            shapeLayer.path = UIBezierPath(rect: insetRect).cgPath
        }
        shapeLayer.lineWidth = width
        shapeLayer.fillColor = UIColor.clear.cgColor
        shapeLayer.strokeColor = UIColor.black.cgColor
        
        // 将形状层设置为渐变层的遮罩
        gradientLayer.mask = shapeLayer
        
        // 将渐变层添加到视图的图层上
        layer.addSublayer(gradientLayer)
        self.gradientLayer = gradientLayer
    }
}

// MARK: - Unavailable Initializers

//extension GradientBorderView {
//    @available(*, unavailable)
//    class func new() -> Self {
//        fatalError("new() is unavailable")
//    }
//    
//    @available(*, unavailable)
//    override convenience init() {
//        fatalError("init() is unavailable")
//    }
//    
//    @available(*, unavailable)
//    override convenience init(frame: CGRect) {
//        fatalError("init(frame:) is unavailable")
//    }
//    
//    @available(*, unavailable)
//    required convenience init?(coder: NSCoder) {
//        fatalError("init(coder:) is unavailable")
//    }
//    
//    @available(*, unavailable)
//    convenience init?(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
//        fatalError("init(nibName:bundle:) is unavailable")
//    }
//}

// MARK: - Assumptions and Unknowns


// Unknown: 'hex_clear' is assumed to be UIColor.clear based on context (clear background)
// Unknown: 'DP(1)' is assumed to map to 1.0 point as a design pixel unit; adjust if a different scaling is intended
// Unknown: The full definition of GradientType is not provided; a default horizontal gradient is assumed
