//
//  WA_NewHometypeCell.swift
//  wallpaper_project
//
//  Created by 忆海16 on 2024/3/28.
//

import UIKit

class WA_NewHometypeCell: UITableViewCell {
    @IBOutlet weak var collectionView: UICollectionView!
    let cellWidth: CGFloat = 122
       let cellHeight: CGFloat = 134
       let cellSpacing: CGFloat = 10
    
    let dataname = ["Live", "4k", "Newest", "Popular", "Superheroes","Christmas","Flowers"]
    
    let datacolor = ["blue", "systemPink", "purple", "black", "red","orange","green"]
    
    let dataimag = ["live", "4k", "new", "pop", "sup","chr","flower"]
    
    let dataimaghero = ["live", "4k", "new", "pop","chr","flower"]
    
    var liveBlock:(()->())?
    var jpgblock:((Int)->())?
    override func awakeFromNib() {
        super.awakeFromNib()
        setcollectionV()
        
    }
    
    func setcollectionV(){
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.collectionViewLayout = layout
        collectionView.register(UINib(nibName: "WA_NHTypeCollectCell", bundle: nil), forCellWithReuseIdentifier: "WA_NHTypeCollectCell")

    }

    
    
}
extension WA_NewHometypeCell: UICollectionViewDelegateFlowLayout,UICollectionViewDataSource,UICollectionViewDelegate{
    
     func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
         if StartManager.shared.superHero == true{
             return dataimag.count
         }else{
             return dataimaghero.count
         }
        
    }
    
     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
         
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "WA_NHTypeCollectCell", for: indexPath) as! WA_NHTypeCollectCell
//         cell.typename.text = self.dataname[indexPath.row]
//         cell.bgView.backgroundColor = getColorFromString(datacolor[indexPath.item])
         if StartManager.shared.superHero == true{
//             return dataimag.count
             cell.typeImagV.image = UIImage(named: "\(self.dataimag[indexPath.row])")
         }else{
//             return dataimaghero.count
             cell.typeImagV.image = UIImage(named: "\(self.dataimaghero[indexPath.row])")
         }
       
        return cell
         
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
          return CGSize(width: cellWidth, height: cellHeight)
      }
      
      func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
          return cellSpacing
      }
      
      func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
          return UIEdgeInsets(top: 0, left: cellSpacing, bottom: 0, right: cellSpacing)
      }
    func getColorFromString(_ colorString: String) -> UIColor {
         if colorString == "systemPink" {
             return UIColor.systemPink
         } else if colorString == "blue" {
             return UIColor.blue
         } else if colorString == "purple" {
             return UIColor.purple
         }else if colorString == "red" {
             return UIColor.red
         }else if colorString == "black" {
             return UIColor.black
         }else if colorString == "orange" {
             return UIColor.orange
         }else if colorString == "green" {
             return UIColor.green
         }else {
             return UIColor(named: colorString) ?? UIColor.black
         }
     }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if indexPath.row == 0{
            if self.liveBlock != nil{
                liveBlock!()
            }
        }else{
            if self.jpgblock != nil{
                jpgblock!(indexPath.row)
            }
        }
    }
    
}
