//
//  TSPlogBootImageVC.swift
//  KittensTravelNotes
//
//  Created by 100Years on 2025/7/9.
//

class TSPlogImageBrowseVC: TSBaseVC {
    var dataModelArray:[TSAIDataItemModel]
    var currentIndex:Int{
        didSet{
            reloadUI()
        }
    }
    init(dataModelArray:[TSAIDataItemModel],currentIndex:Int) {
        self.dataModelArray = dataModelArray
        self.currentIndex = currentIndex
        super.init()
    }
    
    @MainActor required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var collectionView: UICollectionView = {
        let layout = TSBaseCollectionViewFlowLayout()
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .horizontal
        layout.itemSize = CGSize(width: k_ScreenWidth, height: k_ScreenHeight)
        
        let collectionView = TSBaseCollectionView(frame: CGRectMake(0, 0, k_ScreenWidth, k_ScreenHeight), collectionViewLayout: layout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(TSPlogImageBrowseCell.self, forCellWithReuseIdentifier: "TSPlogImageBrowseCell")
//        collectionView.register(TSAIVideoBrowseCell.self, forCellWithReuseIdentifier: videoCellId)
        collectionView.isPagingEnabled = true
        collectionView.isHidden = true
        return collectionView
    }()
    
    lazy var submitBtn: UIButton = {
        let submitBtn = kCreateNormalSubmitBtn(title: "Let my cat go".localized) { [weak self]  in
            guard let self = self else { return }
            generateImage()
        }
        return submitBtn
    }()

    override func createView() {
        contentView.snp.makeConstraints { make in
            make.top.equalTo(0)
        }
        contentView.addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        contentView.addSubview(submitBtn)
        submitBtn.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.width.equalTo(250*kDesignScale)
            make.height.equalTo(56)
            make.bottom.equalTo(-k_Height_safeAreaInsetsBottom())
        }
        
        reloadUI()
        
        
        DispatchQueue.main.async {
            self.collectionView.isHidden = false
            self.collectionView.reloadData()
            self.collectionView.setContentOffset(CGPoint(x: CGFloat(self.currentIndex) * self.collectionView.frame.size.width, y: 0), animated: false)

            if let flowLayout = self.collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
                flowLayout.itemSize = self.collectionView.bounds.size
            }
        }
    }

    override func dealThings() {

    }
    
    func generateImage() {
        if let model = dataModelArray.safeObj(At: currentIndex){
            TSPlogVC.enterSelectPhotos(target: self,style: .ptp) { [weak self] image in
                guard let self = self else { return }
                kPushVC(target: self, modelVC: TSPlogUploadPhotoVC(upLoadImage:image, generatorStyle: model.style,dataModel: model.generateModel))
            }
        }
    }
    
    func reloadUI() {
        if let model = dataModelArray.safeObj(At: currentIndex){
            setPageTitle(model.generateModel?.inputText ?? "")
        }
    }
}

//MARK: UICollectionViewDataSource
extension TSPlogImageBrowseVC:UICollectionViewDataSource,UICollectionViewDelegate {
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        resetIndexWithOffset(scrollView)
    }

    func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
        resetIndexWithOffset(scrollView)
    }

    private func resetIndexWithOffset(_ scrollView: UIScrollView) {
        let item = Int((scrollView.contentOffset.x / scrollView.bounds.width).rounded())
        currentIndex = item
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataModelArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if let model = dataModelArray.safeObj(At: indexPath.item){
//            if model.isVideo {
//                let cell = collectionView.dequeueReusableCell(withReuseIdentifier: videoCellId, for: indexPath) as! TSAIVideoBrowseCell
//                cell.model = model.getModel()
//                return cell
//            }else{
                let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TSPlogImageBrowseCell", for: indexPath) as! TSPlogImageBrowseCell
                cell.model = model
                return cell
//            }
        }
        
        return UICollectionViewCell()
    }

}

class TSPlogImageBrowseCell : TSBaseCollectionCell{
    lazy var netWorkImageView : UIImageView = {
        let netWorkImageView = UIImageView.createImageView(imageName: "",contentMode: .scaleAspectFit)
        return netWorkImageView
    }()

    override func creatUI() {
        let w = k_ScreenWidth
        let h = k_ScreenHeight
        bgContentView.addSubview(netWorkImageView)
        netWorkImageView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.equalTo(w)
            make.height.equalTo(h)
        }
    }
    
    var model:TSAIDataItemModel?{
        didSet{
            netWorkImageView.setAsyncImage(urlString: model?.viewModel.imageURLString,placeholder: .plogPlaceholder)
        }
    }
}
