//
//  TSLightColorPaletteView.swift
//  Girly
//
//  Created by 100Years on 2025/1/7.
//
import MultiSlider


class TSCommonSliderView : TSBaseView{
    
    var changedValueComplete:((CGFloat)->Void)?
    
    var currentValue:CGFloat = 1.0 {
        didSet{
            leftLabel.text = "\(Int(currentValue*100))%"
            changedValueComplete?(currentValue)
        }
    }
    

    lazy var leftImageView: UIImageView = {
        let leftImageView = UIImageView.createImageView(imageName: "")
        return leftImageView
    }()
    
    lazy var leftLabel: UILabel = {
        let leftLabel = UILabel.createLabel(text: "100%",font: UIFont.font(size: 14),textColor: .white)
        return leftLabel
    }()
    
    lazy var slider: MultiSlider = {
        let slider = MultiSlider()
        slider.value = [1.0]
        slider.minimumValue = 0.5
        slider.orientation = .horizontal
        slider.addTarget(self, action: #selector(sliderChanged(slider:)), for: .valueChanged)
        slider.tintColor = .white // color of track
        slider.thumbTintColor = .white // color of thumbs
        slider.trackWidth = 4
        return slider
    }()
    
    
    override func creatUI() {
        
        addSubview(leftImageView)
        addSubview(leftLabel)
        addSubview(slider)
        
        leftImageView.snp.makeConstraints { make in
            make.leading.equalTo(0)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(24)
        }
        
        leftLabel.snp.makeConstraints { make in
            make.leading.equalTo(leftImageView.snp.trailing).offset(10)
            make.centerY.equalToSuperview()
        }
        
        slider.snp.makeConstraints { make in
            make.leading.equalTo(81)
            make.trailing.equalTo(0)
            make.centerY.equalToSuperview()
            make.height.equalTo(20)
        }
    }
    
    @objc func sliderChanged(slider: MultiSlider) {
        debugPrint("thumb \(slider.draggedThumbIndex) moved")
        debugPrint("now thumbs are at \(slider.value)") // e.g., [1.0, 4.5, 5.0]
        if let num = slider.value.first {
            currentValue = num
        }
    }
}


class TSLightColorPaletteView : TSBaseView{
    
    //改变亮度
    var changedBrightnessComplete:((CGFloat)->Void)?
    //改变色彩饱和度
    var changedSaturationComplete:((CGFloat)->Void)?
    
    lazy var brightnessSlider: TSCommonSliderView = {
        let slider = TSCommonSliderView()
        slider.leftImageView.image = UIImage(named: "brightness")
        slider.changedValueComplete = { [weak self] num in
            guard let self = self else { return }
            changedBrightnessComplete?(num)
        }
        return slider
    }()
    
    
    lazy var saturationSlider: TSCommonSliderView = {
        let slider = TSCommonSliderView()
        slider.slider.minimumValue = 0.1
        slider.leftImageView.image = UIImage(named: "saturation")
        slider.changedValueComplete = { [weak self] num in
            guard let self = self else { return }
            changedSaturationComplete?(num)
        }
        return slider
    }()
    
    override func creatUI() {
        cornerRadius = 18.0
        backgroundColor = UIColor.fromHex("#000000", alpha: 0.2)

        addSubview(brightnessSlider)
        brightnessSlider.snp.makeConstraints { make in
            make.leading.equalTo(16)
            make.top.equalTo(16)
            make.trailing.equalTo(-16)
            make.height.equalTo(24)
        }
        
        addSubview(saturationSlider)
        saturationSlider.snp.makeConstraints { make in
            make.leading.equalTo(16)
            make.top.equalTo(60)
            make.trailing.equalTo(-16)
            make.height.equalTo(24)
        }
    }
    
    
    @objc func sliderChanged(slider: MultiSlider) {
        debugPrint("thumb \(slider.draggedThumbIndex) moved")
        debugPrint("now thumbs are at \(slider.value)") // e.g., [1.0, 4.5, 5.0]
        if let num = slider.value.first {
            if slider == brightnessSlider{
                changedBrightnessComplete?(num)
            }else{
                changedSaturationComplete?(num)
            }
        }
    }
}


