# Path: resources/lib/functions.py
# def play_action(params):
#     log.debug("== ENTER: PLAY ==")
# 
#     log.debug("PLAY ACTION PARAMS: {0}", params)
#     item_id = params.get("item_id")
# 
#     auto_resume = int(params.get("auto_resume", "-1"))
#     log.debug("AUTO_RESUME: {0}", auto_resume)
# 
#     force_transcode = params.get("force_transcode", None) is not None
#     log.debug("FORCE_TRANSCODE: {0}", force_transcode)
# 
#     media_source_id = params.get("media_source_id", "")
#     log.debug("media_source_id: {0}", media_source_id)
# 
#     subtitle_stream_index = params.get("subtitle_stream_index")
#     log.debug("subtitle_stream_index: {0}", subtitle_stream_index)
# 
#     audio_stream_index = params.get("audio_stream_index")
#     log.debug("audio_stream_index: {0}", audio_stream_index)
# 
#     action = params.get("action", "play")
# 
#     # set the current playing item id
#     # set all the playback info, this will be picked up by the service
#     # the service will then start the playback
# 
#     xbmc.Player().stop()
# 
#     play_info = {}
#     play_info["action"] = action
#     play_info["item_id"] = item_id
#     play_info["auto_resume"] = str(auto_resume)
#     play_info["force_transcode"] = force_transcode
#     play_info["media_source_id"] = media_source_id
#     play_info["subtitle_stream_index"] = subtitle_stream_index
#     play_info["audio_stream_index"] = audio_stream_index
#     log.info("Sending embycon_play_action : {0}", play_info)
#     send_event_notification("embycon_play_action", play_info)
# 
# Path: resources/lib/simple_logging.py
# class SimpleLogging:
#     name = ""
#     enable_logging = False
# 
#     def __init__(self, name):
#         settings = xbmcaddon.Addon()
#         prefix = settings.getAddonInfo('name')
#         self.name = prefix + '.' + name
#         self.enable_logging = settings.getSetting('log_debug') == "true"
# 
#         # params = {"setting": "debug.showloginfo"}
#         # setting_result = json_rpc('Settings.getSettingValue').execute(params)
#         # current_value = setting_result.get("result", None)
#         # if current_value is not None:
#         #     self.enable_logging = current_value.get("value", False)
#         # xbmc.log("LOGGING_ENABLED %s : %s" % (self.name, str(self.enable_logging)), level=xbmc.LOGDEBUG)
# 
#     def __str__(self):
#         return "LoggingEnabled: " + str(self.enable_logging)
# 
#     def info(self, fmt, *args, **kwargs):
#         log_line = self.name + "|INFO|" + self.log_line(fmt, *args)
#         xbmc.log(log_line, level=xbmc.LOGNOTICE)
# 
#     def error(self, fmt, *args, **kwargs):
#         log_line = self.name + "|ERROR|" + self.log_line(fmt, *args)
#         xbmc.log(log_line, level=xbmc.LOGERROR)
# 
#     def debug(self, fmt, *args, **kwargs):
#         if self.enable_logging:
#             log_line = self.name + "|DEBUG|" + self.log_line(fmt, *args)
#             xbmc.log(log_line, level=xbmc.LOGNOTICE)
# 
#     @staticmethod
#     def log_line(fmt, *args):
#         new_args = []
#         # convert any unicode to utf-8 strings
#         for arg in args:
#             if isinstance(arg, unicode):
#                 new_args.append(arg.encode("utf-8"))
#             else:
#                 new_args.append(arg)
#         log_line = fmt.format(*new_args)
#         return log_line
# 
# Path: resources/lib/jsonrpc.py
# class JsonRpc(object):
# 
#     id_ = 1
#     jsonrpc = "2.0"
#     params = None
# 
#     def __init__(self, method, **kwargs):
#         
#         self.method = method
# 
#         for arg in kwargs:  # id_(int), jsonrpc(str)
#             self.arg = arg
# 
#     def _query(self):
# 
#         query = {
#             
#             'jsonrpc': self.jsonrpc,
#             'id': self.id_,
#             'method': self.method,
#         }
#         if self.params is not None:
#             query['params'] = self.params
# 
#         return json.dumps(query)
# 
#     def execute(self, params=None):
# 
#         self.params = params
#         return json.loads(xbmc.executeJSONRPC(self._query()))
# 
# Path: resources/lib/kodi_utils.py
# class HomeWindow:
#     """
#         xbmcgui.Window(10000) with add-on id prefixed to keys
#     """
# 
#     def __init__(self):
#         self.id_string = 'plugin.video.embycon-%s'
#         self.window = xbmcgui.Window(10000)
# 
#     def get_property(self, key):
#         key = self.id_string % key
#         value = self.window.getProperty(key)
#         # log.debug('HomeWindow: getProperty |{0}| -> |{1}|', key, value)
#         return value
# 
#     def set_property(self, key, value):
#         key = self.id_string % key
#         # log.debug('HomeWindow: setProperty |{0}| -> |{1}|', key, value)
#         self.window.setProperty(key, value)
# 
#     def clear_property(self, key):
#         key = self.id_string % key
#         # log.debug('HomeWindow: clearProperty |{0}|', key)
#         self.window.clearProperty(key)


import json
import threading
import websocket
import xbmc
import xbmcgui
from .functions import play_action
from .simple_logging import SimpleLogging
from . import clientinfo
from . import downloadutils
from .jsonrpc import JsonRpc
from .kodi_utils import HomeWindow
# -*- coding: utf-8 -*-

#################################################################################################




log = SimpleLogging(__name__)


class WebSocketClient(threading.Thread):

    _shared_state = {}


