package org.academiadecodigo.enuminatti.cadetregistry.controllers;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import org.academiadecodigo.enuminatti.cadetregistry.models.User;
import org.academiadecodigo.enuminatti.cadetregistry.services.UserService;

public class LoginController {

    private UserService userService;
    private User loggedUser;

    @FXML // ResourceBundle that was given to the FXMLLoader
    private ResourceBundle resources;

    @FXML // URL location of the FXML file that was given to the FXMLLoader
    private URL location;

    @FXML // fx:id="emailLabel"
    private Label emailLabel;

    @FXML // fx:id="userField"
    private TextField userField; // Value injected by FXMLLoader

    @FXML // fx:id="passField"
    private PasswordField passField; // Value injected by FXMLLoader

    @FXML // fx:id="emailField"
    private TextField emailField; // Value injected by FXMLLoader

    @FXML // fx:id="loginButton"
    private Button loginButton; // Value injected by FXMLLoader

    @FXML // fx:id="registerButton"
    private Button registerButton; // Value injected by FXMLLoader

    @FXML // fx:id="switchFields"
    private Hyperlink switchFields; // Value injected by FXMLLoader

    @FXML // fx:id="consoleLog"
    private Label consoleLog; // Value injected by FXMLLoader

    @FXML
    void onLogin(ActionEvent event) {
        if (loggedUser != null) {
            setErrorText("User already logged in.");
            return;
        }

        if (userService.authenticate(userField.getText(), passField.getText())) {
            loggedUser = userService.findByName(userField.getText());
        }

        resetFields();
        setErrorText(userService.getErrorStatus());

    }

    @FXML
    void onRegister(ActionEvent event) {
        String username = userField.getText();
        if (userService.findByName(username) == null) {
            User newUser = new User(username, emailField.getText(), passField.getText());
            userService.addUser(newUser);
            setRegisterFieldsVisibility(false);
            setErrorText(userService.getErrorStatus());
            return;
        }

        setErrorText("Username already registered.");
    }

    @FXML
    void showRegister(ActionEvent event) {
        setRegisterFieldsVisibility(true);
    }

    @FXML
        // This method is called by the FXMLLoader when initialization is complete
    void initialize() {
        resetFields();

        setRegisterFieldsVisibility(false);
    }

    private void setRegisterFieldsVisibility(boolean show) {
        emailField.setVisible(show);
        emailLabel.setVisible(show);
        if (!show) {
            emailField.setMaxHeight(0);
            emailLabel.setMaxHeight(0);
        } else {
            emailField.setMaxHeight(emailField.getPrefHeight());
            emailLabel.setMaxHeight(emailLabel.getPrefHeight());
        }
        registerButton.setVisible(show);
        registerButton.setDefaultButton(show);

        switchFields.setVisible(!show);
        loginButton.setDefaultButton(!show);
    }

    private void resetFields() {
        userField.setText("");
        passField.setText("");
        emailField.setText("");
        consoleLog.setText("");
        userField.requestFocus();
    }

    private void setErrorText(String message) {
        this.consoleLog.setText("console.log(\"" + message + "\");");
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}

