package controllers;

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

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.MapValueFactory;
import objects.SoccerMatch;

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

	@FXML TableColumn<Map, String> homeTeamColumn = new TableColumn<>("Home Team");
	@FXML TableColumn<Map, String> awayTeamColumn = new TableColumn<>("Away Team");
	@FXML TableColumn<Map, Float> goalsHomeColumn = new TableColumn<>("Goals home");
	@FXML TableColumn<Map, Float> goalsAwayColumn = new TableColumn<>("Goals away");
	@FXML TableColumn<Map, String> countryColumn = new TableColumn<>("Country");
	@FXML TableColumn<Map, String> leagueColumn = new TableColumn<>("League");

	@FXML private TableView<Map<String, Object>> matchResultTable;

	private ObservableList<Map<String, Object>> matches;

	@Override public void initialize(URL arg0, ResourceBundle arg1) {
		homeTeamColumn.setCellValueFactory(new MapValueFactory<>("homeTeam"));
		awayTeamColumn.setCellValueFactory(new MapValueFactory<>("awayTeam"));
		goalsHomeColumn.setCellValueFactory(new MapValueFactory<>("goalsHome"));
		goalsAwayColumn.setCellValueFactory(new MapValueFactory<>("goalsAway"));
		countryColumn.setCellValueFactory(new MapValueFactory<>("country"));
		leagueColumn.setCellValueFactory(new MapValueFactory<>("league"));

		matches = FXCollections.<Map<String, Object>>observableArrayList();

	}

	public void setMatches(List<SoccerMatch> upcomingMatches) {

		for (final SoccerMatch soccerMatch : upcomingMatches) {
			final Map<String, Object> match = new HashMap<>();
			match.put("homeTeam", soccerMatch.getHomeTeam().getTeamName());
			match.put("awayTeam", soccerMatch.getAwayTeam().getTeamName());

			match.put("country", soccerMatch.getHomeTeam().getCountryName());
			match.put("league", soccerMatch.getHomeTeam().getTeamLeague());

			matches.add(match);
		}

		matchResultTable.getItems().addAll(matches);

	}
}
