package controllers;

import controllers.strategytest.FibonacciStrategy;
import controllers.strategytest.OneThreeTwoSixStrategy;
import controllers.strategytest.OneXStrategy;
import data.GuiMysql;
import data.MatchStatSql;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import objects.SoccerMatch;

import java.net.URL;
import java.time.LocalDate;
import java.util.List;
import java.util.ResourceBundle;

public class StrategyTesterController implements Initializable {

    private static StrategyTesterController instance;
    @FXML
    public DatePicker datePicker;
    @FXML
    public ListView<SoccerMatch> MatchesListView;
    public DatePicker DP_StartDate;
    public DatePicker DP_EndDate;
    public Label L_InfoText;
    public Pane centerPane;

    @FXML
    public HBox HBox_testsPanel;
    public Button btn_go;
    public VBox oneXStatPanel;
    public VBox allInPanel;
    public VBox oneThreeTwoSixPanel;
    public VBox FibonacciPanel;
    public VBox KellyFormulaPanel;
    public VBox DutchingPanel;
    public VBox MoneyRacePanel;
    public VBox BetOnDrawPanel;
    public Label Label_LoadedMatches;

    boolean b_startDateOk = false;
    boolean b_endDateOk = false;
    private MatchStatSql database = new MatchStatSql();
    OneXStrategy oneXStrategy = new OneXStrategy();
    FibonacciStrategy fibonacciStrategy = new FibonacciStrategy();

    private OneThreeTwoSixStrategy oneThreeTwoSixStrategy = new OneThreeTwoSixStrategy();

    public static StrategyTesterController getInstance() {
        if (instance == null) {
            synchronized (StrategyTesterController.class) {
                if (instance == null) {
                    instance = new StrategyTesterController();
                }
            }
        }
        return instance;
    }

    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        instance = this;

        DP_StartDate.valueProperty().addListener((observable, oldValue, newValue) -> {
            checkDatesOk();
        });

        DP_EndDate.valueProperty().addListener((observable, oldValue, newValue) -> {
            checkDatesOk();
        });

        DP_EndDate.setValue(LocalDate.now());

        oneXStatPanel.getChildren().add(oneXStrategy);
        oneThreeTwoSixPanel.getChildren().add(oneThreeTwoSixStrategy);
        FibonacciPanel.getChildren().add(fibonacciStrategy);
    }

    private void checkDatesOk() {
        b_startDateOk = true;
        b_endDateOk = true;
        if (DP_StartDate.getValue() == null) {
            b_startDateOk = false;
            L_InfoText.setText("Start date cannot be empty");
            return;
        }
        if (DP_EndDate.getValue() == null) {
            b_endDateOk = false;
            L_InfoText.setText("End date cannot be empty");
            return;
        }

        if (DP_StartDate.getValue().isAfter(DP_EndDate.getValue())) {
            L_InfoText.setText("Start date cannot be after end date");
            b_startDateOk = false;
            b_endDateOk = false;
        }

        if (b_startDateOk && b_endDateOk) {
            L_InfoText.setDisable(true);
            L_InfoText.setVisible(false);
            HBox_testsPanel.setDisable(false);
            HBox_testsPanel.setVisible(true);
            btn_go.setDisable(false);
            btn_go.setVisible(true);

            getMatches();
        } else {
            L_InfoText.setDisable(false);
            L_InfoText.setVisible(true);

            HBox_testsPanel.setDisable(true);
            HBox_testsPanel.setVisible(false);

            btn_go.setDisable(true);
            btn_go.setVisible(false);
        }
    }

    private void getMatches() {
        List<SoccerMatch> matches = GuiMysql.getInstance().getPrioMatches(DP_StartDate.getValue().toString(), DP_EndDate.getValue().toString());
        Label_LoadedMatches.setText( String.valueOf(matches.size()) );
        oneXStrategy.setMatches(matches);
        oneThreeTwoSixStrategy.setMatches(matches);
        fibonacciStrategy.setMatches(matches);
    }

    public void runTests(ActionEvent actionEvent) {
        fibonacciStrategy.reset();
        //oneXStrategy.runTest();
        //oneThreeTwoSixStrategy.runTest();
        fibonacciStrategy.runTest();
    }
}
