//
//  TSFlashSliderView.swift
//  Girly
//
//  Created by 100Years on 2025/1/9.
//
import MultiSlider
class TSFlashSliderView: TSBaseView {
    
    var changedValueComplete:((CGFloat,Bool)->Void)?
    var currentValue:CGFloat = 1.0
    
    lazy var bgView: UIView = {
        let bgView = UIView()
        bgView.cornerRadius = 24.0
        return bgView
    }()
    
    lazy var leftImageView: UIImageView = {
        let leftImageView = UIImageView.createImageView(imageName: "soundOff")
        return leftImageView
    }()
    
    lazy var rightImageView: UIImageView = {
        let rightImageView = UIImageView.createImageView(imageName: "volume")
        return rightImageView
    }()
    
    lazy var slider: MultiSlider = {
        let slider = MultiSlider()
        slider.value = [0.5]
        slider.minimumValue = 0.0
        slider.orientation = .horizontal
        slider.tintColor = .clear
        slider.thumbTintColor = .clear
        slider.showsThumbImageShadow = false
        slider.trackWidth = 44.0
        slider.thumbImage = UIImage(named: "slider_thumb")
        slider.addTarget(self, action: #selector(sliderChanged(slider:)), for: .valueChanged)
        slider.addTarget(self, action: #selector(sliderCancel(slider:)), for: .touchUpInside)
        return slider
    }()
    
    
    override func creatUI() {
        
        contentView.addSubview(bgView)
        bgView.snp.makeConstraints { make in
            make.leading.equalTo(0)
            make.trailing.equalTo(0)
            make.top.equalTo(0)
            make.bottom.equalTo(0)
        }
        
     
        bgView.addSubview(leftImageView)
        bgView.addSubview(rightImageView)
        bgView.addSubview(slider)

        
        leftImageView.snp.makeConstraints { make in
            make.leading.equalTo(12)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(24)
        }
        
        rightImageView.snp.makeConstraints { make in
            make.trailing.equalTo(-12)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(24)
        }
        
        slider.snp.makeConstraints { make in
//            make.leading.equalTo(40)
//            make.trailing.equalTo(-40)
            
            make.leading.equalTo(4)
            make.trailing.equalTo(-4)
            
            make.centerY.equalToSuperview()
            make.height.equalTo(44)
        }
        
        kDelayMainShort {
            self.bgView.addGradientBg(colors: [UIColor.fromHex("#FFFFFF", alpha: 0.2).cgColor,UIColor.fromHex("#FFFFFF", alpha: 0.8).cgColor],startPoint: CGPoint(x: 0, y: 1),endPoint: CGPoint(x: 1, y: 1))
        }
        
    }
    
    
    func setSliderValue(minimumValue:CGFloat,maximumValue:CGFloat,value:CGFloat) {
        slider.minimumValue = minimumValue
        slider.maximumValue = maximumValue
        slider.value = [value]
    }
    
    func setSliderImage(leftImage:UIImage,rightImage:UIImage) {
        leftImageView.image = leftImage
        rightImageView.image = rightImage
    }
    
    @objc func sliderChanged(slider: MultiSlider) {
        debugPrint("now thumbs are at \(slider.value)")
        if let num = slider.value.first {
            currentValue = num
            changedValueComplete?(currentValue,false)
        }
    }
    
    @objc func sliderCancel(slider: MultiSlider) {
        debugPrint("sliderCancel \(slider.value)")
        if let num = slider.value.first {
            currentValue = num
            changedValueComplete?(currentValue,true)
        }
    }
    
    
}
