package pl.lanuda.kret.limb;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;
import javafx.scene.layout.VBox;
import pl.lanuda.kret.limb.benchmark.BenchmarkResult;

class View {

    private TextField wrappedListSizeInputTextField;
    private TextField iteratedListSizeInputTextField;

    private Button runButton;

    private Label outputLabel;

    private Scene scene;

    View() {
        Label wrappedListSizeInputLabel = new Label("Wrapped list size");
        wrappedListSizeInputTextField = new TextField("1000");
        wrappedListSizeInputTextField.setTextFormatter(new TextFormatter<>(new BenchmarkInputTextFilter()));

        Label iteratedListSizeInputLabel = new Label("Iterated list size");
        iteratedListSizeInputTextField = new TextField("1000");
        iteratedListSizeInputTextField.setTextFormatter(new TextFormatter<>(new BenchmarkInputTextFilter()));

        runButton = new Button("Run");
        runButton.setDefaultButton(true);

        outputLabel = new Label();

        VBox root = new VBox(2, wrappedListSizeInputLabel, wrappedListSizeInputTextField,
                iteratedListSizeInputLabel, iteratedListSizeInputTextField, runButton, outputLabel);
        root.setPadding(new Insets(2));

        scene = new Scene(root, 600, 180);
    }

    String getWrappedListSizeInputText() {
        return wrappedListSizeInputTextField.getText();
    }

    String getIteratedListSizeInputText() {
        return iteratedListSizeInputTextField.getText();
    }

    void showProcessingStartedMessage(int wrappedListSize, int iteratedListSize) {
        outputLabel.setText(String.format("Running (wrapped size: %d, iterated size: %d)",
                wrappedListSize, iteratedListSize));
    }

    void showSuccessOutputMessage(BenchmarkResult result) {
        outputLabel.setText(String.format("Done. Intersection size: %d, execution duration: %dns.",
                result.getIntersection().size(),
                result.getExecutionDuration()));
    }

    void showFailureOutputMessage(Throwable exception) {
        outputLabel.setText(String.format("Failed. Failure reason: %s", exception.getMessage()));
    }

    void setRunButtonActionHandler(EventHandler<ActionEvent> actionHandler) {
        runButton.setOnAction(actionHandler);
    }

    Scene getScene() {
        return scene;
    }

    void enableControls() {
        setControlState(false);
    }

    void disableControls() {
        setControlState(true);
    }

    private void setControlState(boolean controlState) {
        runButton.setDisable(controlState);
        wrappedListSizeInputTextField.setDisable(controlState);
        iteratedListSizeInputTextField.setDisable(controlState);
    }
}

@@ -389,16 +389,48 @@
 esult;%0A%0A
+import java.text.NumberFormat;%0A%0A
 class Vi
@@ -640,16 +640,115 @@
     
-View() %7B
+private NumberFormat numberFormat;%0A%0A    View() %7B%0A        numberFormat = NumberFormat.getIntegerInstance();%0A
 %0A   
@@ -2006,17 +2006,17 @@
  size: %25
-d
+s
 , iterat
@@ -2025,17 +2025,17 @@
  size: %25
-d
+s
 )%22,%0A    
@@ -2046,33 +2046,90 @@
         
-wrappedListSize, 
+numberFormat.format(wrappedListSize),%0A                numberFormat.format(
 iterated
@@ -2138,16 +2138,17 @@
 stSize))
+)
 ;%0A    %7D%0A
@@ -2277,17 +2277,17 @@
  size: %25
-d
+s
 , execut
@@ -2301,17 +2301,17 @@
 ation: %25
-d
+s
 ns.%22,%0A  
@@ -2316,32 +2316,52 @@
                 
+numberFormat.format(
 result.getInters
@@ -2375,16 +2375,17 @@
 ).size()
+)
 ,%0A      
@@ -2394,16 +2394,36 @@
         
+numberFormat.format(
 result.g
@@ -2441,24 +2441,25 @@
 Duration()))
+)
 ;%0A    %7D%0A%0A   

Update numbers' formatting in output label