from core.helper import getRandomPath
from siege import game
from siege.util import Vector
from siege.world import World


class DialogUi(object):
    '''Base dialog class that handles the dialog fundamentals.'''

    def __init__(self, viewName, viewArea):
        self.view = game.gui.createView(viewName, viewArea)
        self.defaultPosition = Vector(viewArea.x, viewArea.y)
        self.view.register('playSound', self.onPlaySound)
        self.entity = None
        self.maxDistance = 0

    def update(self, frameTime):
        if self.entity:
            distance = World.get().getPlayer().entity.physics.getCenter().getDistance(self.entity.render.getCenter(), self.entity.loopWidth)
            if distance > self.maxDistance:
                self.hide()

    def load(self, page):
        self.view.load(page)

    def show(self, focus=True):
        self.view.show()
        if focus:
            game.gui.focusView(self.view)

    def showWhileNearby(self, entity, maxDistance=128):
        self.entity = entity
        self.maxDistance = maxDistance
        self.show()

    def hide(self):
        self.view.hide()

    def toggleVisibility(self):
        self.hide() if self.view.isVisible() else self.show()

    def isVisible(self):
        return self.view.isVisible()

    def onPlaySound(self, view, args):
        game.audio.play(getRandomPath(args[1].toString()))


def boolStr(value):
    return "true" if value else "false"
