//
//  TSColorPickerView.swift
//  Girly
//
//  Created by 100Years on 2025/1/7.
//

class TSColorPickerView: TSBaseView {
    
    var dataArray:[TSLightItemModel] = [TSLightItemModel](){
        didSet{
            collectionView.reloadData()
        }
    }
    var selectedColorComplete:((TSLightItemModel)->Void)?
    
    lazy var layout: UICollectionViewFlowLayout = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        layout.itemSize = CGSize(width: 40, height: 40)
        layout.minimumInteritemSpacing = 16.0
        layout.minimumLineSpacing = 0.0
        return layout
    }()
    
    lazy var collectionView: UICollectionView = {
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.backgroundColor = .clear
        collectionView.register(TSColorPickerCell.self, forCellWithReuseIdentifier: TSColorPickerCell.cellID)
        if #available(iOS 11.0, *) {
            collectionView.contentInsetAdjustmentBehavior = .never
        }
        return collectionView
    }()
    
    var currentIndexPath:IndexPath = IndexPath(item: 0, section: 0)
    
    override func creatUI() {
        addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
    }
    
    func unCheck(){
        collectionView.deselectItem(at: currentIndexPath, animated: true)
    }
}


extension TSColorPickerView: UICollectionViewDataSource ,UICollectionViewDelegate {
    
    public func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: TSColorPickerCell.cellID, for: indexPath)
        if let cell = cell as? TSColorPickerCell,let colorModel = dataArray.safeObj(At: indexPath.item){
            cell.colorModel = colorModel
        }
        return cell
    }

    public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if let colorModel = dataArray.safeObj(At: indexPath.item){
            selectedColorComplete?(colorModel)
            currentIndexPath = indexPath
        }
    }
}


class TSColorPickerCell: TSBaseCollectionCell {
    
    static let cellID = "TSColorPickerCell"

    override var isSelected: Bool{
        didSet{
            imageView.isHidden = !isSelected
        }
    }
    
    var colorModel:TSLightItemModel = TSLightItemModel(){
        didSet{
            if colorModel.image.count > 0 {
                shapeImageView.isHidden = false
                colorView.isHidden = true
                shapeImageView.image = UIImage(named: colorModel.image)
            }else{
                shapeImageView.isHidden = true
                colorView.isHidden = false
                
                if colorModel.color.contains("-") {
                    colorView.addGradientBg(colors: colorModel.color.cgColors(separator: "-"),layerSize: CGSize(width: 40, height: 40))
                }else{
                    colorView.backgroundColor = colorModel.color.color
                }
            }
        }
    }
    
    lazy var imageView: UIImageView = {
        let imageView = UIImageView.createImageView(imageName: "select")
        imageView.isHidden = true
        return imageView
    }()
    
    lazy var shapeImageView: UIImageView = {
        let imageView = UIImageView.createImageView(imageName: "")
        imageView.isHidden = true
        return imageView
    }()

    lazy var colorView: UIView = {
        let colorView = UIView()
        return colorView
    }()
    
    override func creatUI() {
        
        bgContentView.addSubview(shapeImageView)
        shapeImageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }

        bgContentView.addSubview(colorView)
        colorView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        colorView.cornerRadius = self.bounds.size.width/2
        
        bgContentView.addSubview(imageView)
        imageView.snp.makeConstraints { make in
            make.width.height.equalTo(24.0)
            make.center.equalToSuperview()
        }
        
    }
}
