package de.jeyp91.tippliga;

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 TLWMatchesCreatorTipperPokal extends TLWMatchesCreatorBase{

    Logger logger = LogManager.getLogger(TLWMatchesCreatorTipperPokal.class);

    int season;
    int league;

    ArrayList<TLWMatch> TLWMatches;
    JSONObject tipperList;
    JSONArray tipperTeamConfig;
    ArrayList<TLWMatchday> matchdays;

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


        GistProvider prov = GistProvider.getInstance();

        this.TLWMatches = new ArrayList<>();

        try {
            JSONParser jsonParser = new JSONParser();

            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() {

        int matchday = 1;

        for(int matchNo = 1; matchNo < 13; matchNo++) {

            String homeName = this.tipperList.get(String.valueOf(2 * matchNo - 1)).toString();
            String guestName = this.tipperList.get(String.valueOf(2 * matchNo)).toString();

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

            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("Did not find Tipper ID for " + name);
        }
        return teamId;
    }

    public ArrayList<TLWMatch> getMatches() {
        return this.TLWMatches;
    }

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