//
//  TSPlogMoreCell.swift
//  KittensTravelNotes
//
//  Created by 100Years on 2025/7/5.
//

class TSPlogMoreCell : TSPlogBaseCell {
    lazy var collectionView: UICollectionView = {
        let layout = TSBaseCollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        layout.minimumLineSpacing = 12
        layout.itemSize = CGSize(width: 134, height: 236)
        let collectionView = TSBaseCollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(TSPlogMoreBaseCell.self, forCellWithReuseIdentifier: "TSPlogMoreBaseCell")
//        collectionView.register(TSAILIstStyleMoreAnimationGifCell.self, forCellWithReuseIdentifier: "TSAILIstStyleMoreAnimationGifCell")
        collectionView.contentInset = kAIDataSection
        return collectionView
    }()
    var models:[TSAIDataItemModel] = []
    override func upDateModel() {
        if let models = items as? [TSAIDataItemModel] {
            self.models = models
        }
        collectionView.reloadData()
    }
    
    override func creatUI() {
        bgContentView.addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
}
extension TSPlogMoreCell: UICollectionViewDataSource ,UICollectionViewDelegate {
    public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return models.count
    }
    
    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if let dbModel = models.safeObj(At: indexPath.row) {
//            if let animationModel = dbModel.viewModel as? TSAIDataAnimationItemVM {
//            if animationModel.style == .gif {
//                    if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TSAILIstStyleMoreAnimationGifCell", for: indexPath) as? TSAILIstStyleMoreAnimationGifCell{
//                        cell.model = dbModel
//                        return cell
//                    }
//                }
//            }else{
                if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TSPlogMoreBaseCell", for: indexPath) as? TSPlogMoreBaseCell{
                    cell.model = dbModel
                    return cell
                }
//            }
        }
        return UICollectionViewCell()
    }

    public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if let model = models.safeObj(At: indexPath.row) {
            clickBlock?(model)
        }
    }
}

class TSPlogMoreBaseCell: TSBaseCollectionCell {
    var model:TSAIDataItemModel? {
        didSet {
            guard let model = model else { return }
            imageView.setAsyncImage(urlString: model.viewModel.imageURLString,placeholder: .plogPlaceholder)

            if let generateModel = model.generateModel {
                textLabel.text = generateModel.inputText
            }
        }
    }

    lazy var imageView: UIImageView = {
        let imageView = UIImageView.createImageView(contentMode: .scaleAspectFill,backgroundColor: .white.withAlphaComponent(0.2))
        return imageView
    }()
    
    
    lazy var frontImageView: UIImageView = {
        let imageView = UIImageView.createImageView(image:.plogPhotoFrame,contentMode: .scaleToFill)
        return imageView
    }()
    
    lazy var textLabel: UILabel = {
        let textLabel = UILabel.createLabel(font: .font(name: .CoveredByYourGrace,size: 14,weight: .bold),textColor: "#3D3D3D".uiColor,adjustsFontSizeToFitWidth: false)
        return textLabel
    }()
    
    override func creatUI() {

        bgContentView.addSubview(imageView)
        bgContentView.addSubview(frontImageView)
        bgContentView.addSubview(textLabel)
        
        imageView.snp.makeConstraints { make in
            make.top.equalTo(14.0)
            make.width.equalTo(122)
            make.height.equalTo(173)
            make.centerX.equalToSuperview()
        }
        
        frontImageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        textLabel.snp.makeConstraints { make in
            make.leading.equalTo(10)
            make.trailing.equalTo(-38)
            make.bottom.equalTo(-9)
            make.height.equalTo(38)//28
//            make.bottom.equalTo(0)
//            make.height.equalTo(52)
        }
    
    }
}


//import Kingfisher
//class TSAILIstStyleMoreAnimationGifCell: TSPlogMoreBaseCell {
//    override var model:TSAIDataItemModel? {
//        didSet {
//            guard let model = model else { return }
//            textLabel.text = model.name
//    
//            if let animationModel = model.viewModel as? TSAIDataAnimationItemVM,let imageNamed = animationModel.imageNameds.first {
//                if let gifURL = Bundle.main.url(forResource: imageNamed, withExtension: "gif") {
//                    animatedImageView.kf.setImage(with: gifURL, options: [.cacheOriginalImage]){ result in
//                        switch result {
//                        case .success(_):
//                            kMainAfter(1.0) {
//                                self.animatedImageView.addRealisticFlashEffect {
//                                    self.animatedImageView.startAnimating()
//                                }
//                            }
//                        case .failure(let error):
//                            print("GIF 加载失败: \(error.localizedDescription)")
//                        }
//                    }
//                }
//            }
//        }
//    }
//
//    lazy var animatedImageView: AnimatedImageView = {
//        let animatedImageView = AnimatedImageView()
//        animatedImageView.autoPlayAnimatedImage = false
//        animatedImageView.repeatCount = .once
//        animatedImageView.delegate = self
//        return animatedImageView
//    }()
//    
//    override func creatUI() {
//        super.creatUI()
//        imageView.image = nil
//        imageView.addSubview(animatedImageView)
//        animatedImageView.snp.makeConstraints { make in
//            make.edges.equalToSuperview()
//        }
//
//    }
//    
//
//}
//
//extension TSAILIstStyleMoreAnimationGifCell : AnimatedImageViewDelegate {
//    
//    func animatedImageViewDidFinishAnimating(_ imageView: AnimatedImageView){
//        self.animatedImageView.addRealisticFlashEffect {
//            self.model = self.model
//        }
//
//    }
//    
//}

