<reponame>PhilippOesch/open_spiel
import classes.Card as Card
import classes.Cards as Cards
from termcolor import colored
from globals import GameType


class Player:

    def __init__(self, _idx_player: int, _game: GameType):
        self.idx_player = _idx_player
        self.game = _game
        self.cards = Cards.Cards()
        self.blinds = Cards.Cards()
        self.throw_aways = Cards.Cards()
        self.card_amount = 11
        self.score: float = 0

    def __str__(self):
        return "Player(" + str(self.idx_player) + ") \n" + \
            "Cards: " + str(self.cards) + "\n" + \
            "Score: " + str(self.score)

    def receive_card(self, _card):
        self.cards.add_card(_card)

    def update_score(self, _score):
        self.score += _score

    def get_score(self):
        return self.score

    def print_cards(self):
        print("Cards Player(" + str(self.idx_player) + "): " +
              self.cards.to_string())

    def print_blinds(self):
        if len(self.blinds.get_cards()) == 0:
            print("No blind received")
        else:
            print("Blinds Player(" + str(self.idx_player) + "): " +
                  self.blinds.to_string())

    def receive_blind(self, _card):
        self.blinds.add_card(_card)

    def take_blinds_to_hand(self):
        self.blinds.sort_by_Rank_descending()  # Sort by Rank descending
        self.cards.sort_by_Rank_descending()  # Sort by Rank descending
        print("Blinds: " + str(self.blinds))  # Print blinds

        sorted_blinds = self.blinds.get_cards()  # Get blinds
        sorted_cards = self.cards.get_cards()  # Get cards

        # take the blinds and throw away the worst card + take the 2 best cards from the hand cards
        new_deck_array = sorted_blinds[:-1] + sorted_cards[0:2]
        self.cards = Cards.Cards(new_deck_array)
        print("New Deck:", str(self.cards))

        # the opposite are the throw away cards
        throw_away_array = sorted_cards[1:]
        throw_away_array.append(sorted_blinds[-1])
        self.throw_aways = Cards.Cards(throw_away_array)
        print("Throw aways:", str(self.throw_aways))

        # the throw away cards are the save points
        self.score = self.throw_aways.get_value()

    def prompt_play_card(self, _trick):
        print("Current Trick: " + str(_trick))
        trick_deck = _trick.get_cards()  # get current cards in trick

        # if no card is in the trick, the player can play any card
        if len(trick_deck.get_cards()) == 0:
            legal_cards = self.cards

        # if there is a card in the trick, it has to be decides, which card the player can currently play
        else:
            suit = _trick.get_suit()
            legal_cards = self.cards.get_valid_responses(suit)

        print("Legal Cards: " + str(legal_cards))

        # prompt the player to play a card
        card_to_play_idx = input(
            colored("Type Index of Card to play: ", "cyan"))
        while not card_to_play_idx.isdigit() or int(card_to_play_idx) > len(legal_cards.get_cards()) or int(card_to_play_idx) < 0:
            card_to_play_idx = input(colored(
                                     "Something went wrong, please try again: ", "cyan"))

        # the card from the legal cards deck
        card_to_play = legal_cards.get_cards()[int(card_to_play_idx)]
        print("Card_played:", str(card_to_play))

        # add the card to the trick and remove it from the player's hand
        _trick.add_card(Card.CardPlayed(
            card=card_to_play, _player=self.idx_player))
        self.cards.remove_card(card_to_play)
        print("------------------------------------------------------")
