import json
import socket
from Classes.Game.Player import Player


class GameManager:
    __players:dict
    __playingPlayer:Player
    __state:str
    __round:str

    def __init__(self):
        self.__players = {}
        self.__playingPlayer = None
        self.__state = "waiting"
        self.__round = "none"
        pass

    # game round management
    # this section manages the flow of rounds this should inherit itself
    # =============================================================================
    
    # this function iterates all 
    def progressRound(self):
        # phases
        # - playerPrep => playing player switches, gets a mana point and gets verified
        if self.__playingPlayer != None:
            for player in self.__players:
                if self.__playingPlayer != player:
                    self.__playingPlayer = player
        else:
            self.__playingPlayer = next(iter(self.__players))
        # - playerDraw => player draws a card
        # - playerPlay => player can place cards and active effects
        # - playerEnd => player ends his turn and the code reiterates with the remaining player
        pass

    # game state management
    # this section mostly only used by the networking and event handling classes
    # other parts should never need to interface with this unless really required
    # =============================================================================
    def startGame(self, tcpSocket:socket):
        self.__state = "running"

        print("game starts")
        for userAddr in self.__users.keys():
            try:
                user = self.__serverWorld.getPlayers[userAddr]["player"]
                user.addMana(1000)
                user.adjustHP(1000)
                user.shuffleDeck()
                cards = user.getDeck()
                user.setHand(cards[:5])

                payload = {
                    "event":"startgame",
                    "playermana": user.getMana(),
                    "playerhp": user.getHP(),
                    "playername": user.getName(),
                    "hand": user.getHand()
                }

                tcpSocket.send(json.dumps(payload).encode())
            except Exception as e:
                print(f"failed to start game due to error: {e}")
                break
        # handles notifying all players that the game starts
        pass

    def stopGame(self):
        # handles notifying all players that the game stops
        # handles stoping the game itself and notifies server to stop itself
        pass

    # player management
    # the network manager will create a player instance
    # =============================================================

    # gets all player known to the game manager and returns them
    def getPlayers(self) -> dict:
        return self.__players

    # creates a player and handles counting all players and if conditions met starting the game
    # returns the new dict in which the new player now is added
    def addPlayers(self, player:Player, socket:socket, clientAddr) -> dict:
        self.__players[clientAddr] = {
            player: player,
            socket:socket
        }

        # counts participating players and starts the game if enough have joined
        if len(self.__players) == 2:
            self.startGame(socket)

        return self.__players