# Path: lib/libs/mediatypes.py
# TVSHOW = 'tvshow'
# MOVIE = 'movie'
# EPISODE = 'episode'
# SEASON = 'season'
# MOVIESET = 'set'
# MUSICVIDEO = 'musicvideo'
# ARTIST = 'artist'
# ALBUM = 'album'
# SONG = 'song'
# PREFERRED_SOURCE_SHARED = {'0': None, '1': 'fanart.tv'}
# PREFERRED_SOURCE_MEDIA = {'tvshows': ('thetvdb.com', (TVSHOW, SEASON, EPISODE)),
#     'movies': ('themoviedb.org', (MOVIE, MOVIESET)),
#     'music': ('theaudiodb.com', audiotypes),
#     'musicvideos': ('theaudiodb.com', (MUSICVIDEO,))}
# def get_artinfo(mediatype, arttype):
# def hack_mediaarttype(mediatype, arttype):
# def disabled(mediatype):
# def iter_every_arttype(mediatype):
# def downloadartwork(mediatype, arttype):
# def downloadanyartwork(mediatype):
# def _split_arttype(arttype):
# def generatethumb(mediatype):
# def haspreferred_source(mediatype):
# def ispreferred_source(mediatype, provider):
# def only_filesystem(mediatype):
# def update_settings():
# def _get_autolimit_from_setting(settingid):
# def _set_allmulti(always_multi):
# 
# Path: lib/libs/addonsettings.py
# DEFAULT_IMAGESIZE = '1'
# AVAILABLE_IMAGESIZES = {'0': (10000, 10000, 600), '1': (1920, 1080, 600), '2': (1280, 720, 480)}
# PROGRESS_DISPLAY_FULLPROGRESS = '0'
# PROGRESS_DISPLAY_WARNINGSERRORS = '1'
# PROGRESS_DISPLAY_NONE = '2' # Only add-on crashes
# EXCLUSION_PATH_TYPE_FOLDER = '0'
# EXCLUSION_PATH_TYPE_PREFIX = '1'
# EXCLUSION_PATH_TYPE_REGEX = '2'
# class Settings(object):
#     def __init__(self):
#     def update_useragent(self):
#     def update_settings(self):
#     def get_apikey(self, provider):
#     def get_apienabled(self, provider):
#     def get_api_config(self, provider):
#     def autoadd_episodes(self):
#     def autoadd_episodes(self, value):
# def get_projectkey(provider):
# 
# Path: lib/libs/pykodi.py
# def localize(messageid):
#     if isinstance(messageid, basestring):
#         result = messageid
#     elif messageid >= 32000 and messageid < 33000:
#         result = get_main_addon().getLocalizedString(messageid)
#     else:
#         result = xbmc.getLocalizedString(messageid)
#     return result.encode('utf-8') if isinstance(result, unicode) else result


import re
import xbmc
import xbmcgui
from lib.libs import mediatypes
from lib.libs.addonsettings import settings
from lib.libs.pykodi import localize as L

def prompt_for_artwork(mediatype, medialabel, availableart, monitor):
    if not availableart:
        return None, None

    arttypes = []
    for arttype, artlist in availableart.iteritems():
        if arttype.startswith('season.-1.'):
            # Ignore 'all' seasons artwork, as I can't set artwork for it with JSON
            continue
        label = arttype if not arttype.startswith('season.') else get_seasonlabel(arttype)
        for image in artlist:
            if image.get('existing'):
                arttypes.append({'arttype': arttype, 'label': label, 'count': len(artlist), 'url': image['url']})
                break
        if arttype not in (at['arttype'] for at in arttypes):
            arttypes.append({'arttype': arttype, 'label': label, 'count': len(artlist)})
    arttypes.sort(key=lambda art: sort_arttype(art['arttype']))
    typeselectwindow = ArtworkTypeSelector('DialogSelect.xml', settings.addon_path, arttypes=arttypes,
        medialabel=medialabel, show_refresh=mediatype in mediatypes.require_manualid)
    selectedarttype = None
    selectedart = None
    typelist = [at['arttype'] for at in arttypes]
    while selectedart is None and not monitor.abortRequested():
        # The loop shows the first window if viewer backs out of the second
        selectedarttype = typeselectwindow.prompt()
        if selectedarttype not in typelist:
            return selectedarttype, None
        if not selectedarttype:
            break

