package de.thdeg.grademanager.gui;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.stage.Stage;

import java.io.IOException;
import java.util.Optional;

import static de.thdeg.grademanager.gui.MainController.*;

public class StudentModificationController {
    @FXML
    protected ComboBox<String> genderComboBox;
    @FXML
    protected ComboBox<String> bachelorSemesterComboBox;
    @FXML
    protected ComboBox<String> statusComboBox;
    ObservableList<String> genderList = FXCollections.observableArrayList("männlich", "weiblich", "divers");
    ObservableList<String> bachelorSemesterList = FXCollections.observableArrayList("1", "2", "3", "4", "5", "6",
                                                                                            "7", "8", "9", "10", "11",
                                                                                                "12", "13");
    ObservableList<String> statusList = FXCollections.observableArrayList("immatrikuliert", "exmatrikuliert");

    @FXML
    protected void initialize() {
        setComboBox(genderComboBox, genderList);
        setComboBox(bachelorSemesterComboBox, bachelorSemesterList);
        setComboBox(statusComboBox, statusList);
    }

    @FXML
    protected String gender;
    @FXML
    protected TextField firstName;
    @FXML
    protected TextField lastName;
    @FXML
    protected TextField placeOfResidence;
    @FXML
    protected TextField birthPlace;
    @FXML
    protected TextField officialEmail;
    @FXML
    protected TextField privateEmail;
    @FXML
    protected TextField personalCourses;
    @FXML
    protected String status;
    @FXML
    protected boolean paidFees;
    @FXML
    protected String bachelorSemester;
    @FXML
    protected TextField grades;
    @FXML
    protected TextField warning;
    @FXML
    protected RadioButton yes;
    @FXML
    protected RadioButton no;


    private Stage stage;
    private Scene scene;
    private FXMLLoader root;

    @FXML
    protected void onSaveButtonClick(ActionEvent event) throws IOException {
        if(genderComboBox.getValue() != null && !firstName.getText().isBlank() && !lastName.getText().isBlank() &&
                !placeOfResidence.getText().isBlank() && !birthPlace.getText().isBlank() &&
                !officialEmail.getText().isBlank() && !privateEmail.getText().isBlank() &&
                statusComboBox.getValue() != null && (yes.isSelected() || no.isSelected()) &&
                bachelorSemesterComboBox.getValue() != null)
        {
            gender = genderComboBox.getValue();
            paidFees = yes.isSelected();
            status = statusComboBox.getValue();
            bachelorSemester = bachelorSemesterComboBox.getValue();

            studentList.add(lastName.getText() + " " + firstName.getText());
            switchToMain(event);
        }
        else
        {
            warning.setText("Bitte alle Felder ausfüllen.");
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setTitle("Fehler");
            alert.setHeaderText("Bitte alle Felder ausfüllen.");
            Optional<ButtonType> result = alert.showAndWait();
        }
    }

    @FXML
    protected void switchToMain(ActionEvent event) throws IOException {
        root = new FXMLLoader(Main.class.getResource("main.fxml"));
        stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
        scene = new Scene(root.load(), 960, 540);
        stage.setTitle("Main");
        stage.setScene(scene);
        stage.show();
    }

    @FXML
    protected void abortAndSwitchToMain(ActionEvent event) throws IOException {
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        alert.setTitle("Bestätigung erforderlich");
        alert.setHeaderText("Daten wurden nicht gespeichert. Änderungen verwerfen?");

        ButtonType buttonTypeOne = new ButtonType("Ja");
        ButtonType buttonTypeCancel = new ButtonType("Abbrechen", ButtonBar.ButtonData.CANCEL_CLOSE);

        alert.getButtonTypes().setAll(buttonTypeOne, buttonTypeCancel);

        Optional<ButtonType> result = alert.showAndWait();
        if (result.get() == buttonTypeOne){
            switchToMain(event);
        }
    }
}