package ch.zhaw.prog2.fxmlcalculator;

import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.CheckMenuItem;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.Scene;

import javax.xml.transform.Result;
import java.io.IOException;

public class MainWindowController {
    ValueHandlerDecorator valueHandlerDecorator;
    ValueHandler valueHandler;
    String helpText;

    @FXML
    private TextField annualCost;

    @FXML
    private CheckMenuItem clearAnnualCosts;

    @FXML
    private CheckMenuItem clearInitialAmount;

    @FXML
    private CheckMenuItem clearNumberOfYears;

    @FXML
    private CheckMenuItem clearReturnInPercent;

    @FXML
    private TextField initialAmount;

    @FXML
    private TextField numberOfYears;

    @FXML
    private TextArea results;

    @FXML
    private TextField returnInPercent;

    @FXML
    void calculate(ActionEvent event) {
        valueHandlerDecorator.checkValuesAndCalculateResult(initialAmount.getText(), returnInPercent.getText(), annualCost.getText(), numberOfYears.getText());
        if(valueHandler.areValuesOk()){
            results.setBorder(new Border(new BorderStroke(Color.GREEN, BorderStrokeStyle.SOLID, CornerRadii.EMPTY, new BorderWidths(2))));
        }else {
            results.setBorder(new Border(new BorderStroke(Color.RED, BorderStrokeStyle.SOLID, CornerRadii.EMPTY, new BorderWidths(2))));
        }
    }

    @FXML
    void clearResults(ActionEvent event) {
        valueHandlerDecorator.clearResult();
        results.setBorder(null);
    }


    @FXML
    void clearValues(ActionEvent event) {
        if(clearAnnualCosts.isSelected()){
            annualCost.clear();
        }
        if(clearReturnInPercent.isSelected()){
            returnInPercent.clear();
        }
        if(clearNumberOfYears.isSelected()){
            numberOfYears.clear();
        }
        if(clearInitialAmount.isSelected()){
            initialAmount.clear();
        }
    }

    @FXML
    void close(ActionEvent event) {
        Platform.exit();
    }

    @FXML
    void openResultWindow(ActionEvent event) {

        FXMLLoader resultWindowLoader = new FXMLLoader(getClass().getResource("ResultWindow.fxml"));
        Pane resultNode = new StackPane();
        try {
            resultNode = resultWindowLoader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }
        ResultWindowController resultWindowController = resultWindowLoader.getController();
        resultWindowController.setValueHandler(valueHandler, valueHandlerDecorator);
        Scene resultScene = new Scene(resultNode);

        // New window (Stage)
        Stage resultStage = new Stage();
        resultStage.setTitle("Results");
        resultStage.setScene(resultScene);

        resultStage.show();

        valueHandlerDecorator.informListener();
    }


    @FXML
    void showHelp(ActionEvent event) {
        valueHandlerDecorator.showHelp(helpText);
        results.setBorder(new Border(new BorderStroke(Color.BLUE, BorderStrokeStyle.SOLID, CornerRadii.EMPTY, new BorderWidths(2))));
    }

    public void setValueHandler(ValueHandler valueHandler){
        this.valueHandler = valueHandler;
        valueHandlerDecorator = new ValueHandlerDecorator(valueHandler);
        valueHandlerDecorator.addListener(new IsObserver() {
            @Override
            public void update() {
                results.setText(valueHandler.getResultBound());
            }
        });
    }

    public void setHelpText(String helpText){
        this.helpText = helpText;
    }

}
