package controllers.NewTab;

import java.net.URL;
import java.util.AbstractMap.SimpleEntry;
import java.util.List;
import java.util.ResourceBundle;
import java.util.stream.Collectors;

import com.google.common.collect.Lists;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TabPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import objects.HorseRaceData;
import objects.NewTabRaceTableView;
import objects.ResultsDTO;

public class MainContent implements Initializable {

    @FXML TabPane mainNewTabTableView;
    @FXML VBox BetOnContent;
    @FXML Button SubmitBongButton;

    @FXML Pane kuskPanel;
    @FXML Pane horsePanel;
    @FXML Pane ekipagePane;

    List<SimpleEntry<String, List<HorseRaceData>>> racesByTrack = Lists.newArrayList();

    private static MainContent mainContent;

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

    public void updateRaceTable(List<HorseRaceData> data) {
        final List<String> tracks = data.stream().map(m -> m.getTrackName()).distinct().collect(Collectors.toList());
        mainNewTabTableView.getTabs().clear();
        for (final String t : tracks) {
            final SimpleEntry<String, List<HorseRaceData>> trackRaces
                    = new SimpleEntry<>(t, data.stream().filter(p -> p.getTrackName().equals(t)).collect(Collectors.toList()));
            racesByTrack.add(trackRaces);
            final NewTabRaceTableView newTabRaceTableView = new NewTabRaceTableView(trackRaces);
            newTabRaceTableView.setBongObject(BetOnContent);
            mainNewTabTableView.getTabs().add(newTabRaceTableView);
        }
    }

    public static MainContent getThis() {
        return mainContent;
    }

    @FXML
    void SubmitBongAction() {

    }

    public void setAvgTimeLabel(Label firstLabel, Label secondLabel, Label middleText, ResultsDTO firstValue, Float globalAvg,
            ResultsDTO compareValue) {
        firstLabel.setText(firstValue.toString());
        middleText.setText(" (" + globalAvg.toString() + ") ");
        secondLabel.setText(compareValue.toString());

        secondLabel.setDisable(false);
        secondLabel.setOpacity(1);

        middleText.setDisable(false);
        middleText.setOpacity(1);
        if (firstValue.getRes() < compareValue.getRes() && firstValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: green;");
            secondLabel.setStyle("-fx-text-fill: red;");
        } else if (firstValue.getRes() > compareValue.getRes() && compareValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: red;");
            secondLabel.setStyle("-fx-text-fill: green;");
        } else if (firstValue.getRes() == 0f && compareValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: black;");
            secondLabel.setStyle("-fx-text-fill: green;");
        } else if (firstValue.getRes() > 0f && compareValue.getRes() == 0f) {
            firstLabel.setStyle("-fx-text-fill: green;");
            secondLabel.setStyle("-fx-text-fill: black;");
        } else {
            firstLabel.setStyle("-fx-text-fill: black;");
            secondLabel.setStyle("-fx-text-fill: black;");
        }
    }

    public void setAvgTimeLimit5Label(Label firstLabel, Label secondLabel, Label middleText, ResultsDTO firstValue,
            Float globalAvg, ResultsDTO compareValue) {
        firstLabel.setText(firstValue.toString());
        middleText.setText(" (" + globalAvg.toString() + ") ");
        secondLabel.setText(compareValue.toString());

        secondLabel.setDisable(false);
        secondLabel.setOpacity(1);

        middleText.setDisable(false);
        middleText.setOpacity(1);
        if (firstValue.getRes() < compareValue.getRes() && firstValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: green;");
            secondLabel.setStyle("-fx-text-fill: red;");
        } else if (firstValue.getRes() > compareValue.getRes() && compareValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: red;");
            secondLabel.setStyle("-fx-text-fill: green;");
        } else if (firstValue.getRes() == 0f && compareValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: black;");
            secondLabel.setStyle("-fx-text-fill: green;");
        } else if (firstValue.getRes() > 0f && compareValue.getRes() == 0f) {
            firstLabel.setStyle("-fx-text-fill: green;");
            secondLabel.setStyle("-fx-text-fill: black;");
        } else {
            firstLabel.setStyle("-fx-text-fill: black;");
            secondLabel.setStyle("-fx-text-fill: black;");
        }
    }

