//
//  TSBaseNavigationBarView.swift
//  TSLiveWallpaper
//
//  Created by 100Years on 2024/12/20.
//


open class TSBaseNavContentBarView: UIView {
    
    open lazy var statuView: UIView = {
       let view = UIView()
       return view
   }()

    open lazy var barView: UIView = {
       let view = UIView()
       return view
   }()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(statuView)
        addSubview(barView)
        
        statuView.snp.makeConstraints { make in
            make.top.leading.trailing.equalTo(0)
            make.height.equalTo(k_Height_StatusBar)
        }

        barView.snp.makeConstraints { make in
            make.leading.trailing.equalTo(0)
            make.top.equalTo(k_Height_StatusBar)
            make.height.equalTo(k_Height_NavBar)
        }

    }
    
    
    required public init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

open class TSNormalNavigationBarView: TSBaseNavContentBarView {

    // MARK: - Properties
     var navBgColor: UIColor!
     var navTitleColor: UIColor!
     var navTitleFont: UIFont!
     var navLeftFont: UIFont!
     var navRightFont: UIFont!

    public lazy var leftNavBtn: UIButton = {
        let button = createNavButton()
        button.contentHorizontalAlignment = .leading
        button.contentEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        return button
    }()

    public lazy var titleNavBtn: UIButton = {
        let button = createNavButton()
        button.titleLabel?.font = navTitleFont
        return button
    }()

    public lazy var rightNavBtn: UIButton = {
        let button = createNavButton()
        button.titleLabel?.font = navRightFont
        button.titleLabel?.adjustsFontSizeToFitWidth = true
        button.contentHorizontalAlignment = .trailing
        button.setTitleColor(UIColor.themeColor, for: .normal)
        button.contentEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        return button
    }()

    // MARK: - Initializers
    override init(frame: CGRect) {
        super.init(frame: frame)
        createData()
        createView()
    }
    
    @MainActor required public init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    // MARK: - Setup Methods
    private func createData() {
        navBgColor = UIColor.clear
        navTitleColor = .mainText
        navTitleFont = .font(size: 16,weight:.medium)
        navLeftFont = .font(size: 16,weight:.medium)
        navRightFont = .font(size: 16,weight:.medium)
    }

    private func createView() {
//        let space: CGFloat = 10
        let margins: CGFloat = 16

        backgroundColor = navBgColor

  
        barView.addSubview(leftNavBtn)
        barView.addSubview(titleNavBtn)
        barView.addSubview(rightNavBtn)

        leftNavBtn.snp.makeConstraints { make in
            make.leading.equalTo(margins)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(44.0)
        }
        
        rightNavBtn.snp.makeConstraints { make in
            make.trailing.equalTo(-margins)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(44.0)
        }
        
         titleNavBtn.snp.makeConstraints { make in
             make.leading.equalTo(leftNavBtn.snp.trailing).offset(margins)
             make.trailing.equalTo(rightNavBtn.snp.leading).offset(-margins)
             make.centerY.equalToSuperview()
             make.height.equalTo(44.0)
         }
         
    }

    // MARK: - Helper Methods
    public func setTitleName(_ name: String) -> UIButton {
        titleNavBtn.setTitle(name, for: .normal)
        return titleNavBtn
    }

    public func setTitleNavigationItem(name: String, imageName: String, target: Any?, action: Selector) -> UIButton {
        setNavButton(button: titleNavBtn, name: name, imageName: imageName, target: target, action: action)
        return titleNavBtn
    }

    public func setLeftNavigationItem(name: String, imageName: String, target: Any?, action: Selector) -> UIButton {
        setNavButton(button: leftNavBtn, name: name, imageName: imageName, target: target, action: action)
        return leftNavBtn
    }

    public func setRightNavigationItem(name: String, imageName: String, target: Any?, action: Selector) -> UIButton {
        setNavButton(button: rightNavBtn, name: name, imageName: imageName, target: target, action: action)
        return rightNavBtn
    }

    public func setNavButton(button: UIButton, name:String , imageName: String, target: Any?, action: Selector) {
        button.setTitle(name, for: .normal)
        
        if imageName.count > 0 {
            let image = UIImage(named: imageName)?.withRenderingMode(.alwaysOriginal)
            if let image = image {
                button.setImage(image.mirrored, for: .normal)
            }
        }

        button.addTarget(target, action: action, for: .touchUpInside)
    }

    public func createNavButton() -> UIButton {
        let button = UIButton()
        button.setTitleColor(navTitleColor, for: .normal)
        button.titleLabel?.font = navRightFont
        button.titleLabel?.adjustsFontSizeToFitWidth = true
        return button
    }
}
