//
//  SpeakEleGuideViewCtr.swift
//  SpeakEasyLearnEnglish
//
//  Created by edy on 2025/7/11.
//

import UIKit

class SpeakEleGuideViewCtr: SpeakEleBaseViewCtr ,UIScrollViewDelegate {

    // MARK: - ib
    @IBOutlet weak var SpeakScroll: UIScrollView!
    
    private var selectIdx:Double = 0 {
        didSet {
            UIView.animate(withDuration: 0.35) {
                self.SpeakScroll.contentOffset = CGPointMake(self.selectIdx * self.SpeakScroll.width, 0)
            }
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        SpeakScroll.delegate = self
        selectIdx = 0
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        titleView.isHidden = true
        self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
    }

    @IBAction func SpeakNextTaps(_ sender: Any) {
        if selectIdx >= 2 {
            if let isThree = UserDefaults.standard.object(forKey: UnsafeRawUserDefaultsKey.UnsafeThreeDayGuide.rawValue) as? Bool {
                if isThree {
                    if IAPViewModel.share.isSubscribed {
                        let QAndA = SpeakEleQAViewCtr()
                        self.navigationController?.pushViewController(QAndA, animated: true)
                    }else{
                        SpeakElePublicManager.share.PublicData.state = .subscribe
                        let iap = SpeakEleIAPViewCtr()
                        iap.state = .guide
                        self.navigationController?.AnimationState = .present
                        self.navigationController?.pushViewController(iap, animated: true)
                    }
                }
            }else {
                UserDefaults.standard.set(true, forKey: UnsafeRawUserDefaultsKey.UnsafeThreeDayGuide.rawValue)
                let threeFree = SpeakEleThrDaysFreeViewCtr()
                self.navigationController?.pushViewController(threeFree, animated: true)
            }
        }else{
            selectIdx += 1
        }
    }
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        selectIdx = scrollView.contentOffset.x / scrollView.width
    }
}
