//
//  SplashViewController.swift
//  Product Calculator
//
//  Created by Suraj Kumar Mandal on 09/11/21.
//

import UIKit
import SwiftyGif
import GoogleSignIn

class SplashViewController: UIViewController {

    @IBOutlet var gifLoaderImageView: UIImageView!
        
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.gifLoaderImageView.delegate = self
    }
    
    override func viewWillAppear(_ animated: Bool) {
        setupUI()
    }
    
    func setupUI() {
        let gif = try! UIImage(gifName: "gif_splash.gif")
        self.gifLoaderImageView.setGifImage(gif, loopCount: 1)
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

extension SplashViewController : SwiftyGifDelegate {
    func gifDidStop(sender: UIImageView) {
        GIDSignIn.sharedInstance.restorePreviousSignIn { user, error in
            if error != nil || user == nil {
                // Show the app's signed-out state.
                DispatchQueue.main.async {
                    let loginVC = self.storyboard?.instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
                    self.navigationController?.pushViewController(loginVC, animated: true)
                }
            } else {
                // Show the app's signed-in state.
                DispatchQueue.main.async {
                    let loginVC = self.storyboard?.instantiateViewController(withIdentifier: "DashboardViewController") as! DashboardViewController
                    self.navigationController?.pushViewController(loginVC, animated: true)
                }
            }
        }
    }
}
