package de.jeyp91.tippliga;

import com.google.api.client.util.DateTime;
import de.jeyp91.apifootball.APIFootballMatch;
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.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Date;

public class TLWMatchesManagerBase {

    protected int season;
    protected int league;
    protected int numberOfMatchdays;
    protected int matchesPerMatchday;
    protected int ko;

    protected JSONArray matchdayConfig;

    protected void initConfigParamsFromFile(String configFileName) {

        GistProvider prov = GistProvider.getInstance();
        String jsonConfig = prov.getTippligaConfig(season, configFileName);
        try {
            JSONParser jsonParser = new JSONParser();
            //Read JSON file
            JSONObject config = (JSONObject) jsonParser.parse(jsonConfig);
            this.numberOfMatchdays = ((Long) config.get("numberOfMatchdays")).intValue();
            this.matchdayConfig = (JSONArray) config.get("matchdayConfig");
            this.ko = ((Long) config.get("ko")).intValue();

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

    public static ArrayList<TLWMatch> getMatchesStartingFromSecondDay(ArrayList<TLWMatch> matches) {
        LocalDateTime firstMatchtime = getFirstMatchtimeTLW(matches);
        ArrayList<TLWMatch> filteredMatches = new ArrayList<>();
        for(TLWMatch match : matches) {
            LocalDateTime matchDateTime = getDate(match);
            if(getDaysDifference(matchDateTime, firstMatchtime) != 0) {
                filteredMatches.add(match);
            }
        }
        return filteredMatches;
    }

    public static LocalDateTime getFirstMatchtimeTLW(ArrayList<TLWMatch> matches) {
        LocalDateTime matchtime = null;
        for(TLWMatch match : matches) {
            LocalDateTime matchtimeTemp = getDate(match);
            if(matchtime == null) {
                matchtime = matchtimeTemp;
            } else {
                if(matchtimeTemp.isBefore(matchtime)) {
                    matchtime = matchtimeTemp;
                }
            }
        }
        return matchtime;
    }

    public static LocalDateTime getFirstMatchtimeAPIFootball(ArrayList<APIFootballMatch> matches) {
        LocalDateTime matchtime = null;
        for(APIFootballMatch match : matches) {
            LocalDateTime matchtimeTemp = getDate(match);
            if(matchtime == null) {
                matchtime = matchtimeTemp;
            } else {
                if(matchtimeTemp.isBefore(matchtime)) {
                    matchtime = matchtimeTemp;
                }
            }
        }
        return matchtime;
    }

    public static LocalDateTime getDate(TLWMatch match) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime dateTime = LocalDateTime.parse(match.getMatchDateTime(), formatter);
        return dateTime;
    }

    public static LocalDateTime getDate(APIFootballMatch match) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
        LocalDateTime dateTime = LocalDateTime.parse(match.getMatchDateTime().substring(0, 19), formatter);
        return dateTime;
    }

    public static int getDaysDifference(LocalDateTime date1, LocalDateTime date2) {
        date1 = date1.toLocalDate().atTime(0, 0, 0);
        date2 = date2.toLocalDate().atTime(0, 0, 0);
        return (int) date1.until(date2, ChronoUnit.DAYS);
    }
}
