//
//  SplashVC.swift
//  WOKA
//
//  Created by MacBook Pro on 25/04/24.
//

import UIKit
import Lottie

class SplashVC: UIViewController {
    
    @IBOutlet weak var wokaLogo: UIImageView!
    @IBOutlet weak var hindiBtn: UIButton!
    @IBOutlet weak var englishBtn: UIButton!
    @IBOutlet weak var languageBtnStack: UIStackView!
    @IBOutlet weak var wokaOriginY: NSLayoutConstraint!
    
    var vm = SplashVM()
    
    // MARK: - View Life Cycle
    
    override func viewDidLoad() {
        super.viewDidLoad()
        vm.vc = self
        vm.playSplashSound()
        animateImageScale()
        self.navigationItem.backBarButtonItem = UIBarButtonItem(title:"", style:.plain, target:nil, action:nil)
    }
    
    @IBAction func languageBtnTapped(_ sender: UIButton) {
//        let sb1 = UIStoryboard(name: "Home", bundle: nil)
//        let vc1 = sb1.instantiateViewController(withIdentifier: "SideMenu") as! SideMenuController
//        self.navigationController?.pushViewController(vc1, animated: true)
//        
//        return
        switch sender{
        case hindiBtn:
            AuthFunc.shareInstance.languageSelected = .hindi
        case englishBtn:
            AuthFunc.shareInstance.languageSelected = .english
        default:
            AuthFunc.shareInstance.languageSelected = .english
        }
        
        let sb = UIStoryboard(name: K.StoryBoard.main, bundle: nil)
        let vc = sb.instantiateViewController(withIdentifier: K.StoryBoardID.OnBoarding.onBoardVC) as! OnBoardVC
        
        // Create a CATransition instance
        let transition = CATransition()
        transition.duration = 0.3 // Set the duration of the animation
        transition.type = CATransitionType.fade // Set the type of animation to fade

        // Optionally, set other properties such as timing function, subtype, etc.
        // transition.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
        // transition.subtype = CATransitionSubtype.fromTop

        // Get the navigation controller
        guard let navigationController = navigationController else { return }

        // Perform the push animation with the custom transition
        navigationController.view.layer.add(transition, forKey: nil)
        navigationController.pushViewController(vc, animated: false)
    }
    
    func animateImageScale() {
        UIView.animate(withDuration: 3.0, delay: 0, options: [], animations: {
            self.wokaLogo.transform = CGAffineTransform(scaleX: 1.8, y: 1.8)
        }, completion: nil)
        
        let newConstant = wokaOriginY.constant - 50

        Timer.scheduledTimer(withTimeInterval: 6, repeats: false) { _ in
            UIView.animate(withDuration: 0.5, animations: {
                // Update the constant value of the top constraint
                self.wokaOriginY.constant = newConstant
                // Inform the layout system to update
                self.view.layoutIfNeeded()
            }) { _ in
                UIView.animate(withDuration: 0.3, delay: 0,options : [.transitionCrossDissolve],animations: {
                    // Set the isHidden property of the view
                    self.languageBtnStack.isHidden = false
                }) {_ in
                    
                    
                    // Inform the stack view to update its layout
                    self.languageBtnStack.layoutIfNeeded()
                }
            }
        }
    }
    
    override func viewDidLayoutSubviews() {
        vm.initView()
    }
    
}