    public void setAvgTimeLimit10Label(Label firstLabel, Label secondLabel, Label middleText, ResultsDTO firstValue,
            Float globalAvg, ResultsDTO compareValue) {
        firstLabel.setText(firstValue.toString());
        middleText.setText(" (" + globalAvg.toString() + ") ");
        secondLabel.setText(compareValue.toString());

        secondLabel.setDisable(false);
        secondLabel.setOpacity(1);

        middleText.setDisable(false);
        middleText.setOpacity(1);
        if (firstValue.getRes() < compareValue.getRes() && firstValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: green;");
            secondLabel.setStyle("-fx-text-fill: red;");
        } else if (firstValue.getRes() > compareValue.getRes() && compareValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: red;");
            secondLabel.setStyle("-fx-text-fill: green;");
        } else if (firstValue.getRes() == 0f && compareValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: black;");
            secondLabel.setStyle("-fx-text-fill: green;");
        } else if (firstValue.getRes() > 0f && compareValue.getRes() == 0f) {
            firstLabel.setStyle("-fx-text-fill: green;");
            secondLabel.setStyle("-fx-text-fill: black;");
        } else {
            firstLabel.setStyle("-fx-text-fill: black;");
            secondLabel.setStyle("-fx-text-fill: black;");
        }
    }

    public void setAvgTimeLimit15Label(Label firstLabel, Label secondLabel, Label middleText, ResultsDTO firstValue,
            Float globalAvg, ResultsDTO compareValue) {
        firstLabel.setText(firstValue.toString());
        middleText.setText(" (" + globalAvg.toString() + ") ");
        secondLabel.setText(compareValue.toString());

        secondLabel.setDisable(false);
        secondLabel.setOpacity(1);

        middleText.setDisable(false);
        middleText.setOpacity(1);
        if (firstValue.getRes() < compareValue.getRes() && firstValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: green;");
            secondLabel.setStyle("-fx-text-fill: red;");
        } else if (firstValue.getRes() > compareValue.getRes() && compareValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: red;");
            secondLabel.setStyle("-fx-text-fill: green;");
        } else if (firstValue.getRes() == 0f && compareValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: black;");
            secondLabel.setStyle("-fx-text-fill: green;");
        } else if (firstValue.getRes() > 0f && compareValue.getRes() == 0f) {
            firstLabel.setStyle("-fx-text-fill: green;");
            secondLabel.setStyle("-fx-text-fill: black;");
        } else {
            firstLabel.setStyle("-fx-text-fill: black;");
            secondLabel.setStyle("-fx-text-fill: black;");
        }
    }

    public void setAvgTimeDistanceLabel(Label firstLabel, Label secondLabel, Label middleText, ResultsDTO firstValue,
            Float globalAvg, ResultsDTO compareValue) {
        firstLabel.setText(firstValue.toString());
        middleText.setText(" (" + globalAvg.toString() + ") ");
        secondLabel.setText(compareValue.toString());

        secondLabel.setDisable(false);
        secondLabel.setOpacity(1);

        middleText.setDisable(false);
        middleText.setOpacity(1);
        if (firstValue.getRes() < compareValue.getRes() && firstValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: green;");
            secondLabel.setStyle("-fx-text-fill: red;");
        } else if (firstValue.getRes() > compareValue.getRes() && compareValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: red;");
            secondLabel.setStyle("-fx-text-fill: green;");
        } else if (firstValue.getRes() == 0f && compareValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: black;");
            secondLabel.setStyle("-fx-text-fill: green;");
        } else if (firstValue.getRes() > 0f && compareValue.getRes() == 0f) {
            firstLabel.setStyle("-fx-text-fill: green;");
            secondLabel.setStyle("-fx-text-fill: black;");
        } else {
            firstLabel.setStyle("-fx-text-fill: black;");
            secondLabel.setStyle("-fx-text-fill: black;");
        }
    }

