//
//  SpeakProgressSignCell.swift
//  SpeakEasyLearnEnglish
//
//  Created by edy on 2025/7/21.
//

import UIKit

class SpeakProgressSignCell: UITableViewCell {

    static let id = "SpeakProgressSignCell"
    
    // MARK: - ib
    @IBOutlet weak var SpeakContentV: UIView!
    @IBOutlet weak var SpeakSignNumL: UILabel!
    @IBOutlet weak var SpeakCollectionView: UICollectionView!
    private let viewModel = SpeakProgressViewModel()
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.backgroundColor = .clear
        setup()
        dataSource = self.viewModel.currentWeek()
    }
    
    private func setup() -> Void {
        self.SpeakCollectionView.delegate = self
        self.SpeakCollectionView.dataSource = self
        self.SpeakCollectionView.register(UINib(nibName: SpeakProgressWeekCell.id, bundle: nil), forCellWithReuseIdentifier: SpeakProgressWeekCell.id)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        SpeakContentV.corners = 16
    }
    
    private var dataSource:[Date]?
    
}

extension SpeakProgressSignCell :UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataSource?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: SpeakProgressWeekCell.id, for: indexPath) as! SpeakProgressWeekCell
        let i = arc4random() % 3
        cell.alpha = 1
        let date = dataSource?[indexPath.row]
        cell.SpeakWeedLabel.text = date?.toWeek()
        if i == 0 {
            cell.backgroundColor = .init(hex: 0x3980F6,alpha: 0.2)
            cell.SpeakWeedLabel.textColor = .init(hex: 0x3980F6)
            cell.SpeakProgressInfo.image = UIImage(named: "sp-progress-Check")
        }else if i == 1 {
            cell.backgroundColor = .init(hex: 0x3980F6,alpha: 0.2)
            cell.SpeakWeedLabel.textColor = .init(hex: 0x3980F6)
            cell.SpeakProgressInfo.image = UIImage(named: "sp-progress-Check")
            cell.alpha = 0.2
        }else{
            cell.SpeakProgressInfo.image = UIImage(named: "icon_check_off")
            cell.backgroundColor = .init(hex: 0x000000,alpha: 0.08)
            cell.SpeakWeedLabel.textColor = .init(hex: 0x000000,alpha: 0.4)
        }
        return cell
    }
 
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 10
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets()
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let height = collectionView.height
        let width = collectionView.height * (60/80.0)
        return CGSizeMake(width, height)
    }
    
}
