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

class TSLightShapeToolView: TSBaseView {
    
    lazy var colorSlider: ColorSlider = {
        let colorSlider = kCommonColorSlider()
        colorSlider.color = .white
        colorSlider.addTarget(self, action: #selector(changedColor(slider:)), for: .valueChanged)
        return colorSlider
    }()
    
    var changedColorComplete:((UIColor)->Void)?
    
    var changedModelComplete:((TSLightItemModel)->Void)?
    
    lazy var shapePickerView: TSLightShapePickerView = {
        let shapePickerView = TSLightShapePickerView()
        shapePickerView.dataArray = kLightShared.shapes
        shapePickerView.layout.sectionInset = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16)
        shapePickerView.selectedColorComplete = { [weak self] colorModel in
            guard let self = self else { return }
            changedModelComplete?(colorModel)
        }
        return shapePickerView
    }()

    override func creatUI() {

        addSubview(colorSlider)
        colorSlider.snp.makeConstraints { make in
            make.leading.equalTo(18)
            make.trailing.equalTo(-18)
            make.top.equalTo(27)
            make.height.equalTo(6)
        }
        
        addSubview(shapePickerView)
        shapePickerView.snp.makeConstraints { make in
            make.leading.equalTo(0)
            make.trailing.equalTo(0)
            make.top.equalTo(70)
            make.height.equalTo(40)
        }
        
    }
    
    @objc func changedColor(slider: ColorSlider) {
        changedColorComplete?(slider.color)
        shapePickerView.unCheck()
    }
}




class TSLightShapePickerView: TSBaseView {
    
    var dataArray:[TSLightItemModel] = [TSLightItemModel](){
        didSet{
            collectionView.reloadData()
        }
    }
    var selectedColorComplete:((TSLightItemModel)->Void)?
    
    lazy var layout: UICollectionViewFlowLayout = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        layout.itemSize = CGSize(width: 40, height: 40)
        layout.minimumInteritemSpacing = 0.0
        layout.minimumLineSpacing = 16.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(TSLightShapePickerCell.self, forCellWithReuseIdentifier: TSLightShapePickerCell.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()
        }
        
        kDelayMainShort {
            self.collectionView.selectItem(at: self.currentIndexPath, animated: false, scrollPosition: .right)
        }
    }
    
    func unCheck(){
        collectionView.deselectItem(at: currentIndexPath, animated: true)
    }
}


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

    override var isSelected: Bool{
        didSet{
            colorView.borderColor = isSelected ? .themeColor : .clear
        }
    }
    
    var colorModel:TSLightItemModel = TSLightItemModel(){
        didSet{
            shapeImageView.image = UIImage(named: colorModel.image)
        }
    }
    
    
    lazy var bgColorView: UIView = {
        let bgColorView = UIView()
        bgColorView.cornerRadius = self.bounds.size.width/2
        bgColorView.backgroundColor = .fromHex("FFFFFF", alpha: 0.1)
        return bgColorView
    }()
    
    lazy var shapeImageView: UIImageView = {
        let imageView = UIImageView.createImageView(imageName: "")
        return imageView
    }()

    lazy var colorView: UIView = {
        let colorView = UIView()
        colorView.cornerRadius = self.bounds.size.width/2
        colorView.borderColor = .clear
        colorView.borderWidth = 2
        return colorView
    }()
    
    override func creatUI() {
        
        bgContentView.addSubview(bgColorView)
        bgColorView.snp.makeConstraints { make in
            make.width.height.equalTo(40.0)
            make.center.equalToSuperview()
        }
        
        bgContentView.addSubview(shapeImageView)
        shapeImageView.snp.makeConstraints { make in
            make.width.height.equalTo(20.0)
            make.center.equalToSuperview()
        }

        bgContentView.addSubview(colorView)
        colorView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        
        

        
    }
}
