package org.academiadecodigo.enuminatti.fx.calculator;

import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

/**
 * MIT License
 * (c) 2017 Ricardo Constantino
 */

public class CalculatorUI {
    private final int WIDTH = 300;
    private final int BUTTONS_PER_ROW = 4;
    private final int HEIGHT = 300 / BUTTONS_PER_ROW * 5;
    private final int BUTTONSIZE = WIDTH / BUTTONS_PER_ROW;

    private TextField display;
    private TextField operation;
    private GridPane grid;
    private Scene scene;

    public CalculatorUI() {
        grid = new GridPane();
        grid.setGridLinesVisible(true);
        grid.setPrefSize(WIDTH, HEIGHT);
        grid.getStylesheets().add("/calculator.css");
        grid.getStyleClass().add("root");

        addDisplay(grid);
        addOperationDisplay(grid);

        scene = new Scene(grid);
    }

    public void start(Stage primaryStage) {
        primaryStage.setScene(scene);
        primaryStage.setTitle("Calculator");
        primaryStage.show();
    }

    public void setHandler(Controller controller) {
        addButtons(grid, controller);
        scene.setOnKeyPressed(controller);
    }

    private void addOperationDisplay(GridPane grid) {
        operation = new TextField("");
        operation.setDisable(true);
        display.setPrefSize(BUTTONSIZE * BUTTONS_PER_ROW, BUTTONSIZE);
        operation.setId("operation");
        operation.getStyleClass().add("display");
        System.out.println(operation.getStyleClass());

        grid.add(operation, 0, 0, BUTTONS_PER_ROW, 1);
    }

    private void addDisplay(GridPane grid) {
        display = new TextField("0");
        display.setPrefSize(BUTTONSIZE * BUTTONS_PER_ROW, BUTTONSIZE);

        display.setDisable(true);
        display.setId("display");
        display.getStyleClass().add("display");
        System.out.println(display.getStyleClass());


        grid.add(display, 0, 0, BUTTONS_PER_ROW, 1);
    }

    private void addButtons(GridPane grid, Controller controller) {
        String[] buttonsText = new String[]{
                "7", "8", "9", "+",
                "4", "5", "6", "-",
                "1", "2", "3", "x",
                "C", "0", "=", "/"
        };
        for (int i = 0; i < buttonsText.length; i++) {
            Button button = new Button(buttonsText[i]);
            button.setId(buttonsText[i]);
            button.setFocusTraversable(false);
            button.setMinSize(BUTTONSIZE, BUTTONSIZE);

            Operation op = null;
            if ((op = Operation.getOperation(buttonsText[i])) != null) {
                button.getStyleClass().add("operation");
                if (op.isMathOperation()) {
                    button.getStyleClass().add("mathoperation");
                }
            }

            if (buttonsText[i].equals("=")) {
                button.setDefaultButton(true);
            }
            if (buttonsText[i].equals("C")) {
                button.setCancelButton(true);
            }

            button.setOnMouseClicked(controller);

            grid.add(button, i % BUTTONS_PER_ROW, i / BUTTONS_PER_ROW + 1);
        }

    }

    public void setDisplayText(String value) {
        if (value == null) {
            value = "0";
        }
        switch (value) {
            case "666":
                display.setText("\uD83E\uDD18");
                display.getStyleClass().add("emoji");
                return;
            case "69":
                display.setText("\uD83C\uDF46\uD83D\uDC4C");
                display.getStyleClass().add("emoji");
                return;
            default:
                display.setText(value);
                display.getStyleClass().remove("emoji");
        }
    }

    public void appendDisplayText(String valueToAppend) {
        if (display.getText().equals("0")) {
            setDisplayText(valueToAppend);
            return;
        }
        display.appendText(valueToAppend);
    }

    public String getDisplayText() {
        return display.getText();
    }

    public void setOperation(String operation) {
        this.operation.setText(operation != null ? operation : "");
    }
}
