package de.jeyp91.openligadb;

import de.jeyp91.BaseMatch;
import de.jeyp91.tippliga.TLWMatch;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import java.util.regex.*;

/**
 *
 */
public class OpenLigaDBMatch extends BaseMatch {

    private final int RESULT_TYPE_ENDRESULT = 2;

    private Integer season = null;
    private int matchId;
    private int leagueId;
    private boolean matchIsFinished;

    public OpenLigaDBMatch(JSONObject json) {
        this.matchId = Integer.parseInt(json.get("MatchID").toString());
        this.season = getSeasonFromLeageName(json.get("LeagueName").toString());
        this.leagueId = Integer.parseInt(json.get("LeagueId").toString());
        this.teamIdHome = Integer.parseInt(((JSONObject) json.get("Team1")).get("TeamId").toString());
        this.teamIdGuest = Integer.parseInt(((JSONObject) json.get("Team2")).get("TeamId").toString());
        for(int i = 0; i < ((JSONArray) json.get("MatchResults")).size(); i++) {
            if(Integer.parseInt((((JSONObject) ((JSONArray) json.get("MatchResults")).get(i)).get("ResultTypeID")).toString()) == RESULT_TYPE_ENDRESULT) {
                this.goalsHome = Integer.parseInt(((JSONObject) ((JSONArray) json.get("MatchResults")).get(i)).get("PointsTeam1").toString());
                this.goalsGuest = Integer.parseInt(((JSONObject) ((JSONArray) json.get("MatchResults")).get(i)).get("PointsTeam2").toString());
            }
        }
        this.matchday = Integer.parseInt(((JSONObject) json.get("Group")).get("GroupOrderID").toString());
        this.matchIsFinished = (Boolean) json.get("MatchIsFinished");
        this.matchDatetime = (String) json.get("MatchDateTime");
    }

    private int getSeasonFromLeageName(String leagueName) {
        Pattern p = Pattern.compile("\\d*/\\d*");
        Matcher m = p.matcher(leagueName);
        m.find();
        String seasonString = m.group();
        p = Pattern.compile("\\d+");
        m = p.matcher(seasonString);
        Integer season = null;
        while(m.find()) {
            season = Integer.parseInt(m.group());
        }
        return season < 100 ? season + 2000 : season;
    }

    public boolean isSameMatch(TLWMatch compareMatch) {
        if(this.getSeason()!= compareMatch.getSeason()) {
            return false;
        }
        if(this.getMatchday() != compareMatch.getMatchday()) {
            return false;
        }
        if(this.getTeamIdHome() != compareMatch.getTeamIdHome()) {
            return false;
        }
        if(this.getTeamIdGuest() != compareMatch.getTeamIdGuest()) {
            return false;
        }
        String thisDateTime = this.getMatchDateTime().replace("T", " ");
        String tempDateTime = compareMatch.getMatchDateTime().replace("T", " ");
        if(!tempDateTime.equals(thisDateTime)) {
            return false;
        }
        if(this.goalsHome != compareMatch.getGoalsHome() ||
        this.goalsGuest != compareMatch.getGoalsGuest()) {
            return false;
        }
        return true;
    }

    public Integer getSeason() {
        return this.season;
    }

    public Integer getMatchId() {
        return this.matchId;
    }

    public boolean getMatchIsFinished() {
        return this.matchIsFinished;
    }
}