//
//  ForgotPasswordViewController.swift
//  Kalker

import UIKit
import Alamofire
import SwiftyJSON

class ForgotPasswordViewController: UIViewController {

    @IBOutlet weak var emailTextField: UITextField!
    @IBOutlet weak var forgotButton: 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)
    }

    @IBAction func resetPassword(_ sender: UIButton) {
        clickAnimation(button: forgotButton)
        
        if emailTextField.text == "" {
            let alert = setAlert(title: "Kalker", message: "Wypełnij wymagane pola")
            self.present(alert, animated: true, completion: nil)
        }
        else {
            let parameters: Parameters = ["email": emailTextField.text!]
            
            Alamofire.request("https://kalkerapp.pl/api/reset.php", method: .post, parameters: parameters)
                .responseJSON { response in
                    if let jsonData = response.result.value {
                        let json = JSON(jsonData)
                        if json[0]["id"] == 0 {
                            let alert = setAlert(title: "Kalker", message: "Brak użytkownika w bazie")
                            self.present(alert, animated: true, completion: nil)
                        }
                        else {
                            let alert = setAlert(title: "Kalker", message: "Dalsze instrukcje dotyczące resetowania hasła zostały wysłane na podany adres e-mail")
                            self.present(alert, animated: true, completion: nil)
                        }
                    }
            }
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func viewDidLayoutSubviews() {
        forgotButton.setWhiteButton()
        emailTextField.setDefaultTextField()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        setTransparentNavigationBar(navBar: self.navigationController!)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        deregisterKeyboardEvents()
    }
}
