import xbmc
import xbmcaddon
import xbmcgui
import xbmcplugin
import bjsonrpc
import platform
from bjsonrpc.handlers import BaseHandler
from pulsar.addon import ADDON
from pulsar.logger import log
from pulsar.config import JSONRPC_EXT_PORT
from pulsar.platform import PLATFORM


XBMC_PLAYER = xbmc.Player()


class PulsarRPCServer(BaseHandler):
    public_methods_pattern = r'^[A-Za-z]\w+$'

    _objects = {}

    def Reset(self):
        self._objects = {}

    def Keyboard(self, default="", heading="", hidden=False):
        keyboard = xbmc.Keyboard(default, heading, hidden)
        keyboard.doModal()
        if keyboard.isConfirmed():
            return keyboard.getText()

    def Dialog_Select(self, title, items):
        dialog = xbmcgui.Dialog()
        return dialog.select(title, items)

    def Player_GetPlayingFile(self, *args, **kwargs):
        return XBMC_PLAYER.getPlayingFile()

    def Player_IsPlaying(self, *args, **kwargs):
        return XBMC_PLAYER.isPlaying(*args, **kwargs)

    def ConvertLanguage(self, *args, **kwargs):
        return xbmc.convertLanguage(*args, **kwargs)

    def GetPlatform(self):
        return PLATFORM

    def GetAddonInfo(self):
        info = {}
        for key in ("author", "changelog", "description", "disclaimer",
                    "fanart", "icon", "id", "name", "path", "profile", "stars",
                    "summary", "type", "version"):
            info[key] = ADDON.getAddonInfo(key)
        return info

    def GetLanguage(self, *args, **kwargs):
        return xbmc.getLanguage(*args, **kwargs)

    def GetSetting(self, *args, **kwargs):
        return ADDON.getSetting(*args, **kwargs)

    def SetSetting(self, *args, **kwargs):
        return ADDON.setSetting(*args, **kwargs)

    def TranslatePath(self, *args, **kwargs):
        return xbmc.translatePath(*args, **kwargs)

    def Log(self, *args, **kwargs):
        return xbmc.log(*args, **kwargs)

    def Dialog_CloseAll(self, *args, **kwargs):
        return xbmc.executebuiltin("Dialog.Close(all, true)")

    ###########################################################################
    # DialogProgress
    ###########################################################################
    def DialogProgress_Create(self, *args, **kwargs):
        dialog = xbmcgui.DialogProgress()
        self._objects[id(dialog)] = dialog
        dialog.create(*args, **kwargs)
        return id(dialog)

    def DialogProgress_IsCanceled(self, hwnd, *args, **kwargs):
        return self._objects[hwnd].iscanceled(*args, **kwargs)

    def DialogProgress_Update(self, hwnd, *args, **kwargs):
        return self._objects[hwnd].update(*args, **kwargs)

    def DialogProgress_Close(self, hwnd, *args, **kwargs):
        dialog = self._objects.pop(hwnd)
        dialog.close()
        del dialog


def server_thread():
    try:
        s = bjsonrpc.createserver(port=JSONRPC_EXT_PORT, handler_factory=PulsarRPCServer)
        log.info("pulsar: starting jsonrpc service")
        s.serve()
        log.info("pulsar: exiting jsonrpc service")
    except Exception, e:
        import traceback
        map(log.info, traceback.format_exc().split("\n"))
        raise
