# Path: resources/lib/common/logger.py
# def debug(text):
#     xbmc.log('[{}] {}'.format(APP, text), xbmc.LOGDEBUG)
# 
# Path: resources/lib/gui/dialog.py
# def dyesno(heading='heading', message='message', yeslabel='yes', nolabel='no', autoclose=None):
#     if autoclose is not None:
#         autoclose = autoclose * 1000
#     else:
#         autoclose = 0
#     if SYSTEM_VERSION > 18:
#         return dialog().yesno(heading=heading, message=message, nolabel=nolabel, yeslabel=yeslabel,
#                               autoclose=autoclose)
#     return dialog().yesno(heading=heading, line1=message, nolabel=nolabel, yeslabel=yeslabel,
#                           autoclose=autoclose)
# 
# def dok(heading, message=None):
#     if SYSTEM_VERSION > 18:
#         return dialog().ok(heading=heading, message=message)
#     return dialog().ok(heading=heading, line1=message)
# 
# def dinput(heading=None, default='', type=xbmcgui.INPUT_ALPHANUM):
#     return dialog().input(heading=heading, defaultt=default, type=type)
# 
# Path: resources/lib/kodiutils.py
# def translate_path(path):
#     try:
#         return xbmcvfs.translatePath(path)
#     except:
#         return xbmc.translatePath(path)
# 
# def get_system_platform():
#     platform = "unknown"
#     if xbmc.getCondVisibility('system.platform.linux') and not xbmc.getCondVisibility('system.platform.android'):
#         platform = "linux"
#     elif xbmc.getCondVisibility('system.platform.linux') and xbmc.getCondVisibility('system.platform.android'):
#         platform = "android"
#     elif xbmc.getCondVisibility('system.platform.uwp'):
#         platform = "uwp"
#     elif xbmc.getCondVisibility('system.platform.windows'):
#         platform = "windows"
#     elif xbmc.getCondVisibility('system.platform.osx'):
#         platform = "osx"
#     elif xbmc.getCondVisibility('system.platform.ios'):
#         platform = "ios"
#     return platform
# 
# def jsonrpc(**kwargs):
#     from json import dumps, loads
#     if kwargs.get('id') is None:
#         kwargs.update(id=0)
#     if kwargs.get('jsonrpc') is None:
#         kwargs.update(jsonrpc='2.0')
#     return loads(xbmc.executeJSONRPC(dumps(kwargs)))
# 
# Path: resources/lib/language.py
# class Strings:
#     SETUP_VIDEO_CACHE_MSG1 = 30120
#     SETUP_VIDEO_CACHE_MSG2 = 30121
#     SETUP_VIDEO_CACHE_MSG3 = 30122
#     SETUP_VIDEO_CACHE_MSG4 = 30123
# 
#     INTRO_STEP1_H1 = 30100
#     INTRO_STEP1_L1 = 30101
# 
#     INTRO_STEP2_H1 = 30012
# 
#     INTRO_STEP3_H1 = 30013
# 
#     INTRO_STEP4_H1 = 30102
#     INTRO_STEP4_L1 = 30103
# 
#     INTRO_STEP5_H1 = 30104
#     INTRO_STEP5_L1 = 30105
# 
#     KRASKA_NOTIFY_LOW_DAYS_LEFT = 30106
#     KRASKA_NOTIFY_CREDENTIAL_H1 = 30107
#     KRASKA_NOTIFY_CREDENTIAL_L1 = 30108
#     KRASKA_NOTIFY_NO_SUBSCRIPTION = 30131
# 
#     SYSTEM_H1 = 30109
#     SYSTEM_DEBUG_ENABLED = 30110
#     SYSTEM_DEBUG_DISABLED = 30111
#     SYSTEM_NOT_INSTALLED_FROM_REPOSITORY = 30127
# 
#     RESOLVE_ERROR_H1 = 30112
#     RESOLVE_ERROR_L1 = 30113
# 
#     SYSTEM_API_ERROR_L1 = 30114
# 
#     CONTEXT_PIN_UNPIN = 30115
#     CONTEXT_SELECT_STREAM = 30116
#     CONTEXT_DOWNLOAD = 30119
#     CONTEXT_ADD_TO_ANDROID_TV = 30124
#     CONTEXT_PIN_TO_HP = 30125
#     CONTEXT_REMOVE = 30126
#     CONTEXT_ADD_TO_LIBRARY = 30128
#     CONTEXT_ADD_TO_LIBRARY_WITH_SUB = 30129
#     CONTEXT_REMOVE_FROM_SUB = 30130
#     CONTEXT_DEL_PREF_LANG = 30132
#     CONTEXT_ADD_PREF_LANG = 30133
# 
#     EMPTY_HISTORY_H1 = 30117
#     EMPTY_HISTORY_L1 = 30118
# 
# 
#     @staticmethod
#     def interpolate(id):
#         return '${}'.format(id)
# 
#     @staticmethod
#     def txt(id):
#         return get_localized_string(id)


