# Path: epymc/extapi/onlinevideo.py
# ACT_DEFAULT = 0
# ACT_NONE = 1
# ACT_FOLDER = 2
# ACT_MORE = 3
# ACT_PLAY = 4
# ACT_SEARCH = 5
# def state_get():
# def language_get():
# def item_add(next_state, label, url, info=None, icon=None, poster=None, action=ACT_DEFAULT):
# def play_url(url):
# def report_error(msg):
# def local_resource(_file_, res):
# def fetch_url(url, headers=None, parser=None):
# def ydl_executable():
# def call_ydl(url):
# def url_encode(params):
# def seconds_to_duration(seconds):
# def relative_date(date):
# def format_date(date):


from epymc.extapi.onlinevideo import api_version, state_get, \
    fetch_url, play_url, item_add, call_ydl, local_resource, format_date, \
    ACT_NONE, ACT_FOLDER, ACT_MORE, ACT_PLAY, ACT_SEARCH

# this is the first page, show fixed seasons list
if STATE == ST_HOME:
    for i in range(1, 20):
        item_add(ST_EPISODES_LIST, label=_('Season {0}').format(i),
                 url='{}season-{}'.format(json_base, i),
                 poster=local_resource(__file__, 'season{}.jpg'.format(i)))


# show the episodes of a single season
elif STATE == ST_EPISODES_LIST:
    season_data = fetch_url(URL, parser='json')
    for num, episode_data in enumerate(season_data['results'], start=1):
        title = '{}. {}'.format(num, episode_data['title'])
        poster = episode_data['images']
        air_date = int(episode_data['originalAirDate'])
        info = '<title>{}: {}</title><br>' \
               '<name>{}:</name> {}<br>{}'.format(
            _('Episode {0}').format(num), episode_data['title'],
            _('First aired'), format_date(air_date),
            episode_data['description'])
        item_add(ST_ACTS_LIST, title, url=episode_data['_url']['default'],
                 info=info,
                 poster=poster)


# show the list of acts for a single episode (can be played directly)
elif STATE == ST_ACTS_LIST:
    urls = call_ydl(URL).splitlines()
    for i, act_url in enumerate(urls, start=1):

# Path: epymc/extapi/onlinevideo.py
# ACT_DEFAULT = 0
# ACT_NONE = 1
# ACT_FOLDER = 2
# ACT_MORE = 3
# ACT_PLAY = 4
# ACT_SEARCH = 5
# def state_get():
# def language_get():
# def item_add(next_state, label, url, info=None, icon=None, poster=None, action=ACT_DEFAULT):
# def play_url(url):
# def report_error(msg):
# def local_resource(_file_, res):
# def fetch_url(url, headers=None, parser=None):
# def ydl_executable():
# def call_ydl(url):
# def url_encode(params):
# def seconds_to_duration(seconds):
# def relative_date(date):
# def format_date(date):


# Path: epymc/plugins/onlinevideo/southparkstudios/southparkstudios.py
from epymc.extapi.onlinevideo import api_version, state_get, \
    fetch_url, play_url, item_add, call_ydl, local_resource, format_date, \
    ACT_NONE, ACT_FOLDER, ACT_MORE, ACT_PLAY, ACT_SEARCH


# this is the first page, show fixed seasons list
if STATE == ST_HOME:
    for i in range(1, 20):
        item_add(ST_EPISODES_LIST, label=_('Season {0}').format(i),
                 url='{}season-{}'.format(json_base, i),
                 poster=local_resource(__file__, 'season{}.jpg'.format(i)))


# show the episodes of a single season
elif STATE == ST_EPISODES_LIST:
    season_data = fetch_url(URL, parser='json')
    for num, episode_data in enumerate(season_data['results'], start=1):
        title = '{}. {}'.format(num, episode_data['title'])
        poster = episode_data['images']
        air_date = int(episode_data['originalAirDate'])
        info = '<title>{}: {}</title><br>' \
               '<name>{}:</name> {}<br>{}'.format(
            _('Episode {0}').format(num), episode_data['title'],
            _('First aired'), format_date(air_date),
            episode_data['description'])
        item_add(ST_ACTS_LIST, title, url=episode_data['_url']['default'],
                 info=info,
                 poster=poster)


# show the list of acts for a single episode (can be played directly)
elif STATE == ST_ACTS_LIST:
    urls = call_ydl(URL).splitlines()
    for i, act_url in enumerate(urls, start=1):

        act_icon = local_resource(__file__, 'act{}.jpg'.format(i))