package objects.bets;

import objects.SoccerMatch;

public class Bet {

    public static enum Status {
        DONE,
        OPEN,
        LOST,
        COVERED,
        SPLIT
    }

    SoccerMatch match;
    float betAmount;

    float betOdds;
    int id;
    String homeTeamName;
    String awayTeamName;
    String matchup;
    int homeScore;
    int awayScore;
    int coveredBetId;
    String bet;

    int betCoveredNumber;
    Status status;

    public boolean resolved = false;

    public Bet(int id, SoccerMatch match, String bet, float betAmount, float betOdds) {
        this(id, match, bet, betAmount, betOdds, Status.OPEN, -1);
    }

    public Bet(int id, SoccerMatch match, String bet, float betAmount, float betOdds, Status status, int coveredBetId) {
        this.betAmount = betAmount;
        this.betOdds = betOdds;
        this.match = match;
        this.bet = bet;

        homeTeamName = match.getHomeTeam().getTeamName();
        awayTeamName = match.getAwayTeam().getTeamName();

        setMatchup();
        homeScore = match.getHomeScore();
        awayScore = match.getAwayScore();
        this.status = status;
        this.id = id;
        this.coveredBetId = coveredBetId;
    }

    public boolean correctBet() {
        boolean result = false;
        if (isBetOnHomeTeam() && match.getHomeScore() > match.getAwayScore()) {
            result = true;
        }
        if (isBetOnDraw() && match.getHomeScore() == match.getAwayScore()) {
            result = true;
        }
        if (isBetOnAwayTeam() && match.getHomeScore() < match.getAwayScore()) {
            result = true;
        }
        return result;
    }

    public int getAwayScore() {
        return awayScore;
    }

    public String getAwayTeamName() {
        return awayTeamName;
    }

    public String getBet() {
        return bet;
    }

    public float getBetAmount() {
        return betAmount;
    }

    public int getBetCoveredNumber() {
        return betCoveredNumber;
    }

    public float getBetOdds() {
        return betOdds;
    }

    public int getCoveredBetId() {
        return coveredBetId;
    }

    public int getHomeScore() {
        return homeScore;
    }

    public String getHomeTeamName() {
        return homeTeamName;
    }

    public int getId() {
        return id;
    }

    public SoccerMatch getMatch() {
        return match;
    }

    public String getMatchup() {
        return matchup;
    }

    public float getResult() {
        final float result;
        if ((isBetOnHomeTeam() && match.getHomeScore() > match.getAwayScore())
                || (isBetOnDraw() && match.getHomeScore() == match.getAwayScore())
                || (isBetOnAwayTeam() && match.getHomeScore() < match.getAwayScore())) {
            result = betOdds * betAmount;
        } else {
            result = 0;
        }

        return result;
    }

    public Status getStatus() {
        return status;
    }

    public boolean isBetOnAwayTeam() {
        return bet.equals("2");
    }

    public boolean isBetOnDraw() {
        return bet.equals("X");
    }

    public boolean isBetOnHomeTeam() {
        return bet.equals("1");
    }

    public boolean isResolved() {
        return resolved;
    }

    public void setAwayScore(int awayScore) {
        this.awayScore = awayScore;
    }

    public void setAwayTeamName(String awayTeamName) {
        this.awayTeamName = awayTeamName;
    }

    public void setBet(String bet) {
        this.bet = bet;
    }

    public void setBetAmount(float betAmount) {
        this.betAmount = betAmount;
    }

    public void setBetCoveredNumber(int betCoverNumber) {
        this.betCoveredNumber = betCoverNumber;
    }

    public void setBetOdds(float betOdds) {
        this.betOdds = betOdds;
    }

    public void setBetOnAwayTeam() {
        this.bet = "2";
    }

    public void setBetOnDraw() {
        this.bet = "X";
    }

    public void setBetOnHomeTeam() {
        this.bet = "1";
    }

    public void setCoveredBetId(int coveredBetId) {
        this.coveredBetId = coveredBetId;
    }

    public void setHomeScore(int homeScore) {
        this.homeScore = homeScore;
    }

    public void setHomeTeamName(String homeTeamName) {
        this.homeTeamName = homeTeamName;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setMatch(SoccerMatch match) {
        this.match = match;
    }

    public void setMatchup() {
        this.matchup = homeTeamName + " - " + awayTeamName;
    }

    public void setResolved(boolean resolved) {
        this.resolved = resolved;
    }

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

    protected boolean isDrawResult() {
        return match.getHomeScore() == match.getAwayScore();
    }

}
