from src.config import *
from src.networking.Networking import Networking

from src.PlayerState import PlayerState

from src.displays.PlayerDisplay import PlayerDisplay
from src.displays.SpecialDisplay import SpecialDisplay

from src.EventHandler import EventHandler
from src.targets import *

from src.events.PointEvent import PointEvent

class GameFactory:
    def createGame(self):
        targets = self.createAllTargets()
        eventHandler = EventHandler(targets)

        displayNames = Networking.getDisplays()
        playerDisplays = self.createPlayerDisplays(displayNames)

        playerState = PlayerState()
        pointEvent = PointEvent()
        for target in self.targets:
            target.on(target.hit_key,
        pass

    def createPlayerDisplays(self, displayNames):
        result = []
        for displayName in displayNames:
            if("Player" in displayName):
                display = PlayerDisplay(displayName)
                result.append(display)
        return result

    def createPlayerStates(self, playerDisplays, specialDisplay, ballsPerGame = BALLS_PER_GAME, beginningUpperPlayfieldTime = BEGINNING_UPPER_PLAYFIELD_TIME, beginningBonusTime = BEGINNING_BONUS_TIME):
        result = []
        for i in range(len(playerDisplays)):
            playerState = PlayerState(playerDisplays[i], specialDisplay, i, ballsPerGame, beginningUpperPlayfieldTime, beginningBonusTime)
            result.append(playerState)
        return result

    def createAllTargets(self):
        result = dict
        glo = globals() # Save globals so the dict does not change during execution of the script
        for key in glo:
            target = glo[key]
            if(key.endswith('Target') and type(target) == 'class'):
                result[key] = target()
        # todo check with driver?
        return result