import xbmcvfs
import xbmc
from resources.lib.common.logger import debug
from resources.lib.gui.dialog import dyesno, dok, dinput
from xbmcgui import INPUT_NUMERIC
from resources.lib.kodiutils import translate_path, get_system_platform, jsonrpc
from resources.lib.language import Strings




def set_kodi_cache_size():
    free_mem = xbmc.getInfoLabel('System.FreeMemory')
    free_mem = int(free_mem) / 1e6 if 'MB' not in free_mem else int(free_mem.replace('MB', ''))
    msg = Strings.txt(Strings.SETUP_VIDEO_CACHE_MSG1)
    user_input = dinput(msg.format(free_mem), '80', type=INPUT_NUMERIC)
    debug('user input: {}'.format(user_input))
    if user_input == '':
        return
    coefficient = int('{}'.format(user_input).replace('%', ''))
    coefficient = 80 if coefficient is None or 1 < coefficient > 80 else coefficient
    filename = translate_path('special://userdata/advancedsettings.xml')

    debug('cache size: {} * {}% / 3 = {}, '.format(free_mem, coefficient, int(free_mem / 3 * (coefficient/100))))
    if xbmcvfs.exists(filename):

# Path: resources/lib/common/logger.py
# def debug(text):
#     xbmc.log('[{}] {}'.format(APP, text), xbmc.LOGDEBUG)
# 
# Path: resources/lib/gui/dialog.py
# def dyesno(heading='heading', message='message', yeslabel='yes', nolabel='no', autoclose=None):
#     if autoclose is not None:
#         autoclose = autoclose * 1000
#     else:
#         autoclose = 0
#     if SYSTEM_VERSION > 18:
#         return dialog().yesno(heading=heading, message=message, nolabel=nolabel, yeslabel=yeslabel,
#                               autoclose=autoclose)
#     return dialog().yesno(heading=heading, line1=message, nolabel=nolabel, yeslabel=yeslabel,
#                           autoclose=autoclose)
# 
# def dok(heading, message=None):
#     if SYSTEM_VERSION > 18:
#         return dialog().ok(heading=heading, message=message)
#     return dialog().ok(heading=heading, line1=message)
# 
# def dinput(heading=None, default='', type=xbmcgui.INPUT_ALPHANUM):
#     return dialog().input(heading=heading, defaultt=default, type=type)
# 
# Path: resources/lib/kodiutils.py
# def translate_path(path):
#     try:
#         return xbmcvfs.translatePath(path)
#     except:
#         return xbmc.translatePath(path)
# 
# def get_system_platform():
#     platform = "unknown"
#     if xbmc.getCondVisibility('system.platform.linux') and not xbmc.getCondVisibility('system.platform.android'):
#         platform = "linux"
#     elif xbmc.getCondVisibility('system.platform.linux') and xbmc.getCondVisibility('system.platform.android'):
#         platform = "android"
#     elif xbmc.getCondVisibility('system.platform.uwp'):
#         platform = "uwp"
#     elif xbmc.getCondVisibility('system.platform.windows'):
#         platform = "windows"
#     elif xbmc.getCondVisibility('system.platform.osx'):
#         platform = "osx"
#     elif xbmc.getCondVisibility('system.platform.ios'):
#         platform = "ios"
#     return platform
# 
# def jsonrpc(**kwargs):
#     from json import dumps, loads
#     if kwargs.get('id') is None:
#         kwargs.update(id=0)
#     if kwargs.get('jsonrpc') is None:
#         kwargs.update(jsonrpc='2.0')
#     return loads(xbmc.executeJSONRPC(dumps(kwargs)))
# 
# Path: resources/lib/language.py
# class Strings:
#     SETUP_VIDEO_CACHE_MSG1 = 30120
#     SETUP_VIDEO_CACHE_MSG2 = 30121
#     SETUP_VIDEO_CACHE_MSG3 = 30122
#     SETUP_VIDEO_CACHE_MSG4 = 30123
# 
#     INTRO_STEP1_H1 = 30100
#     INTRO_STEP1_L1 = 30101
# 
#     INTRO_STEP2_H1 = 30012
# 
#     INTRO_STEP3_H1 = 30013
# 
#     INTRO_STEP4_H1 = 30102
#     INTRO_STEP4_L1 = 30103
# 
#     INTRO_STEP5_H1 = 30104
#     INTRO_STEP5_L1 = 30105
# 
#     KRASKA_NOTIFY_LOW_DAYS_LEFT = 30106
#     KRASKA_NOTIFY_CREDENTIAL_H1 = 30107
#     KRASKA_NOTIFY_CREDENTIAL_L1 = 30108
#     KRASKA_NOTIFY_NO_SUBSCRIPTION = 30131
# 
#     SYSTEM_H1 = 30109
#     SYSTEM_DEBUG_ENABLED = 30110
#     SYSTEM_DEBUG_DISABLED = 30111
#     SYSTEM_NOT_INSTALLED_FROM_REPOSITORY = 30127
# 
#     RESOLVE_ERROR_H1 = 30112
#     RESOLVE_ERROR_L1 = 30113
# 
#     SYSTEM_API_ERROR_L1 = 30114
# 
#     CONTEXT_PIN_UNPIN = 30115
#     CONTEXT_SELECT_STREAM = 30116
#     CONTEXT_DOWNLOAD = 30119
#     CONTEXT_ADD_TO_ANDROID_TV = 30124
#     CONTEXT_PIN_TO_HP = 30125
#     CONTEXT_REMOVE = 30126
#     CONTEXT_ADD_TO_LIBRARY = 30128
#     CONTEXT_ADD_TO_LIBRARY_WITH_SUB = 30129
#     CONTEXT_REMOVE_FROM_SUB = 30130
#     CONTEXT_DEL_PREF_LANG = 30132
#     CONTEXT_ADD_PREF_LANG = 30133
# 
#     EMPTY_HISTORY_H1 = 30117
#     EMPTY_HISTORY_L1 = 30118
# 
# 
#     @staticmethod
#     def interpolate(id):
#         return '${}'.format(id)
# 
#     @staticmethod
#     def txt(id):
#         return get_localized_string(id)


