//
//  TSGenmojiVC.swift
//  AIEmoji
//
//  Created by 100Years on 2025/1/15.
//
import Alamofire
class TSGenmojiVC: TSBaseVC {

    var viewModel:TSGenmojiViewModel = TSGenmojiViewModel()
    
    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_genmoji",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)
        }
        
        navBarView.barView.addSubview(vipBtn)
        vipBtn.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.trailing.equalTo(-62)
            make.width.height.equalTo(24)
        }
        
        return navBarView
    }()
    
    
    lazy var collectionComponent: TSCollectionViewComponent = {
        let layout = UICollectionViewFlowLayout()
        let cp = TSCollectionViewComponent(frame: CGRect.zero, layout: layout, attributes: [ :])
        cp.collectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: k_Height_TabBar, right: 0)
        cp.itemActionHandler = { [weak self] cellCp, indexPath in
            guard let self = self else { return }
            if let text = cellCp as? String ,indexPath == IndexPath(item: 0, section: 0) {
                generateImage(text: text)
            }
        }
        
        cp.sectionActionHandler = { [weak self] cellCp, indexPath in
            guard let self = self else { return }
            if let cmd = cellCp as? String, cmd == "delete"  {
                showCustomAlert(message: "Are you sure to delete".localized, deleteHandler:  {
                    self.viewModel.removeAllHistoryList()
                    self.collectionComponent.clear()
                    self.collectionComponent.reloadView(with: self.viewModel.colDataArray)
                })
            }
        }
        
        cp.itemDidSelectedHandler = { [weak self] (object, indexPath) in
            guard let self = self else { return }
            view.endEditing(true)
            if indexPath.section == 0{
                return
            }
            
            if let sections = viewModel.colDataArray.safeObj(At: indexPath.section) as? TSGenmojiCoLSectionModel{
                var dataModelArray:[TSGenmojiModel] = []
                for itemModel in sections.items {
                    dataModelArray.append(itemModel.dataModel)
                }
                
                let browseVC = TSSmallIconBrowseVC()
                browseVC.dataModelArray = dataModelArray
                browseVC.currentIndex = indexPath.item
                kPresentModalVC(target: self, modelVC: browseVC,transitionStyle: .crossDissolve)
            }


        }

        return cp
    }()

    override func createView() {
        edgesForExtendedLayout = []
        navBarContentView.addSubview(navBarView)
        navBarView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        contentView.addSubview(collectionComponent.collectionView)
        collectionComponent.collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        
        collectionComponent.clear()
        collectionComponent.reloadView(with:viewModel.colDataArray)
    }
    
    override func dealThings() {
        vipBtn.isHidden = PurchaseManager.default.isVip
        NotificationCenter.default.addObserver(self, selector: #selector(vipInfoChanged), name: .kPurchaseDidChanged, object: nil)
    }
    
    @objc func vipInfoChanged() {
        kExecuteOnMainThread {
            self.vipBtn.isHidden = PurchaseManager.default.isVip
            self.collectionComponent.reloadData()
        }
    }
}


extension TSGenmojiVC {
    func generateImage(text:String) {
        
        //判断 vip
        if kJudgeVip(externalBool: kPurchaseDefault.freeNumAvailable() == false, vc: self) {[weak self] in
            guard let self = self else { return }
        }{ return }
        
        let gennerateVC = TSGenmojiGennerateVC(aiText: text) {[weak self] model in
            guard let self = self else { return }
            if viewModel.saveModel(model:model) {
                collectionComponent.clear()
                collectionComponent.reloadView(with:viewModel.colDataArray)
            }else{
                collectionComponent.reloadData()
            }
        }
        
        kPresentModalVC(target: self, modelVC: gennerateVC,transitionStyle: .crossDissolve)
    }
}
