package de.jeyp91.tippliga;

import org.json.simple.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;

public class TLWMatchdaysUpdater {

    int season;
    int league;
    ArrayList<TLWMatchday> matchdaysOriginal;
    ArrayList<TLWMatchday> matchdaysUpdated;

    public TLWMatchdaysUpdater(int season, int league, String configPath) throws ParseException {
        this.season = season;
        this.league = league;

        TippligaSQLConnector conn = new TippligaSQLConnector();
        this.matchdaysOriginal = conn.getMatchdays(String.valueOf(season), String.valueOf(league));
        this.matchdaysOriginal.sort(Comparator.comparing(TLWMatchday::getMatchday));

        TLWMatchdaysCreator creator = new TLWMatchdaysCreator(season, league, configPath);
        this.matchdaysUpdated = creator.getMatchdays();
        this.matchdaysUpdated.sort(Comparator.comparing(TLWMatchday::getMatchday));
    }

    public String getUpdateSql() throws Exception {
        String updateSql = "";

        if(this.matchdaysUpdated.size() != this.matchdaysOriginal.size()) {
            throw new Exception("Wrong matchdays config");
        }

        for(int i = 0; i < this.matchdaysUpdated.size(); i++) {
            TLWMatchday matchdayOriginal = this.matchdaysOriginal.get(i);
            TLWMatchday matchdayUpdated = this.matchdaysUpdated.get(i);

            if(matchdayOriginal.getMatchday() != matchdayUpdated.getMatchday()) {
                throw new Exception("BUUUUG!");
            }

            Date now = new Date(System.currentTimeMillis());

            Date earliestDate = null;
            try {
                earliestDate = new SimpleDateFormat("yyyy-MM-dd").parse(matchdayUpdated.getDeliveryDate().substring(0, 10));
            } catch (java.text.ParseException e) {
                e.printStackTrace();
            }

            if(earliestDate.after(now)) {

                String updateStart = "UPDATE phpbb_footb_matchdays SET ";
                String condition = "WHERE season = " + matchdayOriginal.getSeason() + " " +
                        "AND league = " + matchdayOriginal.getLeague() + " " +
                        "AND matchday = " + matchdayOriginal.getMatchday() + ";\n";

                if (!matchdayOriginal.getDeliveryDate().equals(matchdayUpdated.getDeliveryDate())) {
                    updateSql += updateStart +
                            "delivery_date = '" + matchdayUpdated.getDeliveryDate() + "' " +
                            condition;
                }

                if (!matchdayOriginal.getDeliveryDate2().equals(matchdayUpdated.getDeliveryDate2())) {
                    updateSql += updateStart +
                            "delivery_date_2 = '" + matchdayUpdated.getDeliveryDate2() + "' " +
                            condition;
                }

                if (!matchdayOriginal.getMatchdayName().equals(matchdayUpdated.getMatchdayName())) {
                    updateSql += updateStart +
                            "matchday_name = '" + matchdayUpdated.getMatchdayName() + "' " +
                            condition;
                }
            }
        }

        return updateSql;
    }
}
