//
//  SpeakElePracticeReviewViewCtr.swift
//  SpeakEasyLearnEnglish
//
//  Created by edy on 2025/7/23.
//

import UIKit

class SpeakElePracticeReviewViewCtr: SpeakEleBaseViewCtr {

    @IBOutlet weak var SpeakNextBtn: UIButton!
    @IBOutlet weak var SpeakCollectionV: UICollectionView!
    
    @IBOutlet var SpeakItemBtns: [UIButton]!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "1 / \(datasource.count)"
        self.rightImage = "icon_wordbook"
        self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
    }

    override func setup() {
        SpeakCollectionV.delegate = self
        SpeakCollectionV.dataSource = self
        SpeakCollectionV.isScrollEnabled = false
        SpeakCollectionV.register(UINib(nibName: SpeakPracticeReviewCell.id, bundle: nil), forCellWithReuseIdentifier: SpeakPracticeReviewCell.id)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.view.bringSubviewToFront(SpeakCollectionV)
    }
    
    override func rightTaps() {
        let vc = SpeakPracticeWordsListViewCtr()
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    @IBAction func SpeakItemTaps(_ sender: UIButton) {
        
    }
    
    @IBAction func SpeakNextTaps(_ sender: Any) {
        guard let cell = SpeakCollectionV.cellForItem(at: IndexPath(row: 0, section: 0)) else { return }
        performFlipAnimation(fromLeft: cell)
    }
    
    private var isAnimating = false
    
    private func performFlipAnimation(fromLeft: UIView) {
        if isAnimating == false {
            isAnimating = true
            fromLeft.layer.isDoubleSided = false
            UIView.animateKeyframes(withDuration: 0.8, delay: 0, options: [], animations: {
                UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 0.2) {
                    fromLeft.layer.transform = CATransform3DRotate(
                        CATransform3DIdentity,
                        CGFloat(-Double.pi/2),
                        0, 0, 0
                    )
                }
            }) { [weak self] _ in
                let st = self?.datasource.first ?? ""
                self?.datasource.remove(at: 0)
                self?.datasource.append(st)
                self?.SpeakCollectionV.reloadData()
                self?.isAnimating = false
            }
        }
    }
    
    
    var datasource:[String] = ["Serendipity",
                               "Ephemeral",
                               "Quintessential",
                               "Resilience",
                               "Whimsical",
                               "Pernicious",
                               "Luminous",
                               "Cacophony"]
}

extension SpeakElePracticeReviewViewCtr : UICollectionViewDelegate,UICollectionViewDataSource , UICollectionViewDelegateFlowLayout{
    
    private func StrangeFamilar(state:SpeakPracticeReviewCell.SpeakPracticeReviewState) -> Void {
        if state == .left {
            
        }else{
            
        }
        self.datasource.remove(at: 0)
        self.title = "1 / \(datasource.count)"
        if self.datasource.count <= 0 {
            self.navigationController?.popViewController(animated: true)
        }else{
            self.SpeakCollectionV.deleteItems(at: [IndexPath(row: 0, section: 0)])
            self.SpeakCollectionV.reloadData()
        }
        self.SpeakItemBtns.forEach({$0.isSelected = false})
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return datasource.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: SpeakPracticeReviewCell.id, for: indexPath) as! SpeakPracticeReviewCell
        let r = Double(arc4random() % UInt32(255.0))
        cell.backgroundColor = UIColor(red: 0.4, green: 0.5, blue: r/255.0, alpha: 1)
        cell.data = datasource[indexPath.row]
        cell.SpeakCallblack = { [weak self] state in
            self?.StrangeFamilar(state: state)
        }
        cell.callblack = { [weak self] state in
            if let st:SpeakPracticeReviewCell.SpeakPracticeReviewState = state as? SpeakPracticeReviewCell.SpeakPracticeReviewState {
                if st == .left {
                    self?.SpeakItemBtns[0].isSelected = true
                    self?.SpeakItemBtns[1].isSelected = false
                }else if st == .right{
                    self?.SpeakItemBtns[0].isSelected = false
                    self?.SpeakItemBtns[1].isSelected = true
                }else{
                    self?.SpeakItemBtns.forEach({$0.isSelected = false})
                }
            }
        }
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return collectionView.size
    }
}
