import socket
import json

class Client:
    def __init__(self, address, port):
        self.address = address
        self.port = port
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        
    def connect(self):
        self.socket.connect((self.address, self.port))

    def send(self, message):
        self.socket.send(message.encode())

    def receive_string(self):
        return self.socket.recv(1024).decode()

    def receive(self):
        return json.loads(self.socket.recv(1024))

    def close(self):
        self.socket.close()

class UI:
    def __init__(self):
        self.client = Client("172.30.114.67", 3000)
        self.client.connect()
        print(self.client.receive_string())
        self.run()
    def run(self):
        while True:
            option = input("Choose an option: ")
            match option:
                case "1":
                    self.client.send(option)
                    data = self.client.receive()
                    print("\nPlayer hand:")
                    for card in data['playerHand']:
                        print(card, end=" ")
                    print("\nPlayer score:", data['playerScore'])

                    print("\nDealer hand:")
                    for card in data['dealerHand']:
                        print(card, end=" ")
                    print("\nDealer score:", data['dealerScore'])
                case "2":
                    self.client.send(option)
                    print(self.client.receive_string())
                    self.client.close()
                    exit()
                case "stand":
                    self.client.send(option)
                    data = self.client.receive()
                    if(data.get('error') != None):
                        print(data['error'])
                        continue
                    print("\nPlayer hand:")
                    for card in data['playerHand']:
                        print(card, end=" ")
                    print("\nPlayer score:", data['playerScore'])

                    print("\nDealer hand:")
                    for card in data['dealerHand']:
                        print(card, end=" ")
                    print("\nDealer score:", data['dealerScore'])
                    if(data.get('winner') != None):
                        print("Winner: " + data['winner'])
                        print(self.client.receive_string())
                    else:
                        print("hit or stand?")
                case "s":
                    self.client.send(option)
                    data = self.client.receive()
                    if(data.get('error') != None):
                        print(data['error'])
                        continue
                    print("\nPlayer hand:")
                    for card in data['playerHand']:
                        print(card, end=" ")
                    print("\nPlayer score:", data['playerScore'])

                    print("\nDealer hand:")
                    for card in data['dealerHand']:
                        print(card, end=" ")
                    print("\nDealer score:", data['dealerScore'])
                    if(data.get('winner') != None):
                        print("Winner: " + data['winner'])
                        print(self.client.receive_string())
                    else:
                        print("hit or stand?")
                case "hit":
                    self.client.send(option)
                    data = self.client.receive()
                    if(data.get('error') != None):
                        print(data['error'])
                        continue
                    print("\nPlayer hand:")
                    for card in data['playerHand']:
                        print(card, end=" ")
                    print("\nPlayer score:", data['playerScore'])

                    print("\nDealer hand:")
                    for card in data['dealerHand']:
                        print(card, end=" ")
                    print("\nDealer score:", data['dealerScore'])
                    if(data.get('winner') != None):
                        print("Winner: " + data['winner'])
                        print(self.client.receive_string())
                    else:
                        print("hit or stand?")
                case "h":
                    self.client.send(option)
                    data = self.client.receive()
                    if(data.get('error') != None):
                        print(data['error'])
                        continue
                    print("\nPlayer hand:")
                    for card in data['playerHand']:
                        print(card, end=" ")
                    print("\nPlayer score:", data['playerScore'])

                    print("\nDealer hand:")
                    for card in data['dealerHand']:
                        print(card, end=" ")
                    print("\nDealer score:", data['dealerScore'])
                    if(data.get('winner') != None):
                        print("Winner: " + data['winner'])
                        print(self.client.receive_string())
                    else:
                        print("hit or stand?")

                case _:
                    print("Invalid option")

    
        
ui=UI()
