//
//  TSMirrorShapePickerView.swift
//  Girly
//
//  Created by 100Years on 2025/1/8.
//

class TSMirrorShapePickerView: TSBaseView {
    
    var dataArray:[TSMirrorItemModel] = [TSMirrorItemModel](){
        didSet{
            let count = dataArray.count
            let itemWs = layout.itemSize.width * CGFloat(count)
            var w = 73 + 52 + itemWs
            w = k_ScreenWidth - w
            w = w / CGFloat(count-1)
            
            layout.minimumInteritemSpacing = w - 1
            collectionView.reloadData()
        }
    }
    var selectedColorComplete:((TSMirrorItemModel)->Void)?
    
    lazy var layout: UICollectionViewFlowLayout = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        layout.itemSize = CGSize(width: 40, height: 40)
        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(TSMirrorShapePickerCell.self, forCellWithReuseIdentifier: TSMirrorShapePickerCell.cellID)
        if #available(iOS 11.0, *) {
            collectionView.contentInsetAdjustmentBehavior = .never
        }
        return collectionView
    }()
    
    var currentIndexPath:IndexPath?
    
    override func creatUI() {
        contentView.cornerRadius = 26.0
        contentView.backgroundColor = UIColor.fromHex("#FFFFFF", alpha: 0.1)
        
        contentView.addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalTo(26.0)
            make.trailing.equalTo(-26.0)
            make.height.equalTo(40.0)
        }
        
        
        kDelayMainShort {
            self.collectionView.selectItem(at: IndexPath(item: 0, section: 0), animated: false, scrollPosition: .top)
        }
    }
    
    func unCheck(){
        if let currentIndexPath = currentIndexPath {
            collectionView.deselectItem(at: currentIndexPath, animated: true)
        }
    }
}

extension TSMirrorShapePickerView: 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: TSMirrorShapePickerCell.cellID, for: indexPath)
        if let cell = cell as? TSMirrorShapePickerCell,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 TSMirrorShapePickerCell: TSBaseCollectionCell {
    
    static let cellID = "TSMirrorShapePickerCell"

    override var isSelected: Bool{
        didSet{
            checkView.borderColor = isSelected ? .themeColor : .clear
        }
    }
    
    var colorModel:TSMirrorItemModel = TSMirrorItemModel(){
        didSet{
            shapeImageView.image = UIImage(named: colorModel.shapeEntity)
        }
    }
    

    lazy var shapeImageView: UIImageView = {
        let imageView = UIImageView.createImageView(imageName: "")
        return imageView
    }()

    lazy var checkView: UIView = {
        let checkView = UIView()
        checkView.cornerRadius = self.bounds.size.width/2
        checkView.borderColor = UIColor.clear
        checkView.borderWidth = 2
        return checkView
    }()
    
    override func creatUI() {
    
        bgContentView.addSubview(checkView)
        checkView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }

        bgContentView.addSubview(shapeImageView)
        shapeImageView.snp.makeConstraints { make in
            make.width.height.equalTo(24.0)
            make.center.equalToSuperview()
        }
    }
}
