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

import UIKit

class SpeakElePracticeViewCtr: SpeakEleBaseViewCtr {

    // MARK: - ib

    @IBOutlet weak var SpeakSectionL: UILabel!
    @IBOutlet weak var SpeakProgressL: UILabel!
    @IBOutlet weak var SpeakCollection: UICollectionView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func setup() {
        SpeakCollection.delegate = self
        SpeakCollection.dataSource = self
        SpeakCollection.isPagingEnabled = true
        SpeakCollection.register(UINib(nibName: SpeakElePracticeViewCell.id, bundle: nil), forCellWithReuseIdentifier: SpeakElePracticeViewCell.id)
        SpeakProgressL.contentInsets = UIEdgeInsets(top: 5, left: 10, bottom: 5, right: 10)
        
        dataSource = ["" , "" , ""]
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
    }

    @IBAction func SpeakSelectTaps(_ sender: Any) {
        
    }
    
    @IBAction func SpeakReviewTaps(_ sender: Any) {
        let review = SpeakElePracticeReviewViewCtr()
        self.parent?.navigationController?.pushViewController(review, animated: true)
    }
    
    private var dataSource:[String] = ["",""] {
        didSet{
            SpeakCollection.contentOffset = CGPointZero
            setPage(current: 1)
        }
    }
}

extension SpeakElePracticeViewCtr :UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
 
    private func setPage( current:Int) -> Void {
        SpeakProgressL.attributedText = "\(current)".attributed().color(.init(hex: 0x000000,alpha: 0.9)).build().combined(with: [" / \(dataSource.count)".attributed().color(.init(hex: 0x000000 ,alpha: 0.26)).build()])
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataSource.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: SpeakElePracticeViewCell.id, for: indexPath) as! SpeakElePracticeViewCell
        let id = (arc4random() % 255)
        cell.backgroundColor = UIColor(red: 0.4, green: CGFloat(id)/255.0, blue: 0.5, alpha: 1)
        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 {
        return collectionView.size
    }
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let point = scrollView.contentOffset
        self.setPage(current: ( Int(round(point.x/scrollView.width)) + 1) )
    }
    
}