# Path: lib/libs/mediatypes.py
# TVSHOW = 'tvshow'
# MOVIE = 'movie'
# EPISODE = 'episode'
# SEASON = 'season'
# MOVIESET = 'set'
# MUSICVIDEO = 'musicvideo'
# ARTIST = 'artist'
# ALBUM = 'album'
# SONG = 'song'
# PREFERRED_SOURCE_SHARED = {'0': None, '1': 'fanart.tv'}
# PREFERRED_SOURCE_MEDIA = {'tvshows': ('thetvdb.com', (TVSHOW, SEASON, EPISODE)),
#     'movies': ('themoviedb.org', (MOVIE, MOVIESET)),
#     'music': ('theaudiodb.com', audiotypes),
#     'musicvideos': ('theaudiodb.com', (MUSICVIDEO,))}
# def get_artinfo(mediatype, arttype):
# def hack_mediaarttype(mediatype, arttype):
# def disabled(mediatype):
# def iter_every_arttype(mediatype):
# def downloadartwork(mediatype, arttype):
# def downloadanyartwork(mediatype):
# def _split_arttype(arttype):
# def generatethumb(mediatype):
# def haspreferred_source(mediatype):
# def ispreferred_source(mediatype, provider):
# def only_filesystem(mediatype):
# def update_settings():
# def _get_autolimit_from_setting(settingid):
# def _set_allmulti(always_multi):
# 
# Path: lib/libs/addonsettings.py
# DEFAULT_IMAGESIZE = '1'
# AVAILABLE_IMAGESIZES = {'0': (10000, 10000, 600), '1': (1920, 1080, 600), '2': (1280, 720, 480)}
# PROGRESS_DISPLAY_FULLPROGRESS = '0'
# PROGRESS_DISPLAY_WARNINGSERRORS = '1'
# PROGRESS_DISPLAY_NONE = '2' # Only add-on crashes
# EXCLUSION_PATH_TYPE_FOLDER = '0'
# EXCLUSION_PATH_TYPE_PREFIX = '1'
# EXCLUSION_PATH_TYPE_REGEX = '2'
# class Settings(object):
#     def __init__(self):
#     def update_useragent(self):
#     def update_settings(self):
#     def get_apikey(self, provider):
#     def get_apienabled(self, provider):
#     def get_api_config(self, provider):
#     def autoadd_episodes(self):
#     def autoadd_episodes(self, value):
# def get_projectkey(provider):
# 
# Path: lib/libs/pykodi.py
# def localize(messageid):
#     if isinstance(messageid, basestring):
#         result = messageid
#     elif messageid >= 32000 and messageid < 33000:
#         result = get_main_addon().getLocalizedString(messageid)
#     else:
#         result = xbmc.getLocalizedString(messageid)
#     return result.encode('utf-8') if isinstance(result, unicode) else result


# Path: lib/artworkselection.py
import re
import xbmc
import xbmcgui
from lib.libs import mediatypes
from lib.libs.addonsettings import settings
from lib.libs.pykodi import localize as L


def prompt_for_artwork(mediatype, medialabel, availableart, monitor):
    if not availableart:
        return None, None

    arttypes = []
    for arttype, artlist in availableart.iteritems():
        if arttype.startswith('season.-1.'):
            # Ignore 'all' seasons artwork, as I can't set artwork for it with JSON
            continue
        label = arttype if not arttype.startswith('season.') else get_seasonlabel(arttype)
        for image in artlist:
            if image.get('existing'):
                arttypes.append({'arttype': arttype, 'label': label, 'count': len(artlist), 'url': image['url']})
                break
        if arttype not in (at['arttype'] for at in arttypes):
            arttypes.append({'arttype': arttype, 'label': label, 'count': len(artlist)})
    arttypes.sort(key=lambda art: sort_arttype(art['arttype']))
    typeselectwindow = ArtworkTypeSelector('DialogSelect.xml', settings.addon_path, arttypes=arttypes,
        medialabel=medialabel, show_refresh=mediatype in mediatypes.require_manualid)
    selectedarttype = None
    selectedart = None
    typelist = [at['arttype'] for at in arttypes]
    while selectedart is None and not monitor.abortRequested():
        # The loop shows the first window if viewer backs out of the second
        selectedarttype = typeselectwindow.prompt()
        if selectedarttype not in typelist:
            return selectedarttype, None
        if not selectedarttype:
            break

        multi = mediatypes.get_artinfo(mediatype, selectedarttype)['multiselect']