//
//  NavigationView.swift

//
//  Created by Leon on 2025/7/12.
//

import Foundation
import UIKit
//import R

@objcMembers class NavigationView: UIView {
    
    var alpha0Title:String?{
        didSet{
            titleLabel.alpha = 0
            titleLabel.text = alpha0Title
        }
    }
    
    var title:String?{
        didSet{
            titleLabel.text = title
        }
    }
    
    /// \> 0
    var paddingRightForRightStack:CGFloat = 2{
        didSet{
            rightStackH.snp.updateConstraints { make in
                make.trailing.equalToSuperview().offset(-paddingRightForRightStack)
            }
        }
    }
    
    var tapBackButtonAction : (() -> Void)?
    var bgView:UIView!
    
    @objc lazy var leftStackH : UIStackView = {
        let v = UIStackView()
        v.spacing = 0
        v.alignment = .center
        addSubview(v)
        v.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(8) // 8+insets.left(16) = 24
            make.bottom.equalToSuperview()
            make.height.equalTo(44)
        }
        return v
    }()
    
    @objc lazy var centerStackH : UIStackView = {
        let v = UIStackView()
        v.spacing = 5
        v.alignment = .center
        addSubview(v)
        v.snp.makeConstraints { make in
            make.centerY.equalTo(backButton)
            make.centerX.equalToSuperview()
            make.leading.greaterThanOrEqualTo(leftStackH.snp.trailing)
            make.trailing.lessThanOrEqualTo(rightStackH.snp.leading)
        }
        return v
    }()
    
    @objc lazy var backButton: UIButton = {
        let backButton = UIButton(type: .custom)
        leftStackH.addArrangedSubview(backButton)
        backButton.setImage(R.image.nav_back_white()!, for: .normal)
        backButton.contentHorizontalAlignment = .left
        backButton.imageEdgeInsets = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: -16)
        backButton.addTarget(self, action: #selector(popAction), for: .touchUpInside)
        backButton.snp.makeConstraints { make in
            make.width.equalTo(44)
            make.height.equalTo(44)
        }
        return backButton
    }()

    @objc lazy var titleLabel: UILabel = {
        let titleLabel = UILabel()
        titleLabel.textColor = .c.ctpn
        titleLabel.preferredMaxLayoutWidth = 120
        titleLabel.textAlignment = .center
        titleLabel.font = .t.ttm//.fredokaOne(size: 18)
//        addSubview(titleLabel)
        centerStackH.addArrangedSubview(titleLabel)

//        titleLabel.snp.makeConstraints { make in
//            make.centerY.equalTo(backButton)
//            make.centerX.equalToSuperview()
//            make.leading.greaterThanOrEqualTo(leftStackH.snp.trailing)
//            make.trailing.lessThanOrEqualTo(rightStackH.snp.leading)
//        }
        return titleLabel
    }()
    
    @objc lazy var iconImgView: UIImageView = {
        let icon = UIImageView()
        icon.isHidden = true
        icon.cornerRadius = 10.0
        icon.snp.makeConstraints { make in
            make.width.height.equalTo(20)
        }
        centerStackH.addArrangedSubview(icon)

        return icon
    }()
    

    /// btn 44x44 is ok
    @objc lazy var rightStackH: UIStackView = {
        let rightStackH = UIStackView()
        rightStackH.axis = .horizontal
        rightStackH.spacing = 0
        rightStackH.alignment = .center
        addSubview(rightStackH)
        rightStackH.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-paddingRightForRightStack)
            make.centerY.equalTo(backButton)
        }
        return rightStackH
    }()

    @objc lazy var rightButton: UIButton = {
        let rightButton = UIButton(type: .custom)
        rightButton.isHidden = true
        rightButton.setTitleColor(.black, for: .normal)
        rightButton.titleLabel?.font = .t.tbm//.popSemiBold(size: 16)
        rightButton.contentHorizontalAlignment = .right
        rightStackH.addArrangedSubview(rightButton)
        rightButton.snp.makeConstraints { make in
            make.height.equalTo(44)
            make.width.equalTo(44)
        }
        return rightButton
    }()

    @objc lazy var styleMainButton: StyleButton = {
        let styleMainButton = StyleButton()
        styleMainButton.primary(size: .small)
        rightStackH.addArrangedSubview(styleMainButton)
        paddingRightForRightStack = 24
        return styleMainButton
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        bgView = {
            let v = UIView()
            addSubview(v)
            v.backgroundColor = .c.cbd
            v.snp.makeConstraints { make in
                make.edges.equalToSuperview()
            }
            return v
        }()
        
        //backgroundColor = .c.cbd
        backgroundColor = .clear
        
        iconImgView.backgroundColor = .blue
    }

    @available(*, unavailable)
    required init?(coder _: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    @objc func popAction() {
        if let block = tapBackButtonAction{
            block()
        }else{
            if let vc = viewController() as? CLBaseViewController {
                vc.close()
            }else{
                viewController()?.navigationController?.popViewController(animated: true)
            }
        }
    }

    @objc open func setupBackButtonCloseIcon(black: Bool = false) {
        if black {
            backButton.setImage(R.image.icon_close_20_black(), for: .normal)
        } else {
            backButton.setImage(R.image.icon_close_20(), for: .normal)
            
        }
        backButton.isHidden = false
    }
}

// MARK: - Public
// 支持渐变 NavigationView
extension NavigationView {
    
    //⚠️ Please use NaviAlphaHandle to adjust alpha
    func changeAlpha(alpha: CGFloat) {
        titleLabel.isHidden = false

        if titleLabel.alpha == alpha {
            return
        }

        backgroundColor = UIColor.white.withAlphaComponent(alpha)
        titleLabel.alpha = alpha
        backButton.alpha = alpha
    }
    
    func setupNaviRightStyleButton(){
        styleMainButton.isHidden = false
        paddingRightForRightStack = 24
    }
    
    func setupBgViewToStatusBarHeight(){
        self.bgView.snp.remakeConstraints { make in
            make.top.leading.trailing.equalToSuperview()
            make.height.equalTo(UIWindow.statusBarHeight)
        }
    }
}
