package domain;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Arno Schutijzer
 * @author Sam Den Haerinck
 */
public class Player {

    private Game game;
    private List<Card> cards;
    private boolean hasSplit = false;
    private boolean hasQuit = false;
    private boolean isBust = false;
    private Double maxCred, credit, minCred;

    public Player(Double credit) {
        this.game = game;
        cards = new ArrayList<>();
        this.credit = credit;
        maxCred = credit;
        minCred = credit;
    }

    public List<Card> getCards() {
        return cards;
    }

    public void setGame(Game g) {
        this.game = g;
    }

    private void doubleDown(List<Card> cards) {
        game.addBet(200.);
        cards.add(game.drawCard());
    }

    public void addCard(List<Card> cards, Card card) {
        cards.add(card);
    }

    private void updateBust(List<Card> cards) {
        isBust = game.getHandVal(cards) > 21;
    }

    public boolean getBust() {
        return this.isBust;
    }

    public boolean hasQuit() {
        return hasQuit;
    }

    public void addCredit(Double credit) {
        this.credit += credit;
        
        if(this.credit > maxCred){
            maxCred = this.credit;
        }
        
        if(this.credit < minCred){
            minCred = this.credit;
        }
    }

    public Double getCredit() {
        return this.credit;
    }

    public void clearCards() {
        cards = new ArrayList<>();
    }

    /**
     *
     * @return String representatie van een player-object.
     */
    public String toString() {

        String retValue = "Player ";
        if (hasQuit) {
            retValue += " has quit with ";
        } else if (isBust) {
            retValue += " is bust with ";
        }

        retValue += "the following hand:\n";

        for (Card c : cards) {
            retValue += "\t- " + c;
        }
        
        retValue+="maxCred: "+maxCred+", minCred: "+minCred+", credits: "+credit;
        return retValue;
    }

    private boolean hasAce(List<Card> cards) {

        return cards.stream().anyMatch((c) -> (c.getValue().equals("Ace")));
    }

    /**
     * De speler maakt een beslissing, gegeven de eigen kaarten en de gekende
     * kaart van de dealer.
     *
     * @param dealersCard kaart van de dealer
     */
    public void makeDecision(Card dealersCard) {

        updateBust(cards);

        int handVal = game.getHandVal(cards);
        
        if (handVal >= 17) {
            hasQuit = true;
            return;
        }

        if (!hasQuit) {
            if (!isBust) {
                if (dealersCard.getCardVal() <= 6) {
                    if (handVal == 12 && dealersCard.getCardVal() <= 3) {
                        addCard(cards, game.drawCard());
                    } else if (handVal > 12) {
                        hasQuit = true;
                    }
                } else {
                    addCard(cards, game.drawCard());
                }
            }
        }

        if (handVal == 11) {
            doubleDown(cards);
        } else if (handVal == 10 && dealersCard.getCardVal() <= 9) {
            doubleDown(cards);
        } else {
            addCard(cards, game.drawCard());
        }

        if (hasAce(cards)) {
            if (handVal >= 19) {
                hasQuit = true;
                return;
            } else if (handVal <= 18 && dealersCard.getCardVal() >= 4 && dealersCard.getCardVal() <= 6) {
                doubleDown(cards);
            } else if (handVal == 18) {
                if (dealersCard.getCardVal() <= 3) {
                    hasQuit = true;
                    return;
                } else if (dealersCard.getCardVal() >= 7 && dealersCard.getCardVal() <= 8) {
                    hasQuit = true;
                } else {
                    addCard(cards, game.drawCard());
                }
            }//heeft handwaarde van 18

        }//heeft aas
    }//is niet bust

}