# Path: resources/lib/functions.py
# def play_action(params):
#     log.debug("== ENTER: PLAY ==")
# 
#     log.debug("PLAY ACTION PARAMS: {0}", params)
#     item_id = params.get("item_id")
# 
#     auto_resume = int(params.get("auto_resume", "-1"))
#     log.debug("AUTO_RESUME: {0}", auto_resume)
# 
#     force_transcode = params.get("force_transcode", None) is not None
#     log.debug("FORCE_TRANSCODE: {0}", force_transcode)
# 
#     media_source_id = params.get("media_source_id", "")
#     log.debug("media_source_id: {0}", media_source_id)
# 
#     subtitle_stream_index = params.get("subtitle_stream_index")
#     log.debug("subtitle_stream_index: {0}", subtitle_stream_index)
# 
#     audio_stream_index = params.get("audio_stream_index")
#     log.debug("audio_stream_index: {0}", audio_stream_index)
# 
#     action = params.get("action", "play")
# 
#     # set the current playing item id
#     # set all the playback info, this will be picked up by the service
#     # the service will then start the playback
# 
#     xbmc.Player().stop()
# 
#     play_info = {}
#     play_info["action"] = action
#     play_info["item_id"] = item_id
#     play_info["auto_resume"] = str(auto_resume)
#     play_info["force_transcode"] = force_transcode
#     play_info["media_source_id"] = media_source_id
#     play_info["subtitle_stream_index"] = subtitle_stream_index
#     play_info["audio_stream_index"] = audio_stream_index
#     log.info("Sending embycon_play_action : {0}", play_info)
#     send_event_notification("embycon_play_action", play_info)
# 
# Path: resources/lib/simple_logging.py
# class SimpleLogging:
#     name = ""
#     enable_logging = False
# 
#     def __init__(self, name):
#         settings = xbmcaddon.Addon()
#         prefix = settings.getAddonInfo('name')
#         self.name = prefix + '.' + name
#         self.enable_logging = settings.getSetting('log_debug') == "true"
# 
#         # params = {"setting": "debug.showloginfo"}
#         # setting_result = json_rpc('Settings.getSettingValue').execute(params)
#         # current_value = setting_result.get("result", None)
#         # if current_value is not None:
#         #     self.enable_logging = current_value.get("value", False)
#         # xbmc.log("LOGGING_ENABLED %s : %s" % (self.name, str(self.enable_logging)), level=xbmc.LOGDEBUG)
# 
#     def __str__(self):
#         return "LoggingEnabled: " + str(self.enable_logging)
# 
#     def info(self, fmt, *args, **kwargs):
#         log_line = self.name + "|INFO|" + self.log_line(fmt, *args)
#         xbmc.log(log_line, level=xbmc.LOGNOTICE)
# 
#     def error(self, fmt, *args, **kwargs):
#         log_line = self.name + "|ERROR|" + self.log_line(fmt, *args)
#         xbmc.log(log_line, level=xbmc.LOGERROR)
# 
#     def debug(self, fmt, *args, **kwargs):
#         if self.enable_logging:
#             log_line = self.name + "|DEBUG|" + self.log_line(fmt, *args)
#             xbmc.log(log_line, level=xbmc.LOGNOTICE)
# 
#     @staticmethod
#     def log_line(fmt, *args):
#         new_args = []
#         # convert any unicode to utf-8 strings
#         for arg in args:
#             if isinstance(arg, unicode):
#                 new_args.append(arg.encode("utf-8"))
#             else:
#                 new_args.append(arg)
#         log_line = fmt.format(*new_args)
#         return log_line
# 
# Path: resources/lib/jsonrpc.py
# class JsonRpc(object):
# 
#     id_ = 1
#     jsonrpc = "2.0"
#     params = None
# 
#     def __init__(self, method, **kwargs):
#         
#         self.method = method
# 
#         for arg in kwargs:  # id_(int), jsonrpc(str)
#             self.arg = arg
# 
#     def _query(self):
# 
#         query = {
#             
#             'jsonrpc': self.jsonrpc,
#             'id': self.id_,
#             'method': self.method,
#         }
#         if self.params is not None:
#             query['params'] = self.params
# 
#         return json.dumps(query)
# 
#     def execute(self, params=None):
# 
#         self.params = params
#         return json.loads(xbmc.executeJSONRPC(self._query()))
# 
# Path: resources/lib/kodi_utils.py
# class HomeWindow:
#     """
#         xbmcgui.Window(10000) with add-on id prefixed to keys
#     """
# 
#     def __init__(self):
#         self.id_string = 'plugin.video.embycon-%s'
#         self.window = xbmcgui.Window(10000)
# 
#     def get_property(self, key):
#         key = self.id_string % key
#         value = self.window.getProperty(key)
#         # log.debug('HomeWindow: getProperty |{0}| -> |{1}|', key, value)
#         return value
# 
#     def set_property(self, key, value):
#         key = self.id_string % key
#         # log.debug('HomeWindow: setProperty |{0}| -> |{1}|', key, value)
#         self.window.setProperty(key, value)
# 
#     def clear_property(self, key):
#         key = self.id_string % key
#         # log.debug('HomeWindow: clearProperty |{0}|', key)
#         self.window.clearProperty(key)


# Path: resources/lib/websocket_client.py
import json
import threading
import websocket
import xbmc
import xbmcgui
from .functions import play_action
from .simple_logging import SimpleLogging
from . import clientinfo
from . import downloadutils
from .jsonrpc import JsonRpc
from .kodi_utils import HomeWindow

# -*- coding: utf-8 -*-

#################################################################################################




log = SimpleLogging(__name__)


class WebSocketClient(threading.Thread):

    _shared_state = {}


    _client = None