package de.jeyp91.tippliga;

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.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;

public class TLWMatchdaysCreator {

    int season;
    int league;
    ArrayList<TLWMatch> matches;
    int matchesPerMatchday;
    JSONObject configObject;

    public TLWMatchdaysCreator(int season, int league, String configPath){
        this.season = season;
        this.league = league;

        TLWMatchesCreatorFootball matchesCreator = new TLWMatchesCreatorFootball(2021, 1, configPath);
        this.matches = matchesCreator.getMatches();

        GistProvider prov = GistProvider.getInstance();
        String jsonConfig = prov.getTippligaConfig(season, configPath);

        JSONParser jsonParser = new JSONParser();
        try {
            this.configObject = (JSONObject) jsonParser.parse(jsonConfig);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //Read JSON file
        this.matchesPerMatchday = ((Long) this.configObject.get("matchesPerMatchday")).intValue();
    }

    public ArrayList<TLWMatchday> getMatchdays() {
        ArrayList<TLWMatchday> matchdays = new ArrayList<>();

        JSONArray matchdaysConfig = (JSONArray) this.configObject.get("matchdayConfig");
        for (Object matchdayConfig : matchdaysConfig) {
            JSONObject matchdayConfigJson = (JSONObject) matchdayConfig;

            int matchdayNumber = Integer.parseInt(matchdayConfigJson.get("TLWMatchday").toString());
            String matchdayName = matchdayConfigJson.containsKey("matchdayName") ? matchdayConfigJson.get("matchdayName").toString() : "";

            int numberOfMatches = this.matchesPerMatchday;
            if(numberOfMatches == 0){
                numberOfMatches = Integer.parseInt(matchdayConfigJson.get("numberOfMatches").toString());
            }

            String deliveryDate1;
            String deliveryDate2 = null;

            JSONArray matchesConfig = (JSONArray) matchdayConfigJson.get("matchesConfig");
            JSONObject matchesConfigOne = (JSONObject) matchesConfig.get(0);
            if(matchesConfigOne.get("type").toString().equals("ToBeDefined")) {
                deliveryDate1 = matchesConfigOne.get("placeholderDatetime").toString();
            }
            else {
                ArrayList<TLWMatch> matchesOfMatchday = getMatchesForMatchday(matches, matchdayNumber);

                LocalDateTime firstMatchDate = TLWMatchesManagerBase.getFirstMatchtimeTLW(matchesOfMatchday);
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                deliveryDate1 = firstMatchDate.format(formatter);

                ArrayList<TLWMatch> remainingMatches = TLWMatchesManagerBase.getMatchesStartingFromSecondDay(matchesOfMatchday);
                if(remainingMatches.size() > 0) {
                    firstMatchDate = TLWMatchesManagerBase.getFirstMatchtimeTLW(remainingMatches);
                    deliveryDate2 = firstMatchDate.format(formatter);
                }
            }

            TLWMatchday matchday = new TLWMatchday(this.season, this.league, matchdayNumber, 0, deliveryDate1, deliveryDate2, "", matchdayName, numberOfMatches);
            matchdays.add(matchday);
        }

        return matchdays;
    }

    public String getMatchdaysSQL() {
        String matchdaySql = "";
        for (TLWMatchday matchday : getMatchdays()) {
            matchdaySql += "REPLACE INTO phpbb_footb_matchdays VALUES('";
            matchdaySql += matchday.getSeason().toString();
            matchdaySql += "', '";
            matchdaySql += matchday.getLeague().toString();
            matchdaySql += "', '";
            matchdaySql += matchday.getMatchday().toString();
            matchdaySql += "', '";
            matchdaySql += matchday.getStatus().toString();
            matchdaySql += "', '";
            matchdaySql += matchday.getDeliveryDate();
            matchdaySql += "', '";
            matchdaySql += matchday.getDeliveryDate2();
            matchdaySql += "', '";
            matchdaySql += matchday.getDeliveryDate3();
            matchdaySql += "', '";
            matchdaySql += matchday.getMatchdayName();
            matchdaySql += "', '";
            matchdaySql += matchday.getMatches().toString();
            matchdaySql += "');\n";
        }
        return matchdaySql;
    }

    private ArrayList<TLWMatch> getMatchesForMatchday(ArrayList<TLWMatch> matches, int matchday) {
        ArrayList<TLWMatch> matchesOfMatchday = new ArrayList<>();
        for(TLWMatch match : matches) {
            if(match.getMatchday() == matchday) {
                matchesOfMatchday.add(match);
            }
        }
        return matchesOfMatchday;
    }

    private String getMatchdayNameFromConfig(int matchday) {
        String matchdayName = "";
        JSONArray matchdaysConfig = (JSONArray) this.configObject.get("matchdayConfig");
        for (Object matchdayConfig : matchdaysConfig) {
            if(((JSONObject) matchdayConfig).get("TLWMatchday").toString().equals(String.valueOf(matchday))
            && ((JSONObject) matchdayConfig).containsKey("matchdayName")) {
                matchdayName = ((JSONObject) matchdayConfig).get("matchdayName").toString();
            }
        }
        return matchdayName;
    }
}
