//
//  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 collectionComponent: TSCollectionViewComponent = {
        let layout = UICollectionViewFlowLayout()
        let cp = TSCollectionViewComponent(frame: CGRect.zero, layout: layout, attributes: [ :])
        cp.collectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 20, 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 }
            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 = []
        
        addNormalNavBarView()
        setPageTitle("AI Text to Emoji".localized)
        
        contentView.addSubview(collectionComponent.collectionView)
        collectionComponent.collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(clickCollectionView))
        tapGesture.cancelsTouchesInView = false // 确保不影响其他点击事件
        collectionComponent.collectionView.addGestureRecognizer(tapGesture)
        
        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()
        }
    }
    
    @objc func clickCollectionView() {
        view.endEditing(true)
    }
}


extension TSGenmojiVC {
    func generateImage(text:String) {
        
        //判断 vip
        if kJudgeVip(externalBool: kPurchaseDefault.freeNumAvailable(type: .generatePic) == 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)
    }
}
