//
//  YHCustomNavigationBar.swift
//  galaxy
//
//  Created by edy on 2024/3/15.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit

enum YHNavLeftSecondBtnType: Int {
    case none = 0
    case search = 1
}
class YHCustomNavigationBar: UIView {
    
    static let searchLeftIcon = ""
    
    var title: String? {
        didSet {
            titleLabel.text = title
        }
    }
    var backBlock:(()->())?
    var leftSecondBtnClick:(()->())?
    
    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.textColor = .navTitleColor
        label.textAlignment = .center
        label.font = UIFont.PFSC_M(ofSize:17)
        label.text = "默认标题"
        return label
    }()
    
    private lazy var backButton: UIButton = {
       let btn = UIButton()
        btn.setImage(UIImage(named: "nav_back_icon"), for: .normal)
        btn.addTarget(self, action: #selector(didBackBtnClicked), for: .touchUpInside)
        return btn
    }()
    
    private lazy var leftSecondButton: UIButton = {
       let btn = UIButton()
        btn.addTarget(self, action: #selector(didleftSecondButtonClicked), for: .touchUpInside)
        return btn
    }()
   
    static func navBar()-> YHCustomNavigationBar  {
        let bar = YHCustomNavigationBar(frame:CGRect(x: 0, y: 0, width: KScreenWidth, height: k_Height_NavContentBar))
        return bar
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        createUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func showLeftSecondButtonType(_ type: YHNavLeftSecondBtnType) {
        if type == .search {
            leftSecondButton.setImage(UIImage(named: "nav_search_icon"), for: .normal)
        }
    }
    
    func createUI() {
        self.addSubview(titleLabel)
        self.addSubview(backButton)
        self.addSubview(leftSecondButton)
        
        titleLabel.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        backButton.snp.makeConstraints { make in
            make.width.height.equalTo(30)
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(12)
        }
        
        leftSecondButton.snp.makeConstraints { make in
            make.width.height.equalTo(30)
            make.centerY.equalToSuperview()
            make.left.equalTo(backButton.snp.right).offset(5)
        }
    }
}

extension YHCustomNavigationBar {
    @objc func didBackBtnClicked() {
        if let backBlock = backBlock {
            backBlock()
        }
    }
    
    @objc func didleftSecondButtonClicked() {
        if let leftSecondBtnClick = leftSecondBtnClick {
            leftSecondBtnClick()
        }
    }
}
