package de.jeyp91.apifootball;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import java.util.ArrayList;

public class APIFootballMatchesProvider {

    APIFootballConnector conn;
    public APIFootballMatchesProvider(int season) {
        this.conn = APIFootballConnector.getAPIFootballConnectorInstance(season - 1);
    }

    public ArrayList<APIFootballMatch> getAPIFootballMatchesFromConfig(JSONArray config) {
        ArrayList<APIFootballMatch> apiFootballMatches = new ArrayList<>();
        for (Object singleConfigObject : config) {
            JSONObject singleConfig = (JSONObject) singleConfigObject;
            String type = (String) singleConfig.get("type");
            switch (type) {
                case "AllMatchesOfMatchday":
                    int matchesLeague = ((Long) singleConfig.get("matchesLeague")).intValue();
                    int leagueMatchday = ((Long) singleConfig.get("leagueMatchday")).intValue();
                    apiFootballMatches.addAll(conn.getMatchDataByLeagueAndMatchday(matchesLeague, leagueMatchday));
                    break;
                case "SingleMatch":
                    int matchLeague = ((Long) singleConfig.get("matchLeague")).intValue();
                    int matchId = ((Long) singleConfig.get("matchId")).intValue();
                    apiFootballMatches.add(conn.getMatchDataByLeagueAndMatchID(matchLeague, matchId));
                    break;
            }
        }
        return apiFootballMatches;
    }
}
