from config import *
from networking.Networking import Networking

from factories.PlayerStateFactory import PlayerStateFactory
from factories.DisplayFactory import DisplayFactory
from factories.TargetFactory import TargetFactory
from factories.EventFactory import EventFactory

from InputHandler import InputHandler

from GameState import GameState

from Game import Game

class GameFactory:
    def __init__(self):
        displayNames = Networking().getDisplays()
        self.displayFactory = DisplayFactory(displayNames)
        specialDisplay = self.displayFactory.createSpecialDisplay()

        self.playerCount = len(displayNames) - 1

        self.playerStateFactory = PlayerStateFactory(specialDisplay)


    def createGame(self):
        targets = TargetFactory.createAllTargets()
        inputHandler = InputHandler(targets)

        players = self.createPlayers()

        gameState = GameState(players, self.getHighScore(), players[0].id)

        eventFactory = EventFactory(targets, gameState.currentPlayer)
        events = eventFactory.allEvents()

        return Game(inputHandler, gameState, events)

    def createPlayers(self):
        players = []
        for id in range(self.playerCount):
            display = self.displayFactory.createPlayerDisplay(id)
            player = self.playerStateFactory.createPlayerState(display, id)
            players.append(player)
        return players

    def getHighScore(self): # todo
        return 0

