//
//  TSChatViewController+Nav.swift
//  AIEmoji
//
//  Created by 100Years on 2025/2/17.
//

extension TSChatViewController {
    
   var creatNavBarContentView: UIView{
        let view = UIView()
        view.backgroundColor = .clear
        return view
    }
    
    var creatNormalNavBarView: TSNormalNavigationBarView{
        let view = TSNormalNavigationBarView()
        return view
    }
    
     var creatVipBtn: UIButton{
        let vipBtn = UIButton.createButton(image: UIImage(named: "nav_vip")) { [weak self]  in
            guard let self = self else { return }
            TSPurchaseVC.show(target: self) {
      
            }
        }
        return vipBtn
    }
    
     var creatNavBarView: TSBaseNavContentBarView{
        let navBarView = TSBaseNavContentBarView()
        
        let titleImageView = UIImageView.createImageView(imageName: "nav_title_aichat",contentMode: .scaleToFill)
        navBarView.barView.addSubview(titleImageView)
        titleImageView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(16)
        }
        
//        let setBtn = UIButton.createButton(image: UIImage(named: "setting")) { [weak self]  in
//            guard let self = self else { return }
//            let setingVC = TSSetingVC()
//            setingVC.hidesBottomBarWhenPushed = true
//            navigationController?.pushViewController(setingVC, animated: true)
//        }
//        navBarView.barView.addSubview(setBtn)
//        setBtn.snp.makeConstraints { make in
//            make.centerY.equalToSuperview()
//            make.trailing.equalTo(-16)
//            make.width.height.equalTo(24)
//        }
        
        let historyBtn = UIButton.createButton(image: UIImage(named: "aichat_history")) { [weak self]  in
            guard let self = self else { return }
            let historyVC = TSAIChatHistoryVC()
            historyVC.hidesBottomBarWhenPushed = true
            navigationController?.pushViewController(historyVC, animated: true)
        }
        navBarView.barView.addSubview(historyBtn)
        historyBtn.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalTo(-16)
            make.width.height.equalTo(24)
        }
        
        navBarView.barView.addSubview(vipBtn)
        vipBtn.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalTo(-60)
            make.width.height.equalTo(24)
        }
        
        return navBarView
    }
    
    public func addNormalNavBarView(){
        navBarContentView.addSubview(normalNavBarView)
        normalNavBarView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    public func setTitleText(_ title: String) {
       _ = normalNavBarView.setTitleName(NSLocalizedString(title, comment: ""))
    }

    public func setPageTitle(_ title: String) {
        let pageTitle = title
        let backTitle = " "
        setTitleText(pageTitle)
        _ = setNavigationItem(backTitle, imageName: "navi_back_white", direction: .left, action: #selector(navBarClickLeftAction))
    }
    
    @objc public func navBarClickLeftAction() {
        debugPrint("navBarClickLeftAction -> \(type(of: self))")
        pop()
    }
    
    public func pop() {
        if navigationController == nil {
            dismiss(animated: true, completion: nil)
        } else if navigationController?.presentingViewController != nil, navigationController?.viewControllers.count == 1 {
            navigationController?.dismiss(animated: true, completion: nil)
        } else {
            navigationController?.popViewController(animated: true)
        }
    }
    
    public func setNavigationItem(_ name: String, imageName: String, direction: NSTextAlignment, action: Selector) -> UIButton {
        if direction == .left {
            return normalNavBarView.setLeftNavigationItem(name: name, imageName: imageName, target: self, action: action)
        } else {
            return normalNavBarView.setRightNavigationItem(name: name, imageName: imageName, target: self, action: action)
        }
    }
    
    
}

 
extension TSChatViewController {
    
    func configureNaviBarView(){
        
        navigationItem.title = "MessageKit"
        edgesForExtendedLayout = [.top]
        
        view.addSubview(navBarContentView)
        navBarContentView.snp.makeConstraints { make in
            make.leading.top.trailing.equalToSuperview()
            make.height.equalTo(k_Nav_Height)
        }
        
        if viewModel.uiStyle == .history {
            addNormalNavBarView()
            setPageTitle("History".localized)
            _ = setNavigationItem("", imageName: "delete_white", direction: .right, action: #selector(clickDelete))
        }else{
            navBarContentView.addSubview(navBarView)
            navBarView.snp.makeConstraints { make in
                make.edges.equalToSuperview()
            }
        }

    }
    
    @objc func clickDelete(){
        showCustomAlert(message: "Are you sure to delete".localized, deleteHandler:  { [weak self]  in
            guard let self = self else { return }
            viewModel.dbAIChatList.delete()
        
            deleteBlock?()
            pop()
        })
    }

    
}