    public void setAvgTimeDistance5LabelWithCompare(Label firstLabel, Label secondLabel, Label middleText, ResultsDTO firstValue,
            Float globalAvg, ResultsDTO compareValue) {
        firstLabel.setText(firstValue.toString());
        middleText.setText(" (" + globalAvg.toString() + ") ");
        secondLabel.setText(compareValue.toString());

        secondLabel.setDisable(false);
        secondLabel.setOpacity(1);

        middleText.setDisable(false);
        middleText.setOpacity(1);
        if (firstValue.getRes() < compareValue.getRes() && firstValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: green;");
            secondLabel.setStyle("-fx-text-fill: red;");
        } else if (firstValue.getRes() > compareValue.getRes() && compareValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: red;");
            secondLabel.setStyle("-fx-text-fill: green;");
        } else if (firstValue.getRes() == 0f && compareValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: black;");
            secondLabel.setStyle("-fx-text-fill: green;");
        } else if (firstValue.getRes() > 0f && compareValue.getRes() == 0f) {
            firstLabel.setStyle("-fx-text-fill: green;");
            secondLabel.setStyle("-fx-text-fill: black;");
        } else {
            firstLabel.setStyle("-fx-text-fill: black;");
            secondLabel.setStyle("-fx-text-fill: black;");
        }
    }

    public void setAvgTimeDistanceLimit10Label(Label firstLabel, Label secondLabel, Label middleText, ResultsDTO firstValue,
            Float globalAvg, ResultsDTO compareValue) {
        firstLabel.setText(firstValue.toString());
        middleText.setText(" (" + globalAvg.toString() + ") ");
        secondLabel.setText(compareValue.toString());

        secondLabel.setDisable(false);
        secondLabel.setOpacity(1);

        middleText.setDisable(false);
        middleText.setOpacity(1);
        if (firstValue.getRes() < compareValue.getRes() && firstValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: green;");
            secondLabel.setStyle("-fx-text-fill: red;");
        } else if (firstValue.getRes() > compareValue.getRes() && compareValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: red;");
            secondLabel.setStyle("-fx-text-fill: green;");
        } else if (firstValue.getRes() == 0f && compareValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: black;");
            secondLabel.setStyle("-fx-text-fill: green;");
        } else if (firstValue.getRes() > 0f && compareValue.getRes() == 0f) {
            firstLabel.setStyle("-fx-text-fill: green;");
            secondLabel.setStyle("-fx-text-fill: black;");
        } else {
            firstLabel.setStyle("-fx-text-fill: black;");
            secondLabel.setStyle("-fx-text-fill: black;");
        }
    }

    public void setAvgTimeDistanceLimit15Label(Label firstLabel, Label secondLabel, Label middleText, ResultsDTO firstValue,
            Float globalAvg, ResultsDTO compareValue) {
        firstLabel.setText(firstValue.toString());
        middleText.setText(" (" + globalAvg.toString() + ") ");
        secondLabel.setText(compareValue.toString());

        secondLabel.setDisable(false);
        secondLabel.setOpacity(1);

        middleText.setDisable(false);
        middleText.setOpacity(1);
        if (firstValue.getRes() < compareValue.getRes() && firstValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: green;");
            secondLabel.setStyle("-fx-text-fill: red;");
        } else if (firstValue.getRes() > compareValue.getRes() && compareValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: red;");
            secondLabel.setStyle("-fx-text-fill: green;");
        } else if (firstValue.getRes() == 0f && compareValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: black;");
            secondLabel.setStyle("-fx-text-fill: green;");
        } else if (firstValue.getRes() > 0f && compareValue.getRes() == 0f) {
            firstLabel.setStyle("-fx-text-fill: green;");
            secondLabel.setStyle("-fx-text-fill: black;");
        } else {
            firstLabel.setStyle("-fx-text-fill: black;");
            secondLabel.setStyle("-fx-text-fill: black;");
        }
    }

