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

class TSNightLightVC: TSBaseVC {
    
    private var isOpen = false
    private var currentValue:CGFloat = 0.5
    
    
    lazy var myTimer: TSCustomTimer = {
        let customTimer = TSCustomTimer(interval: 1.0,repeats: true) { [weak self]  in
            guard let self = self else { return }
            debugPrint("countdown=\(setToolView.countdown)")
            if setToolView.countdown <= 0{
                stopMyTimer()//关闭定时器
                isOpen = false
                showToView()//退出全屏
            }else {
                setToolView.countdown -= 1
                setToolView.timeLabel.text = setToolView.countdown.toHourMinuteSecondString()
            }
        }
        return customTimer
    }()
    
    lazy var shapeView: TSLightShapeView = {
        let shapeView = TSLightShapeView()
        shapeView.imageNamed = "bold_roundness_shape"
        shapeView.color = UIColor.white.adjusted(brightness: currentValue)
        return shapeView
    }()
    

    lazy var setToolView: TSNightSetToolView = {
        let setToolView = TSNightSetToolView()
        setToolView.changedValueComplete = { [weak self] value,isEnd in
            guard let self = self else { return }
            currentValue = value
            shapeView.color = UIColor.white.adjusted(brightness: 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
                }
                UIDevice.keepScreenAwake()
                
                if(setToolView.countdown > 0){
                    startMyTimer()
                }
                setToolView.submitBtn.setTitle("Close".localized, for: .normal)
            }else{
                UIDevice.allowScreenSleep()
                stopMyTimer()
                setToolView.submitBtn.setTitle("Open".localized, for: .normal)
            }
        }
        
        return setToolView
    }()
    

    override func createView() {
        
        view.insertSubview(shapeView, at: 0)
        shapeView.snp.makeConstraints { make in
            make.top.equalTo(10)
            make.centerX.equalToSuperview()
            make.width.equalTo(k_ScreenWidth)
            make.height.equalTo(k_ScreenWidth*1.5)
        }
        contentView.layoutIfNeeded()
        self.shapeView.addContent()
        
        addNormalNavBarView()
        setPageTitle("")
        
        self.view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(showToView)))

        contentView.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
            }
        }
        self.setToolView.submitBtn.setTitle(self.isOpen ? "Close" : "Open", for: .normal)
        UIDevice.allowScreenSleep()
    }
    
    func startMyTimer(){
        myTimer.start()
    }
    
    func stopMyTimer(){
        myTimer.stop()
    }
    
    deinit {
        UIDevice.allowScreenSleep()
    }
}


class TSNightSetToolView: 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
    
    
    var countdown:Int = 0
    
    lazy var titleLabel1: UILabel = {
        let titleLabel1 = UILabel.createLabel(text: "Brightness".localized,font: .font(size: 14,weight: .medium),textColor: .white)
        return titleLabel1
    }()

    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
    }()
    
    lazy var titleLabel2: UILabel = {
        let titleLabel2 = UILabel.createLabel(text: "Night Light Countdown".localized,font: .font(size: 14,weight: .medium),textColor: UIColor.white)
        return titleLabel2
    }()
    
    
    lazy var timeLabel: UILabel = {
        let timeLabel = UILabel.createLabel(text: "00:00:00".localized,font: .font(size: 14,weight: .medium),textColor: UIColor.fromHex("#FFFFFF", alpha: 0.4))
        return timeLabel
    }()
    
    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 timerView: TSTimerView = {
        let timerView = TSTimerView()
        timerView.isHidden = true
        timerView.onTimeSelected = { [weak self] hour,minute in
            guard let self = self else { return }
            countdown = (hour*60+minute)*60
            timeLabel.text = countdown.toHourMinuteSecondString()
        }
        return timerView
    }()
    
    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)
        }
        
        
        let cell2 = creatCell2()
        bottomView.addSubview(cell2)
        cell2.snp.makeConstraints { make in
            make.leading.equalTo(0)
            make.trailing.equalTo(0)
            make.top.equalTo(124)
            make.height.equalTo(44)
        }
        

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

        
        //时间选择器
        self.addSubview(timerView)
        timerView.snp.makeConstraints { make in
            make.edges.equalTo(0)
        }
        

        
        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
    }
    
    func creatCell2() -> UIView{
        let bgView = UIView()

        bgView.addSubview(titleLabel2)
        titleLabel2.snp.makeConstraints { make in
            make.leading.equalTo(16)
            make.width.equalTo(200)
            make.centerY.equalToSuperview()
        }
        
        
        let timeView = UIView()
        timeView.backgroundColor = .fromHex("#FFFFFF", alpha: 0.1)
        timeView.cornerRadius = 14.0
        timeView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(clickTime)))
        timeView.addSubview(timeLabel)
        timeLabel.snp.makeConstraints { make in
            make.leading.equalTo(8)
            make.trailing.equalTo(-8)
            make.height.equalTo(28)
            make.top.bottom.equalTo(0)
        }
        
        bgView.addSubview(timeView)
        timeView.snp.makeConstraints { make in
            make.trailing.equalTo(-16)
            make.centerY.equalToSuperview()
        }
        
        return bgView
    }
    
    @objc func clickTime(){
        self.timerView.isHidden = false
    }
    

}



