package de.jeyp91.tippliga;

import java.sql.ResultSet;
import java.sql.SQLException;

import de.jeyp91.BaseMatch;
import de.jeyp91.StatusHolder;
import de.jeyp91.TeamIDMatcher;
import de.jeyp91.apifootball.APIFootballMatch;
import de.jeyp91.openligadb.OpenLigaDBMatch;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 *
 */
public class TLWMatch extends BaseMatch{

    private final Logger logger = LogManager.getLogger(TLWMatch.class);

    public final Integer STATUS_NOTSTARTED = 0;
    public final Integer STATUS_STARTED = 1;
    public final Integer STATUS_PROVISIONAL_RESULT_AVAILABLE = 2;
    public final Integer STATUS_FINISHED = 3;

    private Integer season = null;
    private Integer league = null;
    private Integer matchNo = null;
    private String groupId = null;
    private String formulaHome = null;
    private String formulaGuest = null;
    private Integer status = null;
    private Integer koMatch = 0;
    private Integer goalsOvertimeHome = null;
    private Integer goalsOvertimeGuest = null;
    private Integer showTable = null;
    private String trend = null;
    private Float odd1 = null;
    private Float oddX = null;
    private Float odd2 = null;
    private Float rating = null;

    public TLWMatch(ResultSet rset) {

        final int SEASON = 1;
        final int LEAGUE = 2;
        final int MATCH_NO = 3;
        final int TEAM_ID_HOME = 4;
        final int TEAM_ID_GUEST = 5;
        final int GOALS_HOME = 6;
        final int GOALS_GUEST = 7;
        final int MATCHDAY = 8;
        final int STATUS = 9;
        final int MATCH_DATETIME = 10;
        final int GROUP_ID = 11;
        final int FORMULA_HOME = 12;
        final int FORMULA_GUEST = 13;
        final int KO_MATCH = 14;
        final int GOALS_OVERTIME_HOME = 15;
        final int GOALS_OVERTIME_GUEST = 16;
        final int SHOW_TABLE = 17;
        final int TREND = 18;
        final int ODD1 = 19;
        final int ODDX = 20;
        final int ODD2 = 21;
        final int RATING = 22;

        try {
            this.season = Integer.parseInt(rset.getString(SEASON));
            this.league = Integer.parseInt(rset.getString(LEAGUE));
            this.matchNo = Integer.parseInt(rset.getString(MATCH_NO));
            this.teamIdHome = Integer.parseInt(rset.getString(TEAM_ID_HOME));
            this.teamIdGuest = Integer.parseInt(rset.getString(TEAM_ID_GUEST));
            this.goalsHome = rset.getString(GOALS_HOME).isEmpty()?null:Integer.parseInt(rset.getString(GOALS_HOME));
            this.goalsGuest = rset.getString(GOALS_GUEST).isEmpty()?null:Integer.parseInt(rset.getString(GOALS_GUEST));
            this.matchday = Integer.parseInt(rset.getString(MATCHDAY));
            this.status = Integer.parseInt(rset.getString(STATUS));
            this.matchDatetime = rset.getString(MATCH_DATETIME);
            this.groupId = rset.getString(GROUP_ID);
            this.formulaHome = rset.getString(FORMULA_HOME);
            this.formulaGuest = rset.getString(FORMULA_GUEST);
            this.koMatch = Integer.parseInt(rset.getString(KO_MATCH));
            this.goalsOvertimeHome = rset.getString(GOALS_OVERTIME_HOME).isEmpty()?null:Integer.parseInt(rset.getString(GOALS_OVERTIME_HOME));
            this.goalsOvertimeGuest = rset.getString(GOALS_OVERTIME_GUEST).isEmpty()?null:Integer.parseInt(rset.getString(GOALS_OVERTIME_GUEST));
            this.showTable = Integer.parseInt(rset.getString(SHOW_TABLE));
            this.trend = rset.getString(TREND);
            this.odd1 = Float.parseFloat(rset.getString(ODD1));
            this.oddX = Float.parseFloat(rset.getString(ODDX));
            this.odd2 = Float.parseFloat(rset.getString(ODD2));
            this.rating = Float.parseFloat(rset.getString(RATING));
        } catch (SQLException e) {
            /* TODO */
            e.printStackTrace();
        }
    }

