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

import UIKit
import GoogleSignIn

class LoginViewController: UIViewController {
        
    @IBOutlet var googleSignInButton: UIButton!
    //@IBOutlet var appleSignInButton: UIButton!
    
    let signInConfig = GIDConfiguration.init(clientID: AppConstant.GOOGLE_CLIENT_ID)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        // Hide the Navigation Bar
        self.navigationController?.setNavigationBarHidden(true, animated: animated)
        setupUI()
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        // Show the Navigation Bar
        self.navigationController?.setNavigationBarHidden(false, animated: animated)
    }
    
    override func willMove(toParent parent: UIViewController?) {
        super.willMove(toParent: parent)
        if parent == nil {
            UIApplication.shared.perform(#selector(NSXPCConnection.suspend))
        }
    }
    
    func setupUI() {
        googleSignInButton.layer.cornerRadius = 5
        //appleSignInButton.layer.cornerRadius = 5
    }
    
    
    /*
     // 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.
     }
     */
    
    @IBAction func googleSignInAction(_ sender: Any) {
        print("Login Button Tapped")
        GIDSignIn.sharedInstance.signIn(with: signInConfig, presenting: self) { user, error in
            guard error == nil else { return }
//            guard let user = user else { return }

//            let emailAddress = user.profile?.email
//            let fullName = user.profile?.name
//            let givenName = user.profile?.givenName
//            let familyName = user.profile?.familyName
//            let profilePicUrl = user.profile?.imageURL(withDimension: 320)
            
            // If sign in succeeded, display the app's main content View.
            DispatchQueue.main.async {
                let loginVC = self.storyboard?.instantiateViewController(withIdentifier: "DashboardViewController") as! DashboardViewController
                self.navigationController?.pushViewController(loginVC, animated: true)
            }
        }
    }
    
}