# Path: resources/lib/common/kodivideocache.py
import xbmcvfs
import xbmc
from resources.lib.common.logger import debug
from resources.lib.gui.dialog import dyesno, dok, dinput
from xbmcgui import INPUT_NUMERIC
from resources.lib.kodiutils import translate_path, get_system_platform, jsonrpc
from resources.lib.language import Strings





def set_kodi_cache_size():
    free_mem = xbmc.getInfoLabel('System.FreeMemory')
    free_mem = int(free_mem) / 1e6 if 'MB' not in free_mem else int(free_mem.replace('MB', ''))
    msg = Strings.txt(Strings.SETUP_VIDEO_CACHE_MSG1)
    user_input = dinput(msg.format(free_mem), '80', type=INPUT_NUMERIC)
    debug('user input: {}'.format(user_input))
    if user_input == '':
        return
    coefficient = int('{}'.format(user_input).replace('%', ''))
    coefficient = 80 if coefficient is None or 1 < coefficient > 80 else coefficient
    filename = translate_path('special://userdata/advancedsettings.xml')

    debug('cache size: {} * {}% / 3 = {}, '.format(free_mem, coefficient, int(free_mem / 3 * (coefficient/100))))
    if xbmcvfs.exists(filename):

        res = dyesno('WARNING', Strings.txt(Strings.SETUP_VIDEO_CACHE_MSG2))