    public TLWMatch(OpenLigaDBMatch oldbmatch, int season, int league, int matchday, int matchNo) {
        this.season = season;
        this.league = league;
        this.matchday = matchday;
        this.matchNo = matchNo;
        this.teamIdHome = TeamIDMatcher.getTippligaIdFromApiFootballId(oldbmatch.getTeamIdHome());
        this.teamIdGuest = TeamIDMatcher.getTippligaIdFromApiFootballId(oldbmatch.getTeamIdGuest());
        this.goalsHome = oldbmatch.getGoalsHome();
        this.goalsGuest = oldbmatch.getGoalsGuest();
        this.matchDatetime = oldbmatch.getMatchDateTime().replace("T", " ");
        this.groupId = "";
        this.formulaHome = "";
        this.formulaGuest = "";
        this.status = 0;
    }

    public TLWMatch(APIFootballMatch apiFootballMatch, int season, int league, int matchday, int matchNo, int status, int koMatch) {
        this.season = season;
        this.league = league;
        this.matchday = matchday;
        this.matchNo = matchNo;
        this.teamIdHome = this.getTeamIdHomeFromApiFootballMatch(apiFootballMatch);
        this.teamIdGuest = this.getTeamIdGuestFromApiFootballMatch(apiFootballMatch);
        this.goalsHome = apiFootballMatch.getGoalsHome();
        this.goalsGuest = apiFootballMatch.getGoalsGuest();
        this.matchDatetime = apiFootballMatch.getMatchDateTime().replace("T", " ").substring(0, 19);
        this.groupId = "";
        this.formulaHome = "";
        this.formulaGuest = "";
        this.status = status;
        this.koMatch = koMatch;
    }

    public TLWMatch(int season, int league, int matchday, int matchNo, String matchDatetime, int status, int koMatch) {
        this.season = season;
        this.matchday = matchday;
        this.league = league;
        this.matchNo = matchNo;
        this.status = status;
        this.koMatch = koMatch;
        this.matchDatetime = matchDatetime;
    }

    public TLWMatch(int season, int league, int matchday, int matchNo, int teamIdHome, int teamIdGuest, String matchDatetime) {
        this.season = season;
        this.matchday = matchday;
        this.league = league;
        this.matchNo = matchNo;
        this.teamIdHome = teamIdHome;
        this.teamIdGuest = teamIdGuest;
        this.matchDatetime = matchDatetime;
        this.status = 0;
    }

    public TLWMatch(TLWMatch referenceMatch) {
        this.season = referenceMatch.season;
        this.league = referenceMatch.league;
        this.matchday = referenceMatch.matchday;
        this.teamIdHome = referenceMatch.teamIdHome;
        this.teamIdGuest = referenceMatch.teamIdGuest;
        this.goalsHome = referenceMatch.goalsHome;
        this.goalsGuest = referenceMatch.goalsGuest;
        this.matchDatetime = referenceMatch.matchDatetime;
        this.matchNo = referenceMatch.matchNo;
        this.groupId = referenceMatch.groupId;
        this.formulaHome = referenceMatch.formulaHome;
        this.formulaGuest = referenceMatch.formulaGuest;
        this.status = referenceMatch.status;
        this.koMatch = referenceMatch.koMatch;
        this.goalsOvertimeHome = referenceMatch.goalsOvertimeHome;
        this.goalsOvertimeGuest = referenceMatch.goalsOvertimeGuest;
        this.showTable = referenceMatch.showTable;
        this.trend = referenceMatch.trend;
        this.odd1 = referenceMatch.odd1;
        this.oddX = referenceMatch.oddX;
        this.odd2 = referenceMatch.odd2;
        this.rating = referenceMatch.rating;
    }

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

