//
//  TSLightGradientVC.swift
//  Girly
//
//  Created by 100Years on 2025/1/7.
//
import JXSegmentedView

class TSLightGradientVC: TSBaseVC{
    
    lazy var currentColorString:String =  {
        if let itemModel = kLightShared.gradientColors.first {
            return itemModel.color
        }else{
            return "#D012F1-#4541FF"
        }
    }(){
        didSet{
            updateColorView()
        }
    }
    
    lazy var brightnessNum:CGFloat =  {
        return 1.0
    }(){
        didSet{
            updateColorView()
        }
    }
    
    lazy var saturationNum:CGFloat =  {
        return 1.0
    }(){
        didSet{
            updateColorView()
        }
    }
    
    lazy var colorPaletteView: TSLightColorPaletteView = {
        let colorPaletteView = TSLightColorPaletteView()
        colorPaletteView.changedBrightnessComplete = { [weak self] floatNum in
            guard let self = self else { return }
            brightnessNum = floatNum
        }
        
        colorPaletteView.changedSaturationComplete = { [weak self] floatNum in
            guard let self = self else { return }
            saturationNum = floatNum
        }
        
        return colorPaletteView
    }()
    
    lazy var topView: UIView = {
        let view = UIView()
        view.cornerRadius = 22.0
        view.backgroundColor = .white
        
        view.addSubview(colorPaletteView)
        colorPaletteView.snp.makeConstraints { make in
            make.leading.equalTo(12)
            make.trailing.equalTo(-12)
            make.height.equalTo(100.0)
            make.bottom.equalTo(-12)
        }
        
        let fullScreenBtn = UIButton.createButton(image: UIImage(named: "fullScreen"),backgroundColor: UIColor.fromHex("000000", alpha: 0.2),corner: 8.0) { [weak self]  in
            guard let self = self else { return }
            kPresentModalVC(target: self, modelVC: TSFullScreenVC.creatGradientVC(cgcolors:getCurrentColorGradient()),transitionStyle: .crossDissolve)
        }
        view.addSubview(fullScreenBtn)
        fullScreenBtn.snp.makeConstraints { make in
            make.top.equalTo(16)
            make.trailing.equalTo(-12)
            make.width.height.equalTo(36)
        }
        
        return view
    }()
    
    lazy var toolView: TSLightGradientToolView = {
        let toolView = TSLightGradientToolView()
        toolView.changedColorComplete = {[weak self] colorModel in
            guard let self = self else { return }
            currentColorString = colorModel.color
        }
        return toolView
    }()

    override func createView() {
        setNavBarViewHidden(true)
        setViewBgImageNamed(named: "lightBg")
        contentView.addSubview(topView)
        contentView.addSubview(toolView)
        topView.snp.makeConstraints { make in
            make.leading.equalTo(16)
            make.trailing.equalTo(-16)
            make.top.equalTo(kLightTop)
            make.bottom.equalTo(-172.0)
        }
        
        toolView.snp.makeConstraints { make in
            make.leading.equalTo(0)
            make.trailing.equalTo(0)
            make.height.equalTo(128.0)
            make.bottom.equalTo(0)
        }
        
        kDelayMainShort {
            self.topView.addGradientBg(colors: self.currentColorString.cgColors(separator: "-"))
        }
        
        updateColorView()
    }
    
    
    func updateColorView(){
        topView.addGradientBg(colors: getCurrentColorGradient())
    }
    
    func getCurrentColorGradient() -> [CGColor] {
        let array = currentColorString.components(separatedBy: "-")
        var colors = [CGColor]()
        for string in array {
            var color = string.color
            color = color.adjusted(brightness: brightnessNum,saturation: saturationNum)
            colors.append(color.cgColor)
        }
        return colors
    }
}

extension TSLightGradientVC: JXSegmentedListContainerViewListDelegate {
    func listView() -> UIView {
        return view
    }
}
