package de.jeyp91.tippliga;

import java.util.ArrayList;
import java.util.HashSet;

public class TLWTeamsUpdater {

    Integer season;
    Integer league;
    ArrayList<TLWTeam> teamsOriginal;
    ArrayList<TLWMatch> matchesUpdated;
    ArrayList<TLWTeam> missingTeams;

    public TLWTeamsUpdater(int season, int league, String configFileName) throws Exception {

        TippligaSQLConnector conn = new TippligaSQLConnector();

        this.season = season;
        this.league = league;
        this.matchesUpdated = new ArrayList<>();

        TLWMatchesUpdaterFootball matchesUpdaterFootball = new TLWMatchesUpdaterFootball(season, league, configFileName);
        this.matchesUpdated.addAll(matchesUpdaterFootball.getTLWMatchesUpdated());
        this.matchesUpdated.addAll(conn.getMatches(String.valueOf(season), String.valueOf(league)));

        missingTeams = new ArrayList<>();

        teamsOriginal = conn.getTeams(String.valueOf(season), String.valueOf(league));

        HashSet<Integer> missingIds = this.initMissingIds();
        this.initMissingTeams(missingIds);
    }

    private HashSet<Integer> initMissingIds() {
        HashSet<Integer> missingIds = new HashSet<>();

        for(TLWMatch match : matchesUpdated) {
            if(isTeamMissing(match.getTeamIdHome())) {
                missingIds.add(match.getTeamIdHome());
            }
            if(isTeamMissing(match.getTeamIdGuest())) {
                missingIds.add(match.getTeamIdGuest());
            }
        }
        missingIds.remove(0);
        return missingIds;
    }

    private void initMissingTeams(HashSet<Integer> missingIds) {
        TippligaSQLConnector conn = new TippligaSQLConnector();
        for (Integer id : missingIds) {
            ArrayList<TLWTeam> teams = conn.getTeams(String.valueOf(id));
            missingTeams.add(new TLWTeam(
                    this.season,
                    this.league,
                    id,
                    teams.get(0).getTeamName(),
                    teams.get(0).getTeamNameShort(),
                    teams.get(0).getTeamSymbol(),
                    "",
                    0
            ));
        }
    }

    private boolean isTeamMissing(int id) {
        boolean teamMissing = true;
        for(TLWTeam team : teamsOriginal) {
            if(team.getTeamId() == id) {
                teamMissing = false;
                break;
            }
        }
        return teamMissing;
    }

    public String getInsertSQL() {
        String updateSql = "";

        for(TLWTeam team : missingTeams) {

            updateSql += "INSERT INTO phpbb_footb_teams VALUES (" +
                    team.getSeason() + ", " +
                    team.getLeague() + ", " +
                    team.getTeamId() + ", " +
                    "'" + team.getTeamName() + "', " +
                    "'" + team.getTeamNameShort() + "', " +
                    "'" + team.getTeamSymbol() + "', " +
                    "'" + team.getGroupId() + "', " +
                    team.getMatchday() +
                    ");\n";
        }

        return updateSql;
    }
}
