//
//  TSPlogMoreVC.swift
//  KittensTravelNotes
//
//  Created by 100Years on 2025/7/7.
//

class TSPlogMoreVC: TSBaseVC {

    var sectionModel:TSAIDataSectionModel
    init(sectionModel: TSAIDataSectionModel) {
        self.sectionModel = sectionModel
        super.init()
    }
    
    @MainActor required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    lazy var models:[TSAIDataItemModel] = {
        if let list = sectionModel.items as? [TSAIDataItemModel] {
            return list
        }else if let list = sectionModel.items as? [[TSAIDataItemModel]] {
            return list.first ?? []
        }
        return []
    }()
    
    //###################################### 集合视图 ######################################
    let collectionViewBtootm:CGFloat = k_Height_safeAreaInsetsBottom() + 10
    lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        
        let itemW = (k_ScreenWidth-32.0-18-2.0)/2.0
        let itemH = kGetScaleHeight(originalSize: CGSize(width: 163, height: 269), width: itemW)

        layout.itemSize = CGSize(width: itemW, height: itemH)
        layout.minimumInteritemSpacing = 8
        layout.minimumLineSpacing = 18
        layout.sectionInset = UIEdgeInsets(top: 8, left: 16, bottom:collectionViewBtootm, right: 16)
        
        let collectionView = TSBaseCollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(TSPlogMoreVCCell.self, forCellWithReuseIdentifier: "TSPlogMoreVCCell")
        return collectionView
    }()
    
    override func createData() {
        
    }
    
    override func createView() {
        addGradientColorTop()
        setPageTitle(sectionModel.title.localized)
        contentView.addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
}

extension TSPlogMoreVC: 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 cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TSPlogMoreVCCell", for: indexPath) as? TSPlogMoreVCCell{
                cell.model = dbModel
                return cell
            }
        }
        return UICollectionViewCell()
    }

    public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if let model = models.safeObj(At: indexPath.row) {
            TSPlogVC.clickCell(target: self,indexPath: indexPath, itemModel:model,secModel: sectionModel)
        }
    }
}


class TSPlogMoreVCCell: 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:.plogPhotoBigFrame,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(20)
            make.leading.equalTo(12)
            make.trailing.equalTo(-12)
            make.bottom.equalTo(-52)
        }
        
        frontImageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        textLabel.snp.makeConstraints { make in
            make.leading.equalTo(12)
            make.trailing.equalTo(-44)
            make.bottom.equalTo(-12)
            make.height.equalTo(38)//28
//            make.bottom.equalTo(0)
//            make.height.equalTo(52)
        }
    
    }
}
