import random
import rich
import sys

game = {"draw_pile": [],
        "play_pile": [],
        "hand0": [],
        "hand1": [],
        "hand2": [],
        "hand3": [],
        "name0": "You",
        "name1": "Jakayla",
        "name2": "Habiba",
        "name3": "Abby",
        "current_player": 0,
        "direction": "clockwise",
        "wild_color": None}

# a color is either
# "red", "blue", "green", or "yellow"

# a card will be either
# - a pair like ("green", 8)
# - "wild", or
# - "draw4"

# Let's build a deck of numbered cards.

def advance_player():
    if game["direction"]=="clockwise":
        if game["current_player"]==0:
            game["current_player"]=1
        elif game["current_player"]==1:
            game["current_player"]=2
        elif game["current_player"]==2:
            game["current_player"]=3
        elif game["current_player"]==3:
            game["current_player"]=0
    else:
        if game["current_player"]==0:
            game["current_player"]=3
        elif game["current_player"]==3:
            game["current_player"]=2
        elif game["current_player"]==2:
            game["current_player"]=1
        elif game["current_player"]==1:
            game["current_player"]=0

def new_deck():
    deck = []
    for color in ["red","green","blue","yellow"]:
        for i in range(10):
            if i==0:
                deck.append((color,i))
            else:
                deck.append((color, i))
                deck.append((color, i))
    return deck

def print_card(card):
    (color, rank) = card
    rich.print("["+color+"]["+str(rank)+"][/]",end="")

def deal():
    for i in range(7):
        game["hand0"].append(game["draw_pile"].pop())
        game["hand1"].append(game["draw_pile"].pop())
        game["hand2"].append(game["draw_pile"].pop())
        game["hand3"].append(game["draw_pile"].pop())

deck = new_deck()
random.shuffle(deck)
game["draw_pile"] = deck

deal()
game["play_pile"].append(game["draw_pile"].pop())

def can_play_card(top_card, card):
    (top_color, top_rank) = top_card
    (card_color, card_rank) = card
    if top_color==card_color:
        return True
    elif top_rank==card_rank:
        return True
    else:
        return False

# build list of cards that can be played on top card
def playable(top_card, hand):
    result = []
    for card in hand:
        if can_play_card(top_card, card):
            result.append(card)
    return result

def show_table():
    print("top card")
    top_card = game["play_pile"][-1] # bottom card of play pile
    print_card(top_card)
    print()
    for i in [0,1,2,3]:
        print(game["name"+str(i)])
        hand = game["hand"+str(i)]
        if i==0:
            for card in hand:
                print_card(card)
        else:
            print(len(hand),"cards")
        print()
        # print("playable")
        # for card in playable(top_card, hand):
        #     print_card(card)
        # print()
        print()
    print("current player", game["current_player"])
    print("direction", game["direction"])
    print("wild color", game["wild_color"])

def player_turn():
    print("top card")
    top_card = game["play_pile"][-1]
    print_card(top_card)
    print()
    i = 0
    hand = game["hand0"]
    for card in hand:
        print(i,end=" ")
        print_card(card)
        print()
        i += 1
    print("What would you like to do?")
    print("Type a number to play a card.")
    print("(d) Draw.")
    print("(q) Quit.")
    choice = input(">> ")
    if choice=="q":
        print("Bye!")
        sys.exit()
    elif choice=="d":
        print("You chose to draw a card.")
        game["hand0"].append(game["draw_pile"].pop())
    else:
        chosen_card = int(choice)
        card = hand[chosen_card]
        print("You chose to play")
        print_card(card)
        print()
        if can_play_card(top_card, card):
            print("Good choice!")
            game["hand0"].remove(card)
            game["play_pile"].append(card)
        else:
            print("You can't play that card.")
            player_turn()
            return
    advance_player()
    show_table()

    #else:
    #    print("I didn't understand your choice.")
    #    player_turn()

def computer_turn():
    cp = game["current_player"]
    name = game["name"+str(cp)]
    hand = game["hand"+str(cp)]
    top_card = game["play_pile"][-1]
    choices = playable(top_card,hand)
    card = random.choice(choices) # randomly choose a playable card
    print(name,"chose")
    print_card(card)
    print()
    game["hand"+str(cp)].remove(card)
    game["play_pile"].append(card)
    advance_player()

show_table()
player_turn()

show_table()
computer_turn()

show_table()
computer_turn()

show_table()
computer_turn()

show_table()