import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.text.Font;
import javafx.event.ActionEvent;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import java.util.Random;


public class MainMathGUI extends Application {
    // TODO: Instance Variables for View Components and Model
    TextField name, userAnswer;
    Label labelA, labelC, labelW, labelQuiz;
    Button submit;

    public int numRight = 0;
    public int numWrong = 0;

    Random randA = new Random();
    Random randB = new Random();
    int x = randA.nextInt(12) + 1;
    int y = randB.nextInt(12) + 1;

    // TODO: Private Event Handlers and Helper Methods
    //private void myHandler(ActionEvent e) {
    // String msg = "";
    //int n = Integer.parseInt(numFieldC.getText());
    // for (int i = 0; i < n; i++) {
    // msg += "Hello, " + name.getText() + "! ";
    // }
    // labela.setText(msg);


    public void calculate(ActionEvent event) {
        /**
         * Create an object.
         */
        MathAB multiply = new MathAB(1, 1);

            multiply.setA(x);
            multiply.setB(y);

            int user = Integer.parseInt(userAnswer.getText());
            multiply.setUserAnswer(user);


            if (multiply.getScore() == true) { numRight++; }

            else {numWrong ++;}

        }



    /**
     * This is where you create your components and the model and add event
     * handlers.
     *
     * @param stage The main stage
     * @throws Exception
     */
    @Override
    public void start(Stage stage) throws Exception {
        Pane root = new Pane();
        Scene scene = new Scene(root, 400, 300); // set the size here
        stage.setTitle("Multiplication Quiz"); // set the window title here
        stage.setScene(scene);

        // TODO: Add your GUI-building code here
        root.setStyle("-fx-background-color: lightblue;");
        // 1. Create the model
        // 2. Create the GUI components
        name = new TextField("Your Name Here");

        userAnswer = new TextField();
        userAnswer.setPrefSize(80,25);
        userAnswer.relocate(250, 165);
        userAnswer.setFont(new Font("Arial", 20));

        submit = new Button("Submit");

        labelA = new Label(" Welcome!");
        labelC = new Label ("Correct: " + numRight);
        labelW = new Label ("Wrong: " + numWrong);
        labelQuiz = new Label ("Question: " + x + " * " + y + " = ");

        // 3. Add components to the root
        root.getChildren().addAll(name,userAnswer,submit,labelA,labelC,labelW,labelQuiz);

        // 4. Configure the components (colors, fonts, size, location)

        labelA.setPrefWidth(600);
        labelA.setFont(new Font("System", 18));
        labelA.setStyle("-fx-background-color: lightblue;-fx-text-fill:darkblue;");

        name.relocate(20,35);

        labelC.relocate(280, 65);
        labelC.setStyle("-fx-text-fill:green;");
        labelC.setFont(new Font("System", 18));
        labelW.relocate(283, 95);
        labelW.setStyle("-fx-text-fill:red;");
        labelW.setFont(new Font("System", 18));

        labelQuiz.relocate(40, 175);
        labelQuiz.setFont(new Font("Arial", 24));

        submit.relocate(325,260);


        // 5. Add Event Handlers and do final setup
        submit.setOnAction(this::calculate);

        // 6. Show the stage
        stage.show();
    }



    /**
     * This is the main method to launch app
     *
     * @param args unused
     */
    public static void main(String[] args) {
        launch(args);
    }
}


class MathAB {
    private int a;
    private int b;
    private int user;

    public MathAB (int a, int b) {
        this.a = a;
        this.b = b;
    }

    public void setA(int a) {
        this.a = a;
    }


    public void setB(int b) {
        this.b = b;
    }
    
    public void setUserAnswer(int user) {
        this.user = user;
    }

    public boolean getScore() {
        int answer = a * b;
        if (user == answer) //if user's answer is correct
        { return true;

        } else { //if user is wrong
            return false;
        }
    }

    @Override
    public String toString() {
        return "A" + a + " * B " + b + " = ";
    }
}
