package de.jeyp91.tippliga;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.Set;

public class TLWTeamsCreator {
    int season;
    int league;
    ArrayList<TLWMatch> matches;
    TippligaSQLConnector connector = new TippligaSQLConnector();

    public TLWTeamsCreator(int season, int league, ArrayList<TLWMatch> matches) {
        this.season = season;
        this.league = league;
        this.matches = matches;
    }

    public Set<Integer> getTeamIds() {
        Set<Integer> teamIds = new LinkedHashSet<>();
        for(TLWMatch match : matches) {
            if(match.getTeamIdHome() != null) {
                teamIds.add(match.getTeamIdHome());
            }
            if(match.getTeamIdGuest() != null) {
                teamIds.add(match.getTeamIdGuest());
            }
        }
        return teamIds;
    }

    public String getSql() {
        Set<Integer> teamIds = getTeamIds();
        String sql = "";
        for (Integer id : teamIds) {
            ArrayList<TLWTeam> teams = connector.getTeams(String.valueOf(id));
            String teamName = teams.get(0).getTeamName();
            String teamNameShort = teams.get(0).getTeamNameShort();
            String teamSymbol = teams.get(0).getTeamSymbol();
            String groupId = "";
            int matchday = 0;
            sql += "REPLACE INTO phpbb_footb_teams VALUES ('";
            sql += this.season;
            sql += "', '";
            sql += this.league;
            sql += "', '";
            sql += id;
            sql += "', '";
            sql += teamName;
            sql += "', '";
            sql += teamNameShort;
            sql += "', '";
            sql += teamSymbol;
            sql += "', '";
            sql += groupId;
            sql += "', '";
            sql += matchday;
            sql += "');\n";
        }
        return sql;
    }
}
