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

import UIKit
import Lottie

class OnBoardVC: UIViewController {
    
    @IBOutlet var viewLottie: UIView!
    @IBOutlet weak var wokaLogoTopConstriant: NSLayoutConstraint!
    @IBOutlet weak var carouselCV: UICollectionView!
    @IBOutlet weak var pageControl: UIPageControl!
    @IBOutlet weak var createAccountBtn: UIButton!
    @IBOutlet weak var loginBtn: UIButton!
    @IBOutlet weak var loginAsGuestBtn: UIButton!
    @IBOutlet weak var mainStack: UIStackView!
    
    var topConstriantFromSplash = Float()
    var vm = OnBoardVM()
    
    // MARK: - LifeCycle

    deinit{
        AuthFunc.shareInstance.stopSound()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        vm.vc = self
        AuthFunc.shareInstance.playStartUpsound()
        vm.initData()
    }
    
    @IBAction func pageControl(_ sender: UIPageControl) {
        self.carouselCV.scrollToItem(at: IndexPath(row: sender.currentPage, section: 0), at: .centeredHorizontally, animated: true)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.setNavigationBarHidden(false, animated: animated)
        vm.resumeBackGroundAnimationJSON()
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        navigationController?.setNavigationBarHidden(true, animated: animated)
        vm.stopBackGroundAnimationJSON()
        
        if self.isMovingFromParent {
            // Back button was pressed
            PersistentStorage.shared.addOthersCount()
        }
    }
    
    // MARK: - Button Tap Handler
    
    @IBAction func createAccountBtnTapped(_ sender: UIButton) {
        PersistentStorage.shared.addOthersCount()
        
        let sb = UIStoryboard(name: K.StoryBoard.main, bundle: nil)
        let vc = sb.instantiateViewController(withIdentifier: K.StoryBoardID.OnBoarding.selectAgeVC) as! SelectAgeVC
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    @IBAction func loginBtnTapped(_ sender: UIButton) {
        PersistentStorage.shared.addOthersCount()
        
        let sb = UIStoryboard(name: K.StoryBoard.authenticationSB, bundle: nil)
        let vc = sb.instantiateViewController(withIdentifier: K.StoryBoardID.Authentication.loginVC) as! LoginVC
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    
    @IBAction func guestLoginBtnTapped(_ sender: UIButton) {
        PersistentStorage.shared.addOthersCount()
        vm.guestLogin()
    }
    
    
    // MARK: - Collection Scroll Handling

    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let visibleRect = CGRect(origin: self.carouselCV.contentOffset, size: self.carouselCV.bounds.size)
        let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)
        if let visibleIndexPath = self.carouselCV.indexPathForItem(at: visiblePoint) {
            self.pageControl.currentPage = visibleIndexPath.row
        }
    }
}


// MARK: - Collection delegate and datasource

extension OnBoardVC : UICollectionViewDelegate,UICollectionViewDataSource{
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return vm.data?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: K.CellIdentifier.OnBoarding.onBoardCell, for: indexPath) as! OnBoardCell
        if let data = vm.data?[indexPath.row]{
            cell.setData(data: data)
        }
        return cell
    }
    
}

// MARK: - Collection Flow Layout

extension OnBoardVC : UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 10
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let frameSize = collectionView.frame.size
        return CGSize(width: frameSize.width - 10, height: frameSize.height)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)
    }
}
