//
//  TSTBBtnView.swift
//  AIRingtone
//
//  Created by 100Years on 2025/3/2.
//

class TSTBBtnView: TSBaseView {
    
    weak var targetVC:UIViewController?
    
    var closePageSelector:Selector
    var copyrightSelector:Selector
    
    var lockSelector:Selector
    var callSelector:Selector
    var soundSelector:Selector
    var doneSelector:Selector

    lazy var normalNavBarView: TSNormalNavigationBarView = {
        let view = TSNormalNavigationBarView()
        return view
    }()
    
    
    var isSoundloadling:Bool = false {
        didSet {
            if isSoundloadling {
                soundBtn.setImage(UIImage(named: "sound_loading_icon"), for: .normal)
                soundBtn.startRotating()
            }else {
                soundBtn.stopRotating()
                let volume = soundVolume
                soundVolume = volume
            }
        }
    }
    
    var soundVolume:Float = 1.0{
        didSet{
            DispatchQueue.main.async {
                if self.isSoundloadling == false {
                    self.soundBtn.setImage(UIImage(named: self.soundVolume == 0 ? "sound_off_icon" : "sound_on_icon"), for: .normal)
                }
            }
        }
    }
    
    
    lazy var lockBtn: UIButton = {
        let btn = UIButton.createButton(image: UIImage(named: "lock_screen_icon"))
        btn.addTarget(targetVC, action: lockSelector, for: .touchUpInside)
        return btn
    }()
    
    lazy var callBtn: UIButton = {
        let btn = UIButton.createButton(image: UIImage(named: "phone_screen_icon"))
        btn.addTarget(targetVC, action: callSelector, for: .touchUpInside)
        return btn
    }()
    
    lazy var soundBtn: UIButton = {
        let btn = UIButton.createButton(image: UIImage(named: "sound_on_icon"))
        btn.addTarget(targetVC, action: soundSelector, for: .touchUpInside)
        return btn
    }()
    
//    lazy var doneBtn: UIButton = {
//        let btn = kCreateNormalSubmitBtn(title: "Set Now".localized)
//        btn.addTarget(targetVC, action: doneSelector, for: .touchUpInside)
//        return btn
//    }()
    
    lazy var doneBtnView: TSAppBtnView = {
        let doneBtnView = TSAppBtnView()
        doneBtnView.setUpButton(style: .themeSet, vipFreeNumType: .none) { [weak self]  in
            guard let self = self else { return }
            if let targetVC = targetVC {
                targetVC.perform(doneSelector)
            }
        }
        return doneBtnView
    }()
    
    init(targetVC:UIViewController,
         closePageSelector:Selector,
         copyrightSelector:Selector,
         lockSelector:Selector,
         callSelector:Selector,
         soundSelector:Selector,
         doneSelector:Selector)
    {
        self.targetVC = targetVC
        self.closePageSelector = closePageSelector
        self.copyrightSelector = copyrightSelector
        self.lockSelector = lockSelector
        self.callSelector = callSelector
        self.soundSelector = soundSelector
        self.doneSelector = doneSelector
        super.init(frame: .zero)
    }
    
    @MainActor required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func creatUI() {
        setUpNavi()
        setIcons()
        setDone()
    }
        
        
       
    
    func setUpNavi(){
    
        contentView.addSubview(normalNavBarView)
        normalNavBarView.snp.makeConstraints { make in
            make.leading.top.trailing.equalToSuperview()
            make.height.equalTo(k_Nav_Height)
        }
        
        _ = normalNavBarView.setLeftNavigationItem(name: "", imageName: "navi_back_white", target: targetVC, action: closePageSelector)
        _ = normalNavBarView.setRightNavigationItem(name: "", imageName: "copyright_icon", target: targetVC, action: copyrightSelector)
    }
    
    
 
    func setIcons() {
        let bottomH = k_Height_safeAreaInsetsBottom()

        contentView.addSubview(soundBtn)
        soundBtn.snp.makeConstraints { make in
            make.bottom.equalTo(-bottomH-112)
            make.trailing.equalTo(-24)
            make.width.height.equalTo(48.0)
        }
        contentView.addSubview(callBtn)
        callBtn.snp.makeConstraints { make in
            make.bottom.equalTo(soundBtn.snp.top).offset(-28)
            make.trailing.equalTo(-24)
            make.width.height.equalTo(48.0)
        }
        contentView.addSubview(lockBtn)
        lockBtn.snp.makeConstraints { make in
            make.bottom.equalTo(callBtn.snp.top).offset(-28)
            make.trailing.equalTo(-24)
            make.width.height.equalTo(48.0)
        }
    }
    
    
    func setDone() {
        contentView.addSubview(doneBtnView)
        doneBtnView.snp.makeConstraints { make in
            make.bottom.equalTo(-k_Height_safeAreaInsetsBottom()-10)
            make.centerX.equalToSuperview()
            make.width.equalTo(doneBtnView.button.width)
            make.height.equalTo(doneBtnView.button.height)
        }
    }
    
    // 重写 hitTest 方法
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        // 首先检查触摸点是否在子视图上
        for subview in contentView.subviews.reversed() {
            let convertedPoint = subview.convert(point, from: self)
            if let hitView = subview.hitTest(convertedPoint, with: event) {
                return hitView
            }
        }
        // 如果触摸点不在任何子视图上，返回 nil 让事件穿透
        return nil
    }
}
