//
//  TSAIChatVC.swift
//  AIEmoji
//
//  Created by 100Years on 2025/2/9.
//

class TSAIChatContainerVC: TSBaseVC {
    
    
    
    var deleteBlock:(()->Void)?
    

    lazy var vipBtn: 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
    }()
    
    lazy var navBarView: 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(-60)
            make.width.height.equalTo(24)
        }
        
        navBarView.barView.addSubview(vipBtn)
        vipBtn.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalTo(-104)
            make.width.height.equalTo(24)
        }
        
        return navBarView
    }()
    
    
    lazy var viewModel : TSAIChatVM = {
        let viewModel = TSAIChatVM()
        return viewModel
    }()
    
    
    lazy var chatVC: TSChatViewController = {
        let chatVC = TSChatViewController()
        chatVC.viewModel = viewModel
        return chatVC
    }()
    
    
    override func createView() {
        edgesForExtendedLayout = []
    
        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()
            }
        }
        
        addChild(chatVC)
        contentView.addSubview(chatVC.view)
        
        chatVC.view.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()
        })
        
        
    }
}
