package sample;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {

        GridPane gridPane = new GridPane();
        gridPane.setHgap(10);
        gridPane.setVgap(20);
        gridPane.setGridLinesVisible(false);
        gridPane.setAlignment(Pos.CENTER);
        gridPane.setPadding(new Insets(10));

        Label titleLabel = new Label();
        titleLabel.setText("#ENUMinatti");
        titleLabel.setFont(Font.font("Menlo", FontWeight.BOLD, 28));
        titleLabel.setBackground(new Background(new BackgroundFill(Color.RED, new CornerRadii(10, true), new Insets(0))));

        gridPane.add(titleLabel, 0, 0, 3, 1);

        // User TextField
        TextField user = new TextField();
        gridPane.add(user, 1, 1, 2, 1);

        // PasswordField
        PasswordField pass = new PasswordField();
        gridPane.add(pass, 1, 2, 2, 1);

        // User label
        Label userLabel = new Label();
        gridPane.add(userLabel, 0, 1);
        userLabel.setText("Code cadet:");
        userLabel.setTextAlignment(TextAlignment.RIGHT);
        userLabel.setLabelFor(user);

        Label passLabel = new Label();
        gridPane.add(passLabel, 0, 2);
        passLabel.setText("Password:");
        passLabel.setTextAlignment(TextAlignment.RIGHT);
        passLabel.setLabelFor(pass);


        // Button
        Button submit = new Button("Submit");
        gridPane.add(submit, 1, 3);
        submit.setDefaultButton(true);

        Button reset = new Button("Reset");
        gridPane.add(reset, 2, 3);
        reset.setCancelButton(true);


        EventHandler<ActionEvent> showCredentials = new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.out.println(String.format("User: %s\nPassword: %s",
                        user.getText(), pass.getText()));
            }
        };

//        EventHandler<ActionEvent> clearFields = new EventHandler<ActionEvent>() {
//            @Override
//            public void handle(ActionEvent event) {
//
//            }
//        }

        submit.setOnAction(showCredentials);
//        reset.setOnAction(clearFields);

        reset.setOnAction(event -> {
            user.setText("");
            pass.setText("");
            user.requestFocus();
        });

        Scene scene = new Scene(gridPane, 310, 280);

        scene.setOnKeyPressed(event -> {
            switch (event.getCode()) {
                case ADD:
                    System.out.println("add key pressed");
                    break;
                case SUBTRACT:
                    System.out.printf("minus key pressed");
                    break;
                default:
                    System.out.println(event.getCode() + "another key pressed");
            }
        });

        primaryStage.setScene(scene);

        primaryStage.setTitle("Academia de Código");
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}
