"""Sdílený stav vstupu pluginu (addon, handle, URL, session) a základní UI helpery."""

import sys

import requests
import xbmcaddon
import xbmcgui

from resources.lib.utils.log_utils import get_logger

try:
    from urllib import urlencode
    from urlparse import parse_qsl, urlparse
except ImportError:
    from urllib.parse import urlencode
    from urllib.parse import parse_qsl, urlparse
try:
    from xbmc import translatePath
except ImportError:
    from xbmcvfs import translatePath

log = get_logger()

ADDON_ID = 'plugin.video.mmirousek_v3'
ADDON = xbmcaddon.Addon(ADDON_ID)
ADDON_NAME = ADDON.getAddonInfo('name')

BASE = 'https://webshare.cz'
UA = (
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
    "AppleWebKit/537.36 (KHTML, like Gecko) "
    "Chrome/81.0.4044.138 Safari/537.36"
)
HEADERS = {'User-Agent': UA, 'Referer': BASE}
_session = requests.Session()
_session.headers.update(HEADERS)

_url = sys.argv[0]
_handle = int(sys.argv[1])
_addon = xbmcaddon.Addon(ADDON_ID)
_profile = translatePath(_addon.getAddonInfo('profile'))
try:
    _profile = _profile.decode("utf-8")
except Exception:
    pass


def get_url(**kwargs):
    return '{0}?{1}'.format(_url, urlencode(kwargs, encoding='utf-8'))


def popinfo(
    message,
    heading=_addon.getAddonInfo('name'),
    icon=xbmcgui.NOTIFICATION_INFO,
    time=3000,
    sound=False,
):
    xbmcgui.Dialog().notification(heading, message, icon, time, sound=sound)


# Kořenové menu: neprovádět os.walk při každém otevření (ARCHITECTURE bod 3).
_ADDON_DATA_SIZE_CACHE_TTL_SEC = 6 * 3600
