//
//  SpeakProgressStudyCell.swift
//  SpeakEasyLearnEnglish
//
//  Created by edy on 2025/7/22.
//

import UIKit

class SpeakProgressStudyCell: UITableViewCell {

    static let id = "SpeakProgressStudyCell"
    
    @IBOutlet weak var SpeakSegmentView: SpeakSegmentTool!
    @IBOutlet weak var SpeakTotalL: UILabel!
    @IBOutlet weak var SpeakAverageL: UILabel!
    @IBOutlet weak var SpeakSegmentChart: UICollectionView!
    @IBOutlet weak var SpeakChartTatalB: UIButton!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        setup()
    }
    
    private func setup() -> Void {
        SpeakSegmentView.item = ["Last 3 days","This week","Last week"]
        SpeakSegmentChart.delegate = self
        SpeakSegmentChart.dataSource = self
        SpeakSegmentChart.register(UINib(nibName: SpeakProgressStudySecCell.id, bundle: nil), forCellWithReuseIdentifier: SpeakProgressStudySecCell.id)
    }
    
    var data:[String] = ["","","","","","",""] {
        didSet{
            SpeakSegmentChart.reloadData()
        }
    }

    private let colors:[UIColor] = [
        .init(hex: 0x0066FF),
        .init(hex: 0xFF7700),
        .init(hex: 0x00BD13),
        .init(hex: 0xFFB200),
        .init(hex: 0xEB3B2F),
    ]
}
 
extension SpeakProgressStudyCell : UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return data.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: SpeakProgressStudySecCell.id, for: indexPath) as! SpeakProgressStudySecCell
        cell.color = colors[safe:indexPath.row % 5] ?? .clear
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt 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 {
        var width = collectionView.width
        width /= Double(data.count)
        width = max(width, 60)
        return CGSizeMake(width, collectionView.height)
    }
}
