//
//  TSRandomWallpaperBrowseSelectView.swift
//  TSLiveWallpaper
//
//  Created by 100Years on 2024/12/24.
//


class TSRandomWallpaperBrowseSelectView : TSBaseView{
    
    var selectedCallBack:(([IndexPath])->Void)?
    
    var items:[TSImageDataItemModel] = [TSImageDataItemModel](){
        didSet{
            collectionView.reloadData()
        }
    }
    
    lazy var collectionView: UICollectionView = {
        let collectionView = UICollectionView.createCommon(delegate: self, cellReuseIds: ["TSRandomWallpaperBrowseSelectCell"])
        if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
            flowLayout.minimumInteritemSpacing = 16
            flowLayout.itemSize = CGSize(width: 100, height: 100)
            flowLayout.scrollDirection = .horizontal
        }
        collectionView.allowsMultipleSelection = true
        collectionView.contentInset = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16)
        return collectionView
    }()
    
    lazy var selectNumLabel: UILabel = {
        return UILabel.createLabel(font: UIFont.font(size: 14),textColor: UIColor.fromHex("FFFFFF", alpha: 0.4))
    }()
    
    override func creatUI() {
        contentView.addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }

        kDelayOnMainThread(0.1) {
            self.selectAllItems()
        }
      
    }
    
    
    func selectAllItems() {
        for section in 0..<collectionView.numberOfSections {
            for item in 0..<collectionView.numberOfItems(inSection: section) {
                let indexPath = IndexPath(item: item, section: section)
                collectionView.selectItem(at: indexPath, animated: false, scrollPosition: [])
            }
        }
    }
    
    func deselectAllItems() {
        for section in 0..<collectionView.numberOfSections {
            for item in 0..<collectionView.numberOfItems(inSection: section) {
                let indexPath = IndexPath(item: item, section: section)
                collectionView.deselectItem(at: indexPath, animated: false)
            }
        }
    }
    
    func clickSelectHandle(){
        if let selectedItems = collectionView.indexPathsForSelectedItems {
            if selectedItems.count == 0 || selectedItems.count < items.count {
                selectAllItems()
            }else if selectedItems.count == items.count {
                deselectAllItems()
            }
            
            selectedCallBack?(collectionView.indexPathsForSelectedItems ?? [IndexPath]())
        }
    }
}


extension TSRandomWallpaperBrowseSelectView : UICollectionViewDataSource,UICollectionViewDelegate {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return items.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TSRandomWallpaperBrowseSelectCell", for: indexPath) as! TSRandomWallpaperBrowseSelectCell
        if let wallpaperModel = items.safeObj(At: indexPath.item), wallpaperModel.imageUrl.count > 0 {
            cell.showImageView.setAsyncImage(urlString: wallpaperModel.imageUrl,placeholder: kWapppaperPlaceholderImage)
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){
        selectedCallBack?(collectionView.indexPathsForSelectedItems ?? [IndexPath]())
    }
    
    func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath){
        selectedCallBack?(collectionView.indexPathsForSelectedItems ?? [IndexPath]())
    }
}

class TSRandomWallpaperBrowseSelectCell : TSBaseCollectionCell {
    
    override var isSelected: Bool{
        didSet{
            selectImageView.image = UIImage(named: isSelected ? "radiobox_selected" : "radiobox_unSelect" )
        }
    }
    
    lazy var showImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFill
        return imageView
    }()
    
    
    lazy var selectImageView: UIImageView = {
        return UIImageView.createImageView(imageName: "radiobox_selected")
    }()
    

    override func creatUI() {

        bgContentView.cornerRadius = 16
        
        bgContentView.addSubview(showImageView)
        showImageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }

        bgContentView.addSubview(selectImageView)
        selectImageView.snp.makeConstraints { make in
            make.width.height.equalTo(24)
            make.trailing.equalTo(-4)
            make.bottom.equalTo(-4)
        }
    }
    
}
