//
//  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
    }()
    
    
    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
    }()
    
    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()
        
        kDelayMainShort {
            self.doneBtn.addCornerRadiusShadow(cornerRadius: 24, shadowColor: UIColor.themeColor.cgColor, shadowOffset: .zero, shadowRadius: 10, shadowOpacity: 1.0)
        }
    }
        
        
       
    
    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(doneBtn)
        doneBtn.snp.makeConstraints { make in
            make.bottom.equalTo(-k_Height_safeAreaInsetsBottom()-10)
            make.centerX.equalToSuperview()
            make.width.equalTo(doneBtn.width)
            make.height.equalTo(doneBtn.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
    }
}