    public void setAvgPosition(Label firstLabel, Label secondLabel, Label middleText, ResultsDTO firstValue,
            ResultsDTO compareValue) {
        firstLabel.setText(firstValue.toString());
        secondLabel.setText(compareValue.toString());

        secondLabel.setDisable(false);
        secondLabel.setOpacity(1);

        middleText.setDisable(false);
        middleText.setOpacity(1);
        if (firstValue.getRes() < compareValue.getRes() && firstValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: green;");
            secondLabel.setStyle("-fx-text-fill: red;");
        } else if (firstValue.getRes() > compareValue.getRes() && compareValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: red;");
            secondLabel.setStyle("-fx-text-fill: green;");
        } else if (firstValue.getRes() == 0f && compareValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: black;");
            secondLabel.setStyle("-fx-text-fill: green;");
        } else if (firstValue.getRes() > 0f && compareValue.getRes() == 0f) {
            firstLabel.setStyle("-fx-text-fill: green;");
            secondLabel.setStyle("-fx-text-fill: black;");
        } else {
            firstLabel.setStyle("-fx-text-fill: black;");
            secondLabel.setStyle("-fx-text-fill: black;");
        }
    }

    public void setAvgPositionLast5(Label firstLabel, Label secondLabel, Label middleText, ResultsDTO firstValue,
            ResultsDTO compareValue) {
        firstLabel.setText(firstValue.toString());
        secondLabel.setText(compareValue.toString());

        secondLabel.setDisable(false);
        secondLabel.setOpacity(1);

        middleText.setDisable(false);
        middleText.setOpacity(1);
        if (firstValue.getRes() < compareValue.getRes() && firstValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: green;");
            secondLabel.setStyle("-fx-text-fill: red;");
        } else if (firstValue.getRes() > compareValue.getRes() && compareValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: red;");
            secondLabel.setStyle("-fx-text-fill: green;");
        } else if (firstValue.getRes() == 0f && compareValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: black;");
            secondLabel.setStyle("-fx-text-fill: green;");
        } else if (firstValue.getRes() > 0f && compareValue.getRes() == 0f) {
            firstLabel.setStyle("-fx-text-fill: green;");
            secondLabel.setStyle("-fx-text-fill: black;");
        } else {
            firstLabel.setStyle("-fx-text-fill: black;");
            secondLabel.setStyle("-fx-text-fill: black;");
        }
    }

    public void setAvgPositionLast10(Label firstLabel, Label secondLabel, Label middleText, ResultsDTO firstValue,
            ResultsDTO compareValue) {
        firstLabel.setText(firstValue.toString());
        secondLabel.setText(compareValue.toString());

        secondLabel.setDisable(false);
        secondLabel.setOpacity(1);

        middleText.setDisable(false);
        middleText.setOpacity(1);
        if (firstValue.getRes() < compareValue.getRes() && firstValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: green;");
            secondLabel.setStyle("-fx-text-fill: red;");
        } else if (firstValue.getRes() > compareValue.getRes() && compareValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: red;");
            secondLabel.setStyle("-fx-text-fill: green;");
        } else if (firstValue.getRes() == 0f && compareValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: black;");
            secondLabel.setStyle("-fx-text-fill: green;");
        } else if (firstValue.getRes() > 0f && compareValue.getRes() == 0f) {
            firstLabel.setStyle("-fx-text-fill: green;");
            secondLabel.setStyle("-fx-text-fill: black;");
        } else {
            firstLabel.setStyle("-fx-text-fill: black;");
            secondLabel.setStyle("-fx-text-fill: black;");
        }
    }

    public void setAvgPositionLast15(Label firstLabel, Label secondLabel, Label middleText, ResultsDTO firstValue,
            ResultsDTO compareValue) {
        firstLabel.setText(firstValue.toString());
        secondLabel.setText(compareValue.toString());

        secondLabel.setDisable(false);
        secondLabel.setOpacity(1);

        middleText.setDisable(false);
        middleText.setOpacity(1);
        if (firstValue.getRes() < compareValue.getRes() && firstValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: green;");
            secondLabel.setStyle("-fx-text-fill: red;");
        } else if (firstValue.getRes() > compareValue.getRes() && compareValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: red;");
            secondLabel.setStyle("-fx-text-fill: green;");
        } else if (firstValue.getRes() == 0f && compareValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: black;");
            secondLabel.setStyle("-fx-text-fill: green;");
        } else if (firstValue.getRes() > 0f && compareValue.getRes() == 0f) {
            firstLabel.setStyle("-fx-text-fill: green;");
            secondLabel.setStyle("-fx-text-fill: black;");
        } else {
            firstLabel.setStyle("-fx-text-fill: black;");
            secondLabel.setStyle("-fx-text-fill: black;");
        }
    }

