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

class TSPlogTravelPlacesView: TSBaseView {
    var changeBlock:((String?)->Void)?
    var dataArray:[String] = []
    lazy var textFiled: UITextField = {
        let tf = UITextField()
        tf.backgroundColor = .clear
        tf.placeholder = "Where your cat want to go?".localized
        tf.font = .font(size: 14)
        tf.textColor = .mainText
        tf.clearButtonMode = .always
        tf.addTarget(self, action: #selector(tfValueChanged), for: .editingChanged)
        return tf
    }()
    
    lazy var textFiledView: UIView = {
        let bgView = UIView()
        bgView.backgroundColor = .white
        bgView.cornerRadius = 16
        
        bgView.addSubview(textFiled)
        textFiled.snp.makeConstraints { make in
            make.leading.equalTo(12)
            make.top.bottom.equalToSuperview()
            make.trailing.equalTo(-12)
        }
        return bgView
    }()
    
    
    lazy var headerView: TSAIDataHeaderView = {
        let headerView = TSAIDataHeaderView()
        headerView.titleLab.text = "Travel Places".localized
        headerView.moreView.isHidden = true
        return headerView
    }()
    
    lazy var collectionView: UICollectionView = {
        let layout = TSBaseCollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        layout.minimumLineSpacing = 12
//        layout.itemSize = .estimatesSize
        layout.estimatedItemSize = CGSize(width: 100, height: 30)
        let collectionView = TSBaseCollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(TSPlogTravelPlacesCell.self, forCellWithReuseIdentifier: "TSPlogTravelPlacesCell")
        collectionView.contentInset = kAIDataSection
        return collectionView
    }()
    
    
    override func creatUI() {
        
        contentView.addSubview(headerView)
        contentView.addSubview(textFiledView)
        contentView.addSubview(collectionView)
        
        headerView.snp.makeConstraints { make in
            make.leading.top.trailing.equalTo(0)
            make.height.equalTo(56)
        }
        
        textFiledView.snp.makeConstraints { make in
            make.top.equalTo(headerView.snp.bottom)
            make.leading.equalTo(16)
            make.trailing.equalTo(-16)
            make.height.equalTo(52)
        }
        
        collectionView.snp.makeConstraints { make in
            make.top.equalTo(textFiledView.snp.bottom).offset(12)
            make.leading.equalTo(0)
            make.trailing.equalTo(0)
            make.height.equalTo(30)
            make.bottom.equalToSuperview()
        }
        
    }
    
    
    @objc func tfValueChanged(){
        changeBlock?(textFiled.text)
    }
}

extension TSPlogTravelPlacesView: UICollectionViewDataSource ,UICollectionViewDelegate {
    public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if let dbModel = dataArray.safeObj(At: indexPath.row) {
            if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TSPlogTravelPlacesCell", for: indexPath) as? TSPlogTravelPlacesCell{
                cell.title = dbModel
                cell.setNeedsLayout()
                cell.layoutIfNeeded()
                return cell
            }
        }
        return UICollectionViewCell()
    }

    public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.selectItem(at: indexPath, animated: true, scrollPosition: .centeredHorizontally)
        if let str = dataArray.safeObj(At: indexPath.row) {
            textFiled.text = str
            tfValueChanged()
        }
    }
}

class TSPlogTravelPlacesCell: TSBaseCollectionCell {
    var title:String = "" {
        didSet {
            textLabel.text = title
        }
    }

    lazy var textLabel: UILabel = {
        let textLabel = UILabel.createLabel(font: .font(size: 14),textColor: .secondaryText)
        return textLabel
    }()
    
    override func creatUI() {
        bgContentView.backgroundColor = .white
        bgContentView.cornerRadius = 15
        bgContentView.addSubview(textLabel)
        textLabel.snp.makeConstraints { make in
            make.leading.equalTo(12)
            make.trailing.equalTo(-12)
            make.top.bottom.equalToSuperview()
            make.height.equalTo(30)
        }
    
    }
}
