package de.jeyp91.apifootball;

import de.jeyp91.gists.GistProvider;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.util.ArrayList;
import java.util.HashMap;

/**
 *
 */
public class APIFootballConnector {

    private static APIFootballConnector conn = null;
    private final int season;
    private final String gistId;
    private HashMap<String, JSONObject> rounds = new HashMap<>();
    private HashMap<String, JSONObject> matches = new HashMap<>();

    private APIFootballConnector(int season) {
        this.season = season;
        GistProvider prov = GistProvider.getInstance();
        this.gistId = prov.getGistID("APIFootball_" + (season + 1));
    }

    public static APIFootballConnector getAPIFootballConnectorInstance(int season) {
        if(conn == null) {
            conn = new APIFootballConnector(season);
        }
        return conn;
    }

    public APIFootballMatch getMatchDataByLeagueAndMatchID(int league, int id) {

        APIFootballMatch matchWithID = null;

        ArrayList<APIFootballMatch> allMatches = getMatchesFromLeagueFromFile(league);
        for (APIFootballMatch singleMatch : allMatches) {
            if (singleMatch.getAPIFootBallMatchID() == id) {
                matchWithID = singleMatch;
                break;
            }
        }

        return matchWithID;
    }

    public ArrayList<APIFootballMatch> getMatchDataByLeagueAndMatchday(int league, int matchday) {

        ArrayList<APIFootballMatch> matchesOfMatchday = new ArrayList<>();

        ArrayList<APIFootballMatch> allMatches = getMatchesFromLeagueFromFile(league);
        for (APIFootballMatch singleMatch : allMatches) {
            if (singleMatch.getMatchday() == matchday) {
                matchesOfMatchday.add(singleMatch);
            }
        }

        return matchesOfMatchday;
    }

    public ArrayList<APIFootballMatch> getMatchesFromLeagueFromFile(int id) {
        ArrayList<APIFootballMatch> matchesList = new ArrayList<>();

        String filename = "matches_league_" + id + ".json";
        if(!this.matches.containsKey(filename)) {
            this.matches.put(filename, readObjectFromFile(filename));
        }
        JSONObject matches = this.matches.get(filename);
        JSONArray matchArray = (JSONArray) (((JSONObject)matches.get("api")).get("fixtures"));

        for(int i = 0; i < matchArray.size(); i++) {
            matchesList.add(new APIFootballMatch((JSONObject) matchArray.get(i), this.season));
        }

        return matchesList;
    }

    public JSONObject getMatchdays(int id) {

        String filename = "rounds_" + id + ".json";
        if(!this.rounds.containsKey(filename)) {
            this.rounds.put(filename, readObjectFromFile(filename));
        }
        return this.rounds.get(filename);
    }

    public JSONObject readObjectFromFile(String filename) {

        JSONObject object = null;
        JSONParser jsonParser = new JSONParser();

        GistProvider prov = GistProvider.getInstance();
        String jsonConfig = prov.getFileFromGist(this.gistId, filename);

        try {
            object = (JSONObject) jsonParser.parse(jsonConfig);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return object;
    }

    public JSONArray readArrayFromFile(String filename) {

        JSONArray object = null;
        JSONParser jsonParser = new JSONParser();

        GistProvider prov = GistProvider.getInstance();
        String jsonConfig = prov.getFileFromGist(this.gistId, filename);

        try {
            object = (JSONArray) jsonParser.parse(jsonConfig);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return object;
    }
}