//
//  SpeakEleGeneratePlanViewCtr.swift
//  SpeakEasyLearnEnglish
//
//  Created by edy on 2025/7/17.
//

import UIKit

class SpeakEleGeneratePlanViewCtr: SpeakEleBaseViewCtr {

    // MARK: - ib
    @IBOutlet weak var SpeakStackView: UIStackView!
    @IBOutlet weak var SpeakContinueBtn: UIButton!
    
    private var offset:Int = 0
    
    private var SpeakPlanChilds:[SpeakEleGeneratePlanView] = [
        SpeakEleGeneratePlanView(string: "Creating diverse topics"),
        SpeakEleGeneratePlanView(string: "Preparing interactive dialogues"),
        SpeakEleGeneratePlanView(string: "Optimizing your learning path"),
        SpeakEleGeneratePlanView(string: "Finalizing your plan")
    ]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
        SpeakPlanChilds.forEach { plan in
            SpeakStackView.addArrangedSubview(plan)
        }
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        titleView.isHidden = true
        start()
    }

    @IBAction func SpeakCountiueTaps(_ sender: Any) {
        //FIXME: 判断是否订阅和登录
        if true {
            let iap = SpeakEleIAPViewCtr()
            iap.state = .plan
            self.navigationController?.AnimationState = .present
            self.navigationController?.pushViewController(iap, animated: true)
        }else if SpeakElePublicManager.share.PublicData.token.count <= 0 {
            let login = SpeakEleFLoginViewCtr()
            self.navigationController?.pushViewController(login, animated: true)
        }else{
            let nav = SpeakEleBaseNavigationCtr(rootViewController: SpeakEleTabbarViewCtr())
            SpWindow.switchToController(nav)
        }
    }
    
    private func start() -> Void {
        offset = 0
        display.preferredFramesPerSecond = 10
        display.isPaused = false
    }
    
    override func SpeakUpdate() {
        offset += 3//step
        switch offset {
        case 0..<100:
            let child = SpeakPlanChilds.first
            child?.state = .start
            child?.startRotation()
            child?.offset = offset
            break
        case 100..<200:
            let child = SpeakPlanChilds[0]
            child.stop()
            child.offset = 100
            child.state = .finish
            
            let child1 = SpeakPlanChilds[1]
            child1.state = .start
            child1.startRotation()
            child1.offset = offset-100
            break
        case 200..<300:
            let child = SpeakPlanChilds[1]
            child.stop()
            child.offset = 100
            child.state = .finish
            
            let child2 = SpeakPlanChilds[2]
            child2.state = .start
            child2.startRotation()
            child2.offset = offset-200
            break
        default:
            
            let child = SpeakPlanChilds[2]
            child.stop()
            child.offset = 100
            child.state = .finish
            
            let child1 = SpeakPlanChilds[3]
            child1.state = .start
            child1.startRotation()
            child1.offset = offset-300
            if offset - 300 > 100 {
                child1.offset = 100
                child1.state = .finish
                UIView.animate(withDuration: 0.1, delay: 0.5) {
                    self.SpeakContinueBtn.alpha = 1
                }
                display.isPaused = true
            }
            break
        }
    }
}
