//
//  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 let viewModel = SpeakUserNetViewModel()
    private let viewModelQA = SpeakEleQAViewModel()
    
    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()
        if SpeakElePublicManager.share.PublicData.isLogin() {
            let name = SpeakElePublicManager.share.PublicData.userInfo.userName ?? ""
            let lanaguage = SpeakElePublicManager.share.PublicData.userInfo.nativeLanguage ?? ""
            let email = SpeakElePublicManager.share.PublicData.userInfo.email ?? ""
            let level = SpeakElePublicManager.share.PublicData.MProficiency
            var iv = 1
            for (i,data) in viewModelQA.proficiency.enumerated() {
                if data == level {
                    iv = i+1
                    break
                }
            }
            viewModel.update(param: [
                "userName" : name,
                "nativeLanguage": lanaguage,
                "email": email,
                "targetLevel": iv
            ]) { su in
                
            }
        }
    }

    @IBAction func SpeakCountiueTaps(_ sender: Any) {
        if !IAPViewModel.share.isSubscribed {
            let iap = SpeakEleIAPViewCtr()
            iap.state = .plan
            self.navigationController?.AnimationState = .present
            self.navigationController?.pushViewController(iap, animated: true)
        }else if !SpeakElePublicManager.share.PublicData.isLogin() {
            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
        }
    }
}
