from core.helper import tileVector

from core.ui import DialogUi

from siege import game
from siege.util import PixelRect
from siege.world import World
from siege.world.realm import Layer


class InfoUi(DialogUi):
    def __init__(self, player):
        self.player = player
        DialogUi.__init__(self, 'info', PixelRect(600, 40, 300, 260))
        self.frameTimeAverage = 20

        self.load('html/info.html')

    def update(self, frameTime):
        #Update FPS
        self.frameTimeAverage = self.frameTimeAverage * 0.85 + frameTime * 0.15

        if not self.view.isVisible():
            return
        commands = []
        world = World.get()

        fps = int(1000 / self.frameTimeAverage)
        commands.append("App.info.set('fps', {});".format(fps))

        tilePosition = world.getRealm().layers[Layer.Ground].getTilePosition(self.player.entity.getPosition())
        commands.append("App.info.set('segment', {});".format(tilePosition.segment))

        commands.append("App.info.set('gametime', {});".format(world.time.hours.get()))
        commands.append("App.info.set('monsters', {});".format(game.monster.getCount()))
        commands.append("App.info.set('spawners', {});".format(game.spawner.getCount()))
        commands.append("App.info.set('automata', '{0.activeCells} : {0.skippedCells}');".format(world.getRealm().automata))
        commands.append("App.info.set('cursor_pixel_position', '{0.x:.2f}, {0.y:.2f}');".format(world.getMousePosition()))
        commands.append("App.info.set('cursor_tile_position', '{0.x}, {0.y}');".format(tileVector(world.getMousePosition())))

        self.view.execute(commands)
