import random
from typing import List, Dict, Tuple

MAX_LINES = 3
MAX_BET = 100
MIN_BET = 1
ROWS = 3
COLS = 3

SYMBOL_COUNT = {
    "A": 2,
    "B": 4,
    "C": 6,
    "D": 8
}

SYMBOL_VALUE = {
    "A": 5,
    "B": 4,
    "C": 3,
    "D": 2
}

def check_winings(columns: List, my_bet: int, my_lines: int):
    winnings = 0
    winning_lines = []
    for line in range(my_lines):
        symbol = columns[0][line]
        for column in columns:
            symbol_to_check = column[line]
            if symbol != symbol_to_check:
                break
        else:
            winnings += SYMBOL_VALUE[symbol] * my_bet
            winning_lines.append(line + 1)

    return winnings, winning_lines

    
# This function will 
def get_slot_machine_spin() -> List:
    all_symbols = []
    # this is so as it picks from the list it gets the values and the index
    for symbol, count  in SYMBOL_COUNT.items():
        for _ in range(count):
            all_symbols.append(symbol)
    columns = []
    for col in range(COLS):
        column = []
        current_symbols = all_symbols[:]
        for row in range(ROWS):
            value = random.choice(current_symbols)
            current_symbols.remove(value)
            column.append(value)
        columns.append(column)
    return columns

def print_slotmachine(columns: List):
    for row in range(ROWS):
        for index, column in enumerate(columns):
            if index != COLS - 1:
                print(column[row], "| ", end='')
        else:
            print(column[row])

def deposit() -> int:
    amount = 0
    while amount == 0:
        response = input("What would you like to depsoit? $")
        if response.isdigit():
            amount = int(response)
            if amount == 0:
                print("   Error: Amount must be greater than zero")
        else: 
            print("   Error: Please enter a number")
    return amount

def get_number_of_lines() -> int:
    lines = 0
    while lines < 1 or lines > MAX_LINES:
            response = input("Enter the number of lines to bet on (1-" + str(MAX_LINES) + ")? ")
            if response.isdigit():
                lines = int(response)
                if lines < 1 or lines > MAX_LINES:
                    print("   Error: Enter a valid number of lines.")
            else: 
                print("   Error: Please enter a number")
    return lines


def get_bet() -> int:
    bet = 0
    while bet < MIN_BET or  bet > MAX_BET:
        amount = input("What would you like to Bet on each line? $")
        if amount.isdigit():
            bet = int(amount)
            if bet < MIN_BET or bet > MAX_BET:
                print(f"  Error: Amount must be between ${MIN_BET} - ${MAX_BET}.")
        else: 
            print("  Error: Please enter a number")
    return bet

def roll(balance) -> int:
    balance = deposit()
    lines = get_number_of_lines()
    bet = -1
    total_bet = balance + 1
    while bet != 0 and total_bet > balance:
        bet = get_bet()
        print(f"___{bet}_{lines}")
        total_bet = bet * lines
        if total_bet > balance:
            print(f"  Error: You do not have enough to bet that amount, curent balance is: ${balance} ")
    if total_bet == 0:
        return
    print(f"You are betting ${bet} on {lines} lines. Total bet is equal to: ${total_bet} ")

    slots = get_slot_machine_spin()
    print_slotmachine(slots)
    winnings, winning_lines = check_winings(slots, bet, lines)
    print(f"You won ${winnings}.")
    if len(winning_lines) > 0:
        print(f"You won on line" + ("s" if len(winning_lines) > 1 else "") + ":", *winning_lines)
        balance += winnings
    else:
        balance -= bet * lines
    print(f"Your balance is now: ${balance}")
    # Should be returning "balance"
    # "winnings - total_bet" is not at all correct
    return winnings - total_bet

# main() does not return a value so should not be type hinted
def main() -> int:
    balance = deposit()
    # Using "while True" requires a break to exit.
    # Should instead be a conditional that tracks the actual exit conditions
    while True:
        print(f"Current Balance is ${balance}")
        answer = input("Press enter to play (q to quit).")
        if answer == "q":
            break
        balance += roll(balance)
    
    print(f"You left with ${balance}")

main()
print()
print("Thank you for playing!")