//
//  GradientView.swift

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

import Foundation
import UIKit

enum GradientType {
    case topToBottom
    case leftToRight
    case upleftToLowright
    case uprightToLowleft
}

/// 支持2～4个颜色，内置默认的分割
class GradientView: UIView {
    private var gradientLayer: CAGradientLayer?
    var colors: [UIColor] = []
    var gradientType: GradientType = .leftToRight
    var specialStyleForVip: Bool = false
    var imBgOverlayMode : Bool = false
    
    // MARK: - Initialization
    init(colors: [UIColor], gradientType: GradientType) {
        super.init(frame: .zero)
        self.backgroundColor = .clear
        self.colors = colors
        self.gradientType = gradientType
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - Class Method
    static func vipGradientView(with size: CGSize) -> GradientView {
        let view = GradientView(colors: [
            UIColor(hex: 0x8CB5F9, alpha: 1.0),
            UIColor(hex: 0x7B47FF, alpha: 1.0),
            UIColor(hex: 0xD668F2, alpha: 1.0)
        ], gradientType: .leftToRight)
        
        view.frame = CGRect(origin: .zero, size: size)
        view.specialStyleForVip = true
        return view
    }
    
    // MARK: - Public Methods
    func modifyColors(_ colors: [UIColor], gradientType: GradientType) {
        self.colors = colors
        self.gradientType = gradientType
        setNeedsLayout()
    }
    
    // MARK: - Private Methods
    func layoutVipSubViews() {
        // Implement VIP-specific layout if needed
    }
    
    // MARK: - Layout
    override func layoutSubviews() {
        super.layoutSubviews()
        
        // Remove existing gradient layer
        gradientLayer?.removeFromSuperlayer()
        gradientLayer = nil
        
        // Check bounds and colors
        guard bounds.size.width > 0, colors.count >= 2 else {
            // assertionFailure("GradientView requires at least 2 colors and non-zero width")
            return
        }
        
        // Configure gradient points
        var start: CGPoint = .zero
        var end: CGPoint = .zero
        
        switch gradientType {
        case .topToBottom:
            start = CGPoint(x: 0.0, y: 0.0)
            end = CGPoint(x: 0.0, y: 1.0)
        case .leftToRight:
            start = CGPoint(x: 0.0, y: 0.0)
            end = CGPoint(x: 1.0, y: 0.0)
        case .upleftToLowright:
            start = CGPoint(x: 0.0, y: 0.0)
            end = CGPoint(x: 1.0, y: 1.0)
        case .uprightToLowleft:
            start = CGPoint(x: 1.0, y: 0.0)
            end = CGPoint(x: 0.0, y: 1.0)
        }
        
        // Create and configure gradient layer
        let gradientLayer = CAGradientLayer()
        gradientLayer.frame = bounds
        gradientLayer.colors = colors.map { $0.cgColor }
        gradientLayer.startPoint = start
        gradientLayer.endPoint = end
        
        // Set gradient locations
        var locations:[NSNumber] = [0, 1.0]
        if(specialStyleForVip){
            locations = [0, 0.6, 1] as [NSNumber]
        }else if colors.count == 3{
            locations = [0, 0.5, 1] as [NSNumber]
        }else if imBgOverlayMode{
            locations = [0, 0.2,0.5, 1] as [NSNumber]
        }else if colors.count == 4{
            locations = [0, 0.33, 0.66, 1] as [NSNumber]
        }
        gradientLayer.locations = locations
        // specialStyleForVip ? [0, 0.6, 1] : (colors.count >= 3 ? [0, 0.5, 1] : [0, 1])
        
        layer.insertSublayer(gradientLayer, at: 0)
        self.gradientLayer = gradientLayer
    }
}

// MARK: - UIColor Extension
extension UIColor {
    convenience init(hex: UInt32, alpha: CGFloat) {
        let red = CGFloat((hex >> 16) & 0xFF) / 255.0
        let green = CGFloat((hex >> 8) & 0xFF) / 255.0
        let blue = CGFloat(hex & 0xFF) / 255.0
        self.init(red: red, green: green, blue: blue, alpha: alpha)
    }
}
