class CogdoFlyingGuiManager:
    def __init__(self, player):

        self.player = player

        self.root = NodePath("CogdoFlyingGui")
        self.root.reparentTo(aspect2d)

        self.fuelMeter = NodePath("scrubMeter")
        self.fuelMeter.reparentTo(self.root)
        self.fuelMeter.setPos(1.1, 0.0, -0.7)
        self.fuelMeter.setSz(2.0)

        cm = CardMaker('card')
        cm.setFrame(-0.07, 0.07, 0.0, 0.75)
        self.fuelMeterBar = self.fuelMeter.attachNewNode(cm.generate())
        self.fuelMeterBar.setColor(0.95, 0.95, 0.0, 1.0)

        self.fuelLabel = DirectLabel(
            parent=self.root,
            relief=None,
            pos=(1.1, 0, -0.8),
            scale=0.075,
            text="Fuel",
            text_fg=(0.95, 0.95, 0, 1),
            text_shadow=(0, 0, 0, 1),
            text_font=ToontownGlobals.getInterfaceFont(),
        )

        self.messageLabel = DirectLabel(
            parent=self.root,
            relief=None,
            pos=(0.0, 0.0, -0.9),
            scale=0.1,
            text="                ",
            text_align=TextNode.ACenter,
            text_fg=(0.95, 0.95, 0, 1),
            text_shadow=(0, 0, 0, 1),
            text_font=ToontownGlobals.getInterfaceFont(),
            textMayChange=1,
        )
        self.messageLabel.stash()

        self.winLabel = DirectLabel(
            parent=self.root,
            relief=None,
            pos=(0.0, 0.0, 0.0),
            scale=0.25,
            text="You win!",
            text_align=TextNode.ACenter,
            text_fg=(0.95, 0.95, 0, 1),
            text_shadow=(0, 0, 0, 1),
            text_font=ToontownGlobals.getInterfaceFont(),
        )
        self.winLabel.stash()

        self.refuelLerp = LerpFunctionInterval(self.fuelMeterBar.setSz,
                                               fromData=0.0,
                                               toData=1.0,
                                               duration=2.0)

    def setRefuelLerpFromData(self):
        startScale = self.fuelMeterBar.getSz()
        self.refuelLerp.fromData = startScale

    def setMessageLabelText(self, text):
        self.messageLabel["text"] = text
        self.messageLabel.setText()

    def update(self):
        self.fuelMeterBar.setSz(self.player.fuel)

    def destroy(self):
        #        print "Destroying GUI"
        self.fuelMeterBar.detachNode()
        self.fuelMeterBar = None

        self.fuelLabel.detachNode()
        self.fuelLabel = None

        self.fuelMeter.detachNode()
        self.fuelMeter = None

        self.winLabel.detachNode()
        self.winLabel = None

        self.root.detachNode()
        self.root = None

        self.player = None
