//
//  CLRoleTagsView.swift
//  Visual_Novel_iOS
//
//  Created by mh on 2025/10/16.
//

import UIKit

class CLRoleTagsView: UIView {
    
//    let tags = ["#浪漫", "#温柔", "#多愁善感", "#深情", "#this is good", "#沙瓦迪", "#科技哈", "#等好a", "#a"]
    var tagModels: [CLRoleTagsModel] = [
        CLRoleTagsModel(name: "#浪漫浪漫浪漫浪漫浪漫浪漫浪漫浪漫浪漫漫浪漫浪漫浪漫漫浪漫浪漫浪漫漫浪漫浪漫浪漫", isSelected: false),
        CLRoleTagsModel(name: "#温柔", isSelected: false),
        CLRoleTagsModel(name: "#多愁善感", isSelected: false),
        CLRoleTagsModel(name: "#this is good", isSelected: false),
        CLRoleTagsModel(name: "#沙瓦迪", isSelected: false),
        CLRoleTagsModel(name: "#科技哈", isSelected: false),
        CLRoleTagsModel(name: "#等好a", isSelected: false),
        CLRoleTagsModel(name: "#a", isSelected: false),
        CLRoleTagsModel(name: "#浪漫", isSelected: false),
    ]
    
    lazy var collectionView: AutoHeightCollectionView = {
        let layout = TagFlowLayout()
        layout.interItemSpacing = 10       // 横向间距
        layout.lineSpacing = 8
        layout.maxLineWidth = UIScreen.width - 20.0
        layout.delegate = self

        let collectionView = AutoHeightCollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.backgroundColor = .clear
        collectionView.setContentHuggingPriority(.required, for: .vertical)
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.register(RoleTagCollectionCell.self, forCellWithReuseIdentifier: "RoleTagCollectionCell")
        return collectionView
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupSubviews()
//        setupDatas()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: Subviews
    func setupSubviews() {
        
        addSubview(collectionView)
        
        collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
        
    // MARK: data
    private func setupDatas() {
        
    }
}

extension CLRoleTagsView: UICollectionViewDataSource, UICollectionViewDelegate, TagFlowLayoutDelegate {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        tagModels.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: RoleTagCollectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: "RoleTagCollectionCell", for: indexPath) as! RoleTagCollectionCell
        
        cell.setupData(model: tagModels[indexPath.item])
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("点击了标签：\(tagModels[indexPath.item].name)")
        tagModels[indexPath.item].isSelected.toggle()
//        collectionView.reloadItems(at: [indexPath])
        collectionView.reloadData()
        
    }
    
    // 根据文字计算 size
    func collectionView(_ collectionView: UICollectionView,
                        layout: TagFlowLayout,
                        sizeForItemAt indexPath: IndexPath) -> CGSize {
        let text = tagModels[indexPath.item].name
        let font = UIFont.boldSystemFont(ofSize: 12)
        let w = text.size(withAttributes: [.font: font]).width + 20   // 左右 10 pt padding
        return CGSize(width: min(w, layout.maxItemWidth), height: 23)
    }
    
}


final class AutoHeightCollectionView: UICollectionView {
    override var contentSize: CGSize {
        didSet { invalidateIntrinsicContentSize() }
    }
    override var intrinsicContentSize: CGSize {
        CGSize(width: UIView.noIntrinsicMetric, height: contentSize.height)
    }
}
