import Foundation
import UIKit

/// sign in view controller
class SignInViewController: UIViewController {
    
    /// initialization function
    init() {
        super.init(nibName: nil, bundle: nil)
        let view = SingInView()
        view.delegate = self
        self.view = view
    }
    
    /// function that gets called when the view appears
    /// - Parameter animated: animated
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)
        navigationController?.setNavigationBarHidden(false, animated: false)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension SignInViewController: SignInViewDelegate {
    
    /// function that gets called when user logges in, it will set a token and redirect to main view
    /// - Parameter token: user token
    func successLogIn(token: String) {
        guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
          let sceneDelegate = windowScene.delegate as? SceneDelegate
        else { return }
        Auth.shared.setToken(token: token)
        let controller = UINavigationController(rootViewController: TabViewController())
        controller.setNavigationBarHidden(true, animated: false)
        sceneDelegate.window?.rootViewController = controller
    }
}
