//
//  TSFlashLightVC.swift
//  Girly
//
//  Created by 100Years on 2025/1/9.
//

class TSFlashLightVC: TSBaseVC {
    
    private var isOpen = false
    private var currentValue:CGFloat = 0.5
    
    lazy var lightImageView: UIImageView = {
        let lightImageView = UIImageView.createImageView(imageName: "flashlight_light")
        lightImageView.isHidden = true
        return lightImageView
    }()
    
    lazy var flashlightImageView: UIImageView = {
        let flashlightImageView = UIImageView.createImageView(imageName: "flashlight_off")
        return flashlightImageView
    }()
    
    
    lazy var flashlight: TSFlashlightTool = {
        let flashlight = TSFlashlightTool()
        flashlight.adjustBrightness(brightness: 0.5)
        return flashlight
    }()
    

    lazy var setToolView: TSFlashNightSetToolView = {
        let setToolView = TSFlashNightSetToolView()
        setToolView.changedValueComplete = { [weak self] value,isEnd in
            guard let self = self else { return }
            currentValue = value
            if isOpen{
                flashlight.adjustBrightness(brightness: Float(currentValue))
            }
        }
        
        setToolView.clickDoneComplete = { [weak self]  in
            guard let self = self else { return }
            isOpen = !isOpen
            
            if isOpen {
                UIView.animate(withDuration: 0.5, delay: 0, options: []) {
                    setToolView.isHidden = true
                }
            }
            changeLight()
        }
        
        return setToolView
    }()
    

    override func createView() {
        addNormalNavBarView()
        
        setPageTitle("")
        self.view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(showToView)))

        view.addSubview(lightImageView)
        lightImageView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.centerX.equalToSuperview()
            make.width.equalTo(375.0)
            make.height.equalTo(130.0)
        }
        
        view.addSubview(flashlightImageView)
        flashlightImageView.snp.makeConstraints { make in
            make.top.equalTo(78)
            make.centerX.equalToSuperview()
            make.width.equalTo(234.0)
            make.height.equalTo(445.0)
        }
        
        view.addSubview(setToolView)
        setToolView.snp.makeConstraints { make in
            make.leading.trailing.equalTo(0)
            make.bottom.equalTo(0)
            make.height.equalTo(282.0)
        }

    }
    
    @objc func showToView() {
        if setToolView.isHidden == true {
            UIView.animate(withDuration: 0.5, delay: 0, options: []) {
                self.setToolView.isHidden = false
                
            }
        }
    }

    
    func changeLight() {
        if isOpen {
            self.lightImageView.isHidden = false
            self.flashlightImageView.image = UIImage(named: "flashlight_on")
            self.flashlight.turnOn(brightness: Float(currentValue))
        }else{
            self.lightImageView.isHidden = true
            self.flashlightImageView.image = UIImage(named: "flashlight_off")
            self.flashlight.turnOff()
        }
        self.setToolView.submitBtn.setTitle(self.isOpen ? "Close" : "Open", for: .normal)
    }
    
    deinit {
        self.flashlight.turnOff()
    }
}


class TSFlashNightSetToolView: TSBaseView {

    var clickDoneComplete:(()->Void)?
    var changedValueComplete:((CGFloat,Bool)->Void)?{
        didSet{
            flashSliderView.changedValueComplete = changedValueComplete
        }
    }

    lazy var bottomView: UIView = {
        let bottomView = UIView()
        bottomView.backgroundColor = .cardBgColor
        return bottomView
    }()
    
    var isShow = true
    
    lazy var titleLabel1: UILabel = {
        let titleLabel1 = UILabel.createLabel(text: "Flicker Frequency".localized,font: .font(size: 14,weight: .medium),textColor: .white)
        return titleLabel1
    }()

    lazy var submitBtn: UIButton = {
        let submitBtn = createNormalSubmitBtn(title: "Open".localized) { [weak self]  in
            guard let self = self else { return }
            isShow = !isShow
            clickDoneComplete?()
        }
        submitBtn.cornerRadius = 22.0
        return submitBtn
    }()
    
    lazy var flashSliderView: TSFlashSliderView = {
        let flashSliderView = TSFlashSliderView()
        flashSliderView.setSliderValue(minimumValue: 0.0, maximumValue: 1.0, value: 0.5)
        flashSliderView.setSliderImage(leftImage: UIImage(named: "reduceBrightness")!, rightImage: UIImage(named: "brightness")!)
        return flashSliderView
    }()
    
    override func creatUI() {
        
        contentView.addSubview(bottomView)
        bottomView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        
        let cell1 = creatCell(subView: titleLabel1)
        bottomView.addSubview(cell1)
        cell1.snp.makeConstraints { make in
            make.leading.equalTo(0)
            make.trailing.equalTo(0)
            make.top.equalTo(20)
            make.height.equalTo(44)
        }
        
        bottomView.addSubview(flashSliderView)
        flashSliderView.snp.makeConstraints { make in
            make.top.equalTo(cell1.snp.bottom)
            make.leading.equalTo(16)
            make.trailing.equalTo(-16)
            make.height.equalTo(48)
        }

        bottomView.addSubview(submitBtn)
        submitBtn.snp.makeConstraints { make in
            make.leading.equalTo(50)
            make.trailing.equalTo(-51)
            make.bottom.equalTo(-34)
            make.height.equalTo(44)
        }

        kDelayMainShort {
            self.bottomView.cornersRound(radius: 20, corner: [.topLeft,.topRight])
        }
        
    }
    
    func creatCell(subView:UIView) -> UIView{
        let view = UIView()
        view.addSubview(subView)
        subView.snp.makeConstraints { make in
            make.leading.equalTo(16)
            make.trailing.equalTo(0)
            make.centerY.equalToSuperview()
        }
        return view
    }
}
