# 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/widgets.py
# @timer
# def check_for_new_content():
#     log.debug("checkForNewContent Called")
# 
#     home_window = HomeWindow()
# 
#     url_params = {}
#     url_params["Recursive"] = True
#     url_params["limit"] = 1
#     url_params["Fields"] = "DateCreated,Etag"
#     url_params["SortBy"] = "DateCreated"
#     url_params["SortOrder"] = "Descending"
#     url_params["IncludeItemTypes"] = "Movie,Episode"
#     url_params["ImageTypeLimit"] = 0
#     url_params["format"] = "json"
# 
#     added_url = get_emby_url('{server}/emby/Users/{userid}/Items', url_params)
# 
#     added_result = downloadUtils.download_url(added_url, suppress=True)
#     result = json.loads(added_result)
#     log.debug("LATEST_ADDED_ITEM: {0}", result)
# 
#     last_added_date = ""
#     if result is not None:
#         items = result.get("Items", [])
#         if len(items) > 0:
#             item = items[0]
#             last_added_date = item.get("Etag", "")
#     log.debug("last_added_date: {0}", last_added_date)
# 
#     url_params = {}
#     url_params["Recursive"] = True
#     url_params["limit"] = 1
#     url_params["Fields"] = "DateCreated,Etag"
#     url_params["SortBy"] = "DatePlayed"
#     url_params["SortOrder"] = "Descending"
#     url_params["IncludeItemTypes"] = "Movie,Episode"
#     url_params["ImageTypeLimit"] = 0
#     url_params["format"] = "json"
# 
#     played_url = get_emby_url('{server}/emby/Users/{userid}/Items', url_params)
# 
#     played_result = downloadUtils.download_url(played_url, suppress=True)
#     result = json.loads(played_result)
#     log.debug("LATEST_PLAYED_ITEM: {0}", result)
# 
#     last_played_date = ""
#     if result is not None:
#         items = result.get("Items", [])
#         if len(items) > 0:
#             item = items[0]
#             # last_played_date = item.get("Etag", "")
#             user_data = item.get("UserData", None)
#             if user_data is not None:
#                 last_played_date = user_data.get("LastPlayedDate", "")
# 
#     log.debug("last_played_date: {0}", last_played_date)
# 
#     current_widget_hash = home_window.get_property("embycon_widget_reload")
#     log.debug("Current Widget Hash: {0}", current_widget_hash)
# 
#     m = hashlib.md5()
#     m.update(last_played_date + last_added_date)
#     new_widget_hash = m.hexdigest()
#     log.debug("New Widget Hash: {0}", new_widget_hash)
# 
#     if current_widget_hash != new_widget_hash:
#         home_window.set_property("embycon_widget_reload", new_widget_hash)
#         log.debug("Setting New Widget Hash: {0}", new_widget_hash)
# 
# Path: resources/lib/tracking.py
# def timer(func):
#     @functools.wraps(func)
#     def wrapper(*args, **kwargs):
#         started = time.time()
#         value = func(*args, **kwargs)
#         ended = time.time()
#         if enabled:
#             data = ""
#             if func.__name__ == "download_url" and len(args) > 1:
#                 data = args[1]
#             elif func.__name__ == "main_entry_point" and len(sys.argv) > 2:
#                 data = sys.argv[2]
#             log.info("timing_data|{0}|{1}|{2}|{3}", func.__name__, started, ended, data)
#         return value
#     return wrapper


import threading
import time
import xbmc
from .simple_logging import SimpleLogging
from .widgets import check_for_new_content
from .tracking import timer



log = SimpleLogging(__name__)


class LibraryChangeMonitor(threading.Thread):

    last_library_change_check = 0
    library_check_triggered = False

# 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/widgets.py
# @timer
# def check_for_new_content():
#     log.debug("checkForNewContent Called")
# 
#     home_window = HomeWindow()
# 
#     url_params = {}
#     url_params["Recursive"] = True
#     url_params["limit"] = 1
#     url_params["Fields"] = "DateCreated,Etag"
#     url_params["SortBy"] = "DateCreated"
#     url_params["SortOrder"] = "Descending"
#     url_params["IncludeItemTypes"] = "Movie,Episode"
#     url_params["ImageTypeLimit"] = 0
#     url_params["format"] = "json"
# 
#     added_url = get_emby_url('{server}/emby/Users/{userid}/Items', url_params)
# 
#     added_result = downloadUtils.download_url(added_url, suppress=True)
#     result = json.loads(added_result)
#     log.debug("LATEST_ADDED_ITEM: {0}", result)
# 
#     last_added_date = ""
#     if result is not None:
#         items = result.get("Items", [])
#         if len(items) > 0:
#             item = items[0]
#             last_added_date = item.get("Etag", "")
#     log.debug("last_added_date: {0}", last_added_date)
# 
#     url_params = {}
#     url_params["Recursive"] = True
#     url_params["limit"] = 1
#     url_params["Fields"] = "DateCreated,Etag"
#     url_params["SortBy"] = "DatePlayed"
#     url_params["SortOrder"] = "Descending"
#     url_params["IncludeItemTypes"] = "Movie,Episode"
#     url_params["ImageTypeLimit"] = 0
#     url_params["format"] = "json"
# 
#     played_url = get_emby_url('{server}/emby/Users/{userid}/Items', url_params)
# 
#     played_result = downloadUtils.download_url(played_url, suppress=True)
#     result = json.loads(played_result)
#     log.debug("LATEST_PLAYED_ITEM: {0}", result)
# 
#     last_played_date = ""
#     if result is not None:
#         items = result.get("Items", [])
#         if len(items) > 0:
#             item = items[0]
#             # last_played_date = item.get("Etag", "")
#             user_data = item.get("UserData", None)
#             if user_data is not None:
#                 last_played_date = user_data.get("LastPlayedDate", "")
# 
#     log.debug("last_played_date: {0}", last_played_date)
# 
#     current_widget_hash = home_window.get_property("embycon_widget_reload")
#     log.debug("Current Widget Hash: {0}", current_widget_hash)
# 
#     m = hashlib.md5()
#     m.update(last_played_date + last_added_date)
#     new_widget_hash = m.hexdigest()
#     log.debug("New Widget Hash: {0}", new_widget_hash)
# 
#     if current_widget_hash != new_widget_hash:
#         home_window.set_property("embycon_widget_reload", new_widget_hash)
#         log.debug("Setting New Widget Hash: {0}", new_widget_hash)
# 
# Path: resources/lib/tracking.py
# def timer(func):
#     @functools.wraps(func)
#     def wrapper(*args, **kwargs):
#         started = time.time()
#         value = func(*args, **kwargs)
#         ended = time.time()
#         if enabled:
#             data = ""
#             if func.__name__ == "download_url" and len(args) > 1:
#                 data = args[1]
#             elif func.__name__ == "main_entry_point" and len(sys.argv) > 2:
#                 data = sys.argv[2]
#             log.info("timing_data|{0}|{1}|{2}|{3}", func.__name__, started, ended, data)
#         return value
#     return wrapper


# Path: resources/lib/library_change_monitor.py
import threading
import time
import xbmc
from .simple_logging import SimpleLogging
from .widgets import check_for_new_content
from .tracking import timer




log = SimpleLogging(__name__)


class LibraryChangeMonitor(threading.Thread):

    last_library_change_check = 0
    library_check_triggered = False

    exit_now = False