# something close to blackjack

import playing_cards

# example: the_hand = a list of Card objects
def handvalue(the_hand):
    total = 0
    ace_exists = False
    for acard in the_hand:
        # acard is of type Card
        total += acard.get_value()
        if acard.get_rank() == 1:
            ace_exists = True

    if ace_exists:
        if total <= 11:
            total += 10   # we are counting the ace as 11 not 1 here
        
    return total
    
# example: the_hand = a list of Card objects
def display_hand(the_hand, name_of_hand='Player'):
    print(f'{name_of_hand} Hand is: ')
    for acard in the_hand:
        print(f'{str(acard)}')
        
    hv = handvalue(the_hand)
    print(f'Value of {name_of_hand} hand is: {hv}')


def deal_a_hand():
    thedeck = playing_cards.Deck()
    thedeck.shuffle()
    thedeck.cut()
    
    player_hand = []
    dealer_hand = []

    # deal a card from thedeck and put it in player_hand
    # then next card dealt goes in dealer_hand
    # then next card dealt into player_hand
    # then next card into dealer_hand

    player_hand.append(thedeck.deal())
    dealer_hand.append(thedeck.deal())
    player_hand.append(thedeck.deal())
    dealer_hand.append(thedeck.deal())
    
    print('--------------------------')
    print('Dealer up card is ' + str(dealer_hand[1]))
    
    # player_hand has 2 cards
    display_hand(player_hand)
    finish_dealer_hand = False
    while True:
        hv = handvalue(player_hand)
        if hv == 21:
            print('Congratulations your hand is 21.')
            finish_dealer_hand = True
            break
        elif hv > 21:
            print('Bust')
            break
        userchoice = input('Hit or Stay')
        print()
        if userchoice == 'Hit' or userchoice == 'hit':
            player_hand.append(thedeck.deal())
            display_hand(player_hand)
        else:
            # assume they typed Stay
            print('OK, your final hand is:')
            display_hand(player_hand)
            print()
            finish_dealer_hand = True
            break

    if finish_dealer_hand:
        # implement the rules that dealer must hit on <= 16 and stay on 17 or higher
        # dealer_hand currently contains 2 cards
        dealer_hv = handvalue(dealer_hand)
        while dealer_hv <= 16:
            # hit
            dealer_hand.append(thedeck.deal())
            # recompute dealer_hv
            dealer_hv = handvalue(dealer_hand)

        
        display_hand(dealer_hand, name_of_hand='Dealer')
        
        # we get here when? dealer_hv > 16
        if dealer_hv > 21:
            print('Dealer busts')
        elif dealer_hv > hv:
            print(f'Dealer wins with {dealer_hv} against player\'s {hv}.')
        elif dealer_hv == hv:
            print('Tie')
        else:
            print(f'Player wins with {hv} against dealer\'s {dealer_hv}.')
        
            
        
user_choice = ''
while user_choice != 'q':
    deal_a_hand()
    user_choice = input('Enter q to quit or anything else to continue')
    