    public Integer getLeague() {
        return this.league;
    }

    public Integer getMatchNo() {
        return this.matchNo;
    }

    public Integer getStatus() {
        return this.status;
    }

    public Integer getKoMatch() {
        return this.koMatch;
    }

    public String getGroupId() {
        return this.groupId;
    }

    public String getFormulaHome() {
        String formula = "'D'";
        if(this.teamIdHome != null) {
            return "''";
        }
        else if (this.formulaHome != null) {
            formula = this.formulaHome;
        }
        return formula;
    }

    public String getFormulaGuest() {
        String formula = "'D'";
        if(this.teamIdGuest != null) {
            return "''";
        }
        else if (this.formulaGuest != null) {
            formula = this.formulaGuest;
        }
        return formula;
    }

    public String getSQLQueryInsert() {
        return "INSERT INTO phpbb_footb_matches VALUES (" +
                getSQLQueryValues()
                + ");";
    }

    public String getSQLQueryReplace() {
        return "REPLACE INTO phpbb_footb_matches VALUES (" +
                getSQLQueryValues()
                + ");";
    }

    public String getSQLQueryValues() {
        return this.season + ", " +
                this.league + ", " +
                this.matchNo + ", " +
                nullToSqlEmptyString(this.teamIdHome) + ", " +
                nullToSqlEmptyString(this.teamIdGuest) + ", " +
                nullToSqlEmptyString(this.goalsHome) + ", " +
                nullToSqlEmptyString(this.goalsGuest) + ", " +
                this.matchday + ", " +
                this.status + ", " +
                nullToSqlEmptyString(this.matchDatetime) + ", " +
                nullToSqlEmptyString(this.groupId) + ", " +
                getFormulaHome() + ", " +
                getFormulaGuest() + ", " +
                nullToSqlEmptyString(this.koMatch) + ", " +
                nullToSqlEmptyString(this.goalsOvertimeHome) + ", " +
                nullToSqlEmptyString(this.goalsOvertimeGuest) + ", " +
                nullToSqlEmptyString(this.showTable) + ", " +
                "'0.00','0.00','0.00','0.00'";
    }

    private String nullToSqlEmptyString(Integer number) {
        return number != null ? number.toString() : "''";
    }

    private String nullToSqlEmptyString(String string) {
        return string != null ? "'"+string+"'" : "''";
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public void setMatchDateTime(String matchDateTime) {
        this.matchDatetime = matchDateTime;
    }

    public void updateMatch(APIFootballMatch apiFootballMatch) {
        this.teamIdHome = TeamIDMatcher.getTippligaIdFromApiFootballId(apiFootballMatch.getTeamIdHome());
        this.teamIdGuest = TeamIDMatcher.getTippligaIdFromApiFootballId(apiFootballMatch.getTeamIdGuest());
        this.goalsHome = apiFootballMatch.getGoalsHome();
        this.goalsGuest = apiFootballMatch.getGoalsGuest();
        this.matchDatetime = apiFootballMatch.getMatchDateTime().replace("T", " ").substring(0, 19);
    }

    private Integer getTeamIdHomeFromApiFootballMatch(APIFootballMatch match) {
        Integer id = TeamIDMatcher.getTippligaIdFromApiFootballId(match.getTeamIdHome());
        if(id == null) {
            logger.error("Could not find id " + match.getTeamIdHome() + " for " + match.getTeamNameHome());
            StatusHolder.setError();
        }
        return id;
    }

    private Integer getTeamIdGuestFromApiFootballMatch(APIFootballMatch match) {
        Integer id = TeamIDMatcher.getTippligaIdFromApiFootballId(match.getTeamIdGuest());
        if(id == null) {
            logger.error("Could not find id " + match.getTeamIdGuest() + " for " + match.getTeamNameGuest());
            StatusHolder.setError();
        }
        return id;
    }
}