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

import MediaPlayer
class TSWarningLightVC: TSBaseVC {
    
    lazy var warningImageView: UIImageView = {
        return UIImageView.createImageView(imageName: "light_alarm")
    }()
    
    private var isOpen = false
    
    let flashlightController = TSFlashlightController()
    private lazy var volumeView: MPVolumeView = {
        let view = MPVolumeView(frame: .zero)
        view.isHidden = true // 隐藏音量控件
        return view
    }()
    
    lazy var setToolView: TSFlashSetToolView = {
        let setToolView = TSFlashSetToolView()
        
        setToolView.changedValueComplete = { [weak self] value,isEnd in
            guard let self = self else { return }
            setVolume(Float(value))
        }
        
        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
                }
                audioPlayer?.play()
                warningImageView.startAlphaAnimation(minAlpha: 0.3,duration: 0.4)
                flashlightController.startFlashing(frequency: 3)
            }else {
                audioPlayer?.pause()
                warningImageView.stopAlphaAnimation()
                flashlightController.stopFlashing()
            }
        }
        
        return setToolView
    }()
    
    
    lazy var audioPlayer: TSAudioPlayer? = {
        let audioPlayer = TSAudioPlayer(url: Bundle.main.url(forResource: "AlarmSound", withExtension: "MP3")!)
        audioPlayer?.setVolume(1.0)
        audioPlayer?.setLoop(true)
        return audioPlayer
    }()
    
    override func createView() {
        addNormalNavBarView()
        setPageTitle("")
        view.backgroundColor = "#161616".color
        
        
        contentView.addSubview(volumeView)
        contentView.addSubview(warningImageView)
        warningImageView.snp.makeConstraints { make in
            make.width.height.equalTo(250.0)
            make.centerX.equalToSuperview()
            make.top.equalTo(156.0)
        }
        
        contentView.addSubview(setToolView)
        setToolView.snp.makeConstraints { make in
            make.leading.trailing.equalTo(0)
            make.bottom.equalTo(0)
            make.height.equalTo(282.0)
        }

        self.view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(showToView)))
        
        kDelayMainShort {
            self.setVolume(0.5)
        }
        
    }
    
    @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)
            }
        }
    }
    
    /// 设置音量
    /// - Parameter volume: 音量值 (0.0 ~ 1.0)
    func setVolume(_ volume: Float) {
        guard let slider = volumeView.subviews.first(where: { $0 is UISlider }) as? UISlider else { return }
        slider.value = volume // 调整音量
    }
    
    deinit {
        audioPlayer?.stop()
        warningImageView.stopAlphaAnimation()
        flashlightController.stopFlashing()
    }
}