    public void setAvgPositionDistanceLast5(Label firstLabel, Label secondLabel, Label middleText, ResultsDTO firstValue,
            ResultsDTO compareValue) {
        firstLabel.setText(firstValue.toString());
        secondLabel.setText(compareValue.toString());

        secondLabel.setDisable(false);
        secondLabel.setOpacity(1);

        middleText.setDisable(false);
        middleText.setOpacity(1);
        if (firstValue.getRes() < compareValue.getRes() && firstValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: green;");
            secondLabel.setStyle("-fx-text-fill: red;");
        } else if (firstValue.getRes() > compareValue.getRes() && compareValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: red;");
            secondLabel.setStyle("-fx-text-fill: green;");
        } else if (firstValue.getRes() == 0f && compareValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: black;");
            secondLabel.setStyle("-fx-text-fill: green;");
        } else if (firstValue.getRes() > 0f && compareValue.getRes() == 0f) {
            firstLabel.setStyle("-fx-text-fill: green;");
            secondLabel.setStyle("-fx-text-fill: black;");
        } else {
            firstLabel.setStyle("-fx-text-fill: black;");
            secondLabel.setStyle("-fx-text-fill: black;");
        }
    }

    public void setAvgPositionDistanceLast10(Label firstLabel, Label secondLabel, Label middleText, ResultsDTO firstValue,
            ResultsDTO compareValue) {
        firstLabel.setText(firstValue.toString());
        secondLabel.setText(compareValue.toString());

        secondLabel.setDisable(false);
        secondLabel.setOpacity(1);

        middleText.setDisable(false);
        middleText.setOpacity(1);
        if (firstValue.getRes() < compareValue.getRes() && firstValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: green;");
            secondLabel.setStyle("-fx-text-fill: red;");
        } else if (firstValue.getRes() > compareValue.getRes() && compareValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: red;");
            secondLabel.setStyle("-fx-text-fill: green;");
        } else if (firstValue.getRes() == 0f && compareValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: black;");
            secondLabel.setStyle("-fx-text-fill: green;");
        } else if (firstValue.getRes() > 0f && compareValue.getRes() == 0f) {
            firstLabel.setStyle("-fx-text-fill: green;");
            secondLabel.setStyle("-fx-text-fill: black;");
        } else {
            firstLabel.setStyle("-fx-text-fill: black;");
            secondLabel.setStyle("-fx-text-fill: black;");
        }
    }

    public void setAvgPositionDistanceLast15(Label firstLabel, Label secondLabel, Label middleText, ResultsDTO firstValue,
            ResultsDTO compareValue) {
        firstLabel.setText(firstValue.toString());
        secondLabel.setText(compareValue.toString());

        secondLabel.setDisable(false);
        secondLabel.setOpacity(1);

        middleText.setDisable(false);
        middleText.setOpacity(1);
        if (firstValue.getRes() < compareValue.getRes() && firstValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: green;");
            secondLabel.setStyle("-fx-text-fill: red;");
        } else if (firstValue.getRes() > compareValue.getRes() && compareValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: red;");
            secondLabel.setStyle("-fx-text-fill: green;");
        } else if (firstValue.getRes() == 0f && compareValue.getRes() > 0f) {
            firstLabel.setStyle("-fx-text-fill: black;");
            secondLabel.setStyle("-fx-text-fill: green;");
        } else if (firstValue.getRes() > 0f && compareValue.getRes() == 0f) {
            firstLabel.setStyle("-fx-text-fill: green;");
            secondLabel.setStyle("-fx-text-fill: black;");
        } else {
            firstLabel.setStyle("-fx-text-fill: black;");
            secondLabel.setStyle("-fx-text-fill: black;");
        }
    }
}
