package de.jeyp91.tippliga;

import de.jeyp91.StatusHolder;
import de.jeyp91.gists.GistProvider;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
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;

public class TLWMatchesCreatorTipperLeague extends TLWMatchesCreatorBase {

    private final Logger logger = LogManager.getLogger(TLWMatchesCreatorTipperLeague.class);

    int season;
    int league;

    JSONArray matchPairingConfig;
    JSONObject tipperList;
    JSONArray tipperTeamConfig;
    ArrayList<TLWMatchday> matchdays;

    public TLWMatchesCreatorTipperLeague(int season, int league, String configFileName, ArrayList<TLWMatchday> matchdays) {
        this.season = season;
        this.league = league;
        this.matchdays = matchdays;

        try {
            JSONParser jsonParser = new JSONParser();
            GistProvider prov = GistProvider.getInstance();

            String matchPairingConfigString = prov.getTippligaBaseConfig("Tipper_Match_Pair_Config.json");
            this.matchPairingConfig = (JSONArray) jsonParser.parse(matchPairingConfigString);

            String tipperListString = prov.getTippligaConfig(season, configFileName);
            this.tipperList = (JSONObject) jsonParser.parse(tipperListString);

            String tipperTeamConfigString = prov.getTippligaBaseConfig("Tipper_Team_Config.json");
            this.tipperTeamConfig = (JSONArray) jsonParser.parse(tipperTeamConfigString);

        } catch (ParseException e) {
            e.printStackTrace();
        }

        this.publicateMatchObjects();
    }

    private void publicateMatchObjects() {

        for(Object matchdayConfig : this.matchPairingConfig) {

            int matchday = ((Long) ((JSONObject) matchdayConfig).get("matchday")).intValue();
            JSONArray matchesConfig = (JSONArray)((JSONObject) matchdayConfig).get("matches");

            for(int i = 0; i < matchesConfig.size(); i++) {
                int homeTipperNumber = ((Long) ((JSONObject) matchesConfig.get(i)).get("home")).intValue();
                int guestTipperNumber = ((Long) ((JSONObject) matchesConfig.get(i)).get("guest")).intValue();

                String homeName = this.tipperList.get(String.valueOf(homeTipperNumber)).toString();
                String guestName = this.tipperList.get(String.valueOf(guestTipperNumber)).toString();

                int teamIdHome = getTeamIdFromTipperName(homeName);
                int teamIdGuest = getTeamIdFromTipperName(guestName);

                int matchNo = (matchday - 1) * matchesConfig.size() + i + 1;

                String matchDatetime = getDeliveryDateForMatchday(matchday);

                TLWMatch tlwMatch = new TLWMatch(this.season, this.league, matchday, matchNo, teamIdHome, teamIdGuest, matchDatetime);
                this.TLWMatches.add(tlwMatch);
            }
        }
    }

    private int getTeamIdFromTipperName(String name) {
        int teamId = 0;
        for(Object config : tipperTeamConfig) {
            if (((JSONObject) config).get("team_name").toString().equals(name)) {
                teamId = ((Long) (((JSONObject) config).get("team_id"))).intValue();
            }
        }
        if(teamId == 0) {
            logger.error("ERROR! Did not find Tipper ID for " + name);
            StatusHolder.setError();
        }
        return teamId;
    }

    private String getDeliveryDateForMatchday(int matchday) {
        String deliveryDate = "";
        for (TLWMatchday matchdayObject : this.matchdays) {
            if(matchdayObject.getMatchday() == matchday) {
                deliveryDate = matchdayObject.getDeliveryDate();
            }
        }
        return deliveryDate;
    }
}
