package controllers;

import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ResourceBundle;

import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TextField;
import objects.DaysSinceLastRaceSummary;
import parsers.TravsportParser;

public class MainController implements Initializable {

    @FXML private Button getHorseStatsButton;
    @FXML private Button getDriverStatsButton;
    @FXML private Button racesByDate;
    @FXML private TextField horseNameTextField;
    @FXML private TextField driverFirstNameTextField;
    @FXML private TextField driverLastNameTextField;
    @FXML private Button testButton;
    @FXML private Button updateResultsButton;
    @FXML private V75TabController V75TabController;
    @FXML private LDTabController LDTabController;
    @FXML public DatePicker datePicker;

    public static SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
    private int mRaceId;
    private final DaysSinceLastRaceSummary driverLastRaceSummary = new DaysSinceLastRaceSummary();

    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        updateRaceId();

        horseNameTextField.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> ov, String oldValue, String newValue) {
                if (newValue.length() > 1) {
                    getHorseStatsButton.setDisable(false);
                } else {
                    getHorseStatsButton.setDisable(true);
                }
            }
        });

        driverFirstNameTextField.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> ov, String oldValue, String newValue) {
                if (newValue.length() > 1) {
                    getDriverStatsButton.setDisable(false);
                } else {
                    getDriverStatsButton.setDisable(true);
                }
            }

        });
        driverLastNameTextField.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> ov, String oldValue, String newValue) {
                if (newValue.length() > 1) {
                    getDriverStatsButton.setDisable(false);
                } else {
                    getDriverStatsButton.setDisable(true);
                }
            }

        });
    }

    private void updateRaceId() {
        mRaceId = DatabaseController.getInstance().getRaceId();
        if (mRaceId != -1) {
            updateResultsButton.setDisable(false);
        } else {
            updateResultsButton.setDisable(true);
        }
    }

    @FXML
    public void GetHorseStatsAction() { // Test class
        final int horseId = TravsportParser.getInstance().getHorseIdByName(horseNameTextField.getText());
        System.out.println("Getting stats for horse " + horseNameTextField.getText() + " with id: " + horseId);
        TravsportParser.getInstance().getHorseStatByIdJson(horseId, horseNameTextField.getText(), "", -1);
    }

    @FXML
    public void GetDriverStatsAction() { // Test class
        final String firstName = driverFirstNameTextField.getText().trim();
        final String lastName = driverLastNameTextField.getText().trim();
        int driverId = DatabaseController.getInstance().getDriverTravsportId(firstName, lastName);
        if (driverId <= 0) {
            driverId = TravsportParser.getInstance().getDriverIdByName(firstName, lastName);
        }
        TravsportParser.getInstance().getDriverStatById(driverId, lastName + " " + firstName, -1);
    }

    @FXML
    public void TestAction() {
        System.out.println("TEST ACTION");
    }

    @FXML
    public void UpdateResults() {
        updateResultsButton.setDisable(true);
        Platform.runLater(() -> {
            while (mRaceId > 0) {
                TravsportParser.getInstance().updateRaceResults(mRaceId);
                DatabaseController.getInstance().removeRaceIds(mRaceId); // Strukna hästar tas bort från rättningen
                updateRaceId();
            }
        });
    }

    @FXML
    public void MainTabSelectionChange(Event e) {
    }
}