class CogdoFlyingGame(DirectObject):
    UPDATE_TASK_NAME = "CogdoFlyingGameUpdate"
    GAME_COMPLETE_TASK_NAME = "CogdoFlyingGameCompleteTask"

    def __init__(self, distGame):
        self.distGame = distGame

        # Unused currently
        self.players = {}
        self.localPlayer = CogdoFlyingLocalPlayer(self, base.localAvatar)

        # Unused currently
        self.toonDropShadows = []

        self.startPlatform = None
        #        self.currentPlatform = None
        self.endPlatform = None
        self.fuelPlatforms = {}
        self.isGameComplete = False

        self.upLimit = 0.0
        self.downLimit = 0.0
        self.leftLimit = 0.0
        self.rightLimit = 0.0

    def load(self):
        self.root = NodePath('root')
        self.root.reparentTo(render)
        self.root.stash()

        self.world = loadMockup("cogdominium/mockup.egg")
        self.world.reparentTo(self.root)
        self.world.stash()

        # Setup and placement of starting platform
        self.startPlatform = loadMockup("cogdominium/start_platform.egg")
        startPlatformLoc = self.world.find("**/start_platform_loc")
        self.startPlatform.reparentTo(startPlatformLoc)
        colModel = self.startPlatform.find("**/col_floor")
        colModel.setTag('start_platform', '%s' % base.localAvatar.doId)

        # Here we set the current platform for the local player
        self.localPlayer.setCheckpointPlatform(self.startPlatform)

        # Setup and placement of the end platform
        self.endPlatform = loadMockup("cogdominium/end_platform.egg")
        endPlatformLoc = self.world.find("**/end_platform_loc")
        self.endPlatform.reparentTo(endPlatformLoc)
        colModel = self.endPlatform.find("**/col_floor")
        colModel.setTag('end_platform', '%s' % base.localAvatar.doId)

        # Setup and placement for all the fuel platforms
        fuelPlatformModel = loadMockup("cogdominium/fuel_platform.egg")
        fuelIndex = 1
        fuelLoc = self.world.find('**/fuel_platform_loc_%d' % fuelIndex)
        while not fuelLoc.isEmpty():
            fuelModel = NodePath("fuel_platform_%d" % fuelIndex)
            fuelPlatformModel.copyTo(fuelModel)
            fuelModel.reparentTo(fuelLoc)
            colModel = fuelModel.find("**/col_floor")
            colModel.setTag('fuel_platform', '%s' % base.localAvatar.doId)
            colModel.setTag('isUsed', '%s' % 0)
            self.fuelPlatforms[fuelModel.getName()] = fuelModel
            fuelIndex += 1
            fuelLoc = self.world.find('**/fuel_platform_loc_%d' % fuelIndex)

        self.accept("entercol_floor", self.handleCollision)

        self.skybox = self.world.find("**/skybox")
        self.upLimit = self.world.find("**/limit_up").getPos(render).getZ()
        self.downLimit = self.world.find("**/limit_down").getPos(render).getZ()
        self.leftLimit = self.world.find("**/limit_left").getPos(render).getX()
        self.rightLimit = self.world.find("**/limit_right").getPos(
            render).getX()

        del fuelPlatformModel

        self._initFog()

    def unload(self):
        self.__stopUpdateTask()
        self.__stopGameCompleteTask()
        self._destroyFog()
        #        print "Unload Flying CogdoGame"
        self.localPlayer.unload()
        del self.localPlayer

        self.fuelPlatforms.clear()
        self.endPlatform = None

        self.world.detachNode()
        del self.world

        self.root.detachNode()
        del self.root

        self.ignore("entercol_floor")

    def handleCollision(self, collEntry):
        fromNodePath = collEntry.getFromNodePath()
        intoNodePath = collEntry.getIntoNodePath()
        intoName = intoNodePath.getName()
        fromName = fromNodePath.getName()

        if intoNodePath.getTag('fuel_platform') != "":
            if not int(
                    intoNodePath.getTag('isUsed')
            ) or CogdoFlyingGameGlobals.FlyingGame.MULTIPLE_REFUELS_PER_STATION:
                intoNodePath.setTag('isUsed', '%s' % 1)
                self.localPlayer.setCheckpointPlatform(
                    intoNodePath.getParent())
                self.localPlayer.request("Refuel")
        if intoNodePath.getTag('end_platform') != "":
            self.localPlayer.request("WaitingForWin")

    def enable(self):
        self.localPlayer.request("FreeFly")
        self.__startUpdateTask()
        self.isGameComplete = False

    def disable(self):
        self.__stopUpdateTask()
        self.__stopGameCompleteTask()
        self.localPlayer.request("Inactive")

    def _initFog(self):
        self.fog = Fog("FlyingFog")
        self.fog.setColor(VBase4(0.8, 0.8, 0.8, 1.0))
        self.fog.setLinearRange(100.0, 400.0)

        self._renderFog = render.getFog()
        render.setFog(self.fog)

    def _destroyFog(self):
        render.clearFog()
        del self.fog
        del self._renderFog

    def onstage(self):
        self.root.unstash()
        self.world.unstash()

        self.localPlayer.onstage()

    def offstage(self):
        self.__stopUpdateTask()
        self.world.stash()
        self.root.stash()
        self.localPlayer.offstage()

        #TODO: Temp solution, this is supposed to come from the minigame
        # Which means the minigame isn't getting cleaned up properly look into this
        self.unload()

    def handleToonJoined(self, toon):
        # Not used, no multiplayer support in yet
        if toon == base.localAvatar:
            player = CogdoFlyingLocalPlayer(toon)
            player.entersActivity()

            self.localPlayer = player
        else:
            player = CogdoFlyingPlayer(toon)
            player.entersActivity()

        self.players[toon.doId] = player

    def __startUpdateTask(self):
        self.__stopUpdateTask()
        taskMgr.add(self.__updateTask, CogdoFlyingGame.UPDATE_TASK_NAME, 45)

    def __stopUpdateTask(self):
        taskMgr.remove(CogdoFlyingGame.UPDATE_TASK_NAME)

    def __stopGameCompleteTask(self):
        taskMgr.remove(CogdoFlyingGame.GAME_COMPLETE_TASK_NAME)

    def gameComplete(self):
        self.localPlayer.request("Win")

    def __updateTask(self, task):
        self.localPlayer.update()

        #TODO:flying: make this win condition stuff work for multiple toons
        if self.localPlayer.state == "WaitingForWin" and not self.isGameComplete:
            self.isGameComplete = True
            taskMgr.doMethodLater(6.0,
                                  self.gameComplete,
                                  CogdoFlyingGame.GAME_COMPLETE_TASK_NAME,
                                  extraArgs=[])

        self.skybox.setPos(self.skybox.getPos().getX(),
                           self.localPlayer.toon.getPos().getY(),
                           self.skybox.getPos().getZ())
        return Task.cont