//
//  RegisterStepOneViewController.swift
//  Kalker

import UIKit

class RegisterStepOneViewController: UIViewController {

    @IBOutlet weak var nameTextField: UITextField!
    @IBOutlet weak var emailTextField: UITextField!
    @IBOutlet weak var passwordTextField: UITextField!
    
    @IBOutlet weak var caregiverRegisterButton: UIButton!
    @IBOutlet weak var patientRegisterButton: UIButton!
    
    // Pokaż/Ukryj klawiaturę podczas edycji
    @objc func keyboardWillShow(notification: NSNotification) {
        let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
        if self.view.frame.origin.y == 0 {
            self.view.frame.origin.y -= (keyboardSize?.height)!
        }
    }
    @objc func keyboardWillHide(notification: NSNotification) {
        self.view.frame.origin.y = 0
    }
    func registerKeyboardEvents() {
        NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
    }
    func deregisterKeyboardEvents() {
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: self.view.window)
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: self.view.window)
    }
    // End
    
    override func viewDidLoad() {
        super.viewDidLoad()
        registerKeyboardEvents()
        self.hideKeyboardWhenTappedAround()

        setDefaultBackground(viewForBackground: self.view)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func viewDidLayoutSubviews() {
        caregiverRegisterButton.setWhiteButton()
        patientRegisterButton.setWhiteButton()
        
        nameTextField.setDefaultTextField()
        emailTextField.setDefaultTextField()
        passwordTextField.setDefaultTextField()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        setTransparentNavigationBar(navBar: self.navigationController!)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        deregisterKeyboardEvents()
    }
    
    @IBAction func registerCaregiver(_ sender: UIButton) {
        clickAnimation(button: caregiverRegisterButton)
        
        if nameTextField.text == "" || emailTextField.text == "" || passwordTextField.text == "" {
            let alert = setAlert(title: "Kalker", message: "Wypełnij wymagane pola")
            self.present(alert, animated: true, completion: nil)
        }
        else {
            if isValidEmail(email: emailTextField.text!) {
                performSegue(withIdentifier: "caregiverRegister", sender: nil)
            }
            else {
                let alert = setAlert(title: "Kalker", message: "Format adresu e-mail jest niepoprawny")
                self.present(alert, animated: true, completion: nil)
            }
        }
    }
    
    @IBAction func registerPatient(_ sender: UIButton) {
        clickAnimation(button: patientRegisterButton)
        
        if nameTextField.text == "" || emailTextField.text == "" || passwordTextField.text == "" {
            let alert = setAlert(title: "Kalker", message: "Wypełnij wymagane pola")
            self.present(alert, animated: true, completion: nil)
        }
        else {
            if isValidEmail(email: emailTextField.text!) {
                performSegue(withIdentifier: "patientRegister", sender: nil)
            }
            else {
                let alert = setAlert(title: "Kalker", message: "Format adresu e-mail jest niepoprawny")
                self.present(alert, animated: true, completion: nil)
            }
        }
    }
    
    // MARK: - Navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let nextScene = segue.destination as! RegisterStepTwoViewController
        
        if segue.identifier == "patientRegister"  {
            print("patient")
            nextScene.typeAccount = 1
            nextScene.name = nameTextField.text!
            nextScene.email = emailTextField.text!
            nextScene.password = passwordTextField.text!
            
        }
        
        if segue.identifier == "caregiverRegister" {
            print("caregiver")
            nextScene.typeAccount = 2
            nextScene.name = nameTextField.text!
            nextScene.email = emailTextField.text!
            nextScene.password = passwordTextField.text!
        }
        
    }
 

}
