package controllers;

import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.ResourceBundle;

import com.google.common.collect.Maps;

import data.GuiMysql;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TableView.TableViewSelectionModel;
import javafx.scene.control.cell.MapValueFactory;
import objects.TeamStanding;

@SuppressWarnings("rawtypes") public class MatchStatTabController implements Initializable {

	private static final String GOALS_CONCEDED = "goalsConceded";

	private static final String GOALS_SCORED = "goalsScored";

	private static final String POINTS_STRING = "points";

	private static final String GAMES_PLAYED = "gamesPlayed";

	private static MatchStatTabController instance;

	@FXML TableView<Map<String, Object>> matchStatTable;

	@FXML TableColumn<Map, String> team = new TableColumn<>("team");
	@FXML TableColumn<Map, Integer> gamesPlayed = new TableColumn<>(GAMES_PLAYED);
	@FXML TableColumn<Map, Integer> win = new TableColumn<>("win");
	@FXML TableColumn<Map, Integer> draw = new TableColumn<>("draw");
	@FXML TableColumn<Map, Integer> loss = new TableColumn<>("loss");
	@FXML TableColumn<Map, Integer> points = new TableColumn<>(POINTS_STRING);
	@FXML TableColumn<Map, Float> goalsScored = new TableColumn<>(GOALS_SCORED);
	@FXML TableColumn<Map, Float> goalsConceded = new TableColumn<>(GOALS_CONCEDED);
	@FXML TableColumn<Map, Float> diff = new TableColumn<>("diff");

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

	@Override public void initialize(URL arg0, ResourceBundle arg1) {
		team.setCellValueFactory(new MapValueFactory<>("team"));
		gamesPlayed.setCellValueFactory(new MapValueFactory<>(GAMES_PLAYED));
		win.setCellValueFactory(new MapValueFactory<>("win"));
		draw.setCellValueFactory(new MapValueFactory<>("draw"));
		loss.setCellValueFactory(new MapValueFactory<>("loss"));
		points.setCellValueFactory(new MapValueFactory<>(POINTS_STRING));
		goalsScored.setCellValueFactory(new MapValueFactory<>(GOALS_SCORED));
		goalsConceded.setCellValueFactory(new MapValueFactory<>(GOALS_CONCEDED));
		diff.setCellValueFactory(new MapValueFactory<>("diff"));

		instance = this;
	}

	public void setLeagueStandingsTable(int leagueId, String season, int countryId, String date) {
		final List<TeamStanding> res = GuiMysql.getInstance().getLeagueTable(leagueId, season, countryId, date);
		matchStatTable.getItems().clear();
		for (final TeamStanding ts : res) {
			final Map<String, Object> line = Maps.newHashMap();
			final int numGamesPlayed = ts.getWins() + ts.getDraws() + ts.getLosses();
			line.put("team", ts.getTeamName());
			line.put(GAMES_PLAYED, numGamesPlayed);
			line.put("win", ts.getWins());
			line.put("draw", ts.getDraws());
			line.put("loss", ts.getLosses());
			line.put(POINTS_STRING, ts.getPoints());
			line.put(GOALS_SCORED, ts.getGoalsScored() / numGamesPlayed);
			line.put(GOALS_CONCEDED, ts.getGoalsConceded() / numGamesPlayed);

			matchStatTable.getItems().add(line);
		}
	}

	public void setTeamSelected(String teamName, boolean homeTeam) {
		TableViewSelectionModel<Map<String, Object>> selectionModel = matchStatTable.getSelectionModel();
		java.util.Optional<Map<String, Object>> optional = matchStatTable.getItems().stream().filter(p -> p.get("team").equals(teamName)).findFirst();
		if (optional.isPresent()) {
			selectionModel.setSelectionMode(SelectionMode.MULTIPLE);
			int indexOf = matchStatTable.getItems().indexOf(optional.get());
			selectionModel.select(indexOf);
		}
	}

	public void clearSelection() {
		TableViewSelectionModel<Map<String, Object>> selectionModel = matchStatTable.getSelectionModel();
		selectionModel.clearSelection();
	}

	public int getTeamPositionDiff(String homeTeamName, String awayTeamName) {
		int result = 0;
		Optional<Map<String, Object>> homeTeamOptional = matchStatTable.getItems().stream().filter(p -> p.get("team").equals(homeTeamName))
				.findFirst();
		Optional<Map<String, Object>> awayTeamOptional = matchStatTable.getItems().stream().filter(p -> p.get("team").equals(awayTeamName))
				.findFirst();

		if (homeTeamOptional.isPresent() && awayTeamOptional.isPresent()) {
			Map<String, Object> homeTeam = homeTeamOptional.get();
			Map<String, Object> awayTeam = awayTeamOptional.get();

			int homeTeamPos = matchStatTable.getItems().indexOf(homeTeam);
			int awayTeamPos = matchStatTable.getItems().indexOf(awayTeam);
			result = Math.abs(homeTeamPos - awayTeamPos);
		}

		return result;
	}
}
