# -*- coding: utf-8 -*-

import sys
import urllib
import urlparse

# import xbmc
import xbmcgui
import xbmcplugin

import aci

# Get the plugin url in plugin:// notation.
_url = sys.argv[0]
# Get the plugin handle as an integer number.
_handle = int(sys.argv[1])

# Get an instance of ACI.
ATV = aci.ACI()
ATV.load_aci()

# Encode user agent headers for video.
user_agent_headers = urllib.urlencode({'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:47.0) Gecko/20100101 '
                                                     'Firefox/47.0 FirePHP/0.7.4',
                                       'X-Requested-With': 'ShockwaveFlash/22.0.0.192'
                                       })


def get_url(**kwargs):
    """
    Create a URL for calling the plugin recursively from the given set of keyword arguments.

    :param kwargs: "argument=value" pairs
    :type kwargs: dict
    :return: plugin call URL
    :rtype: str
    """
    return '{0}?{1}'.format(_url, urllib.urlencode(kwargs))


def get_categories():
    """
    Get the list of video categories.

    Here you can insert some parsing code that retrieves
    the list of video categories (e.g. 'Movies', 'TV-shows', 'Documentaries' etc.)
    from some site or server.

    .. note:: Consider using `generator functions <https://wiki.python.org/moin/Generators>`_
        instead of returning lists.

    :return: The list of video categories
    :rtype: types.GeneratorType
    """
    return ATV.aci.iterkeys()


def get_videos(category):
    """
    Get the list of video files/streams.

    Here you can insert some parsing code that retrieves
    the list of video streams in the given category from some site or server.

    .. note:: Consider using `generators functions <https://wiki.python.org/moin/Generators>`_
        instead of returning lists.

    :param category: Category name
    :type category: str
    :return: the list of videos in the category
    :rtype: list
    """
    return ATV.aci[category]


def list_categories():
    """
    Create the list of video categories in the Kodi interface.
    """
    # Set plugin category. It is displayed in some skins as the name
    # of the current section.
    xbmcplugin.setPluginCategory(_handle, 'ACI')
    # Set plugin content. It allows Kodi to select appropriate views
    # for this type of content.
    xbmcplugin.setContent(_handle, 'videos')
    # Get video categories
    categories = get_categories()

    # Iterate through categories
    for category in categories:
        # xbmc.log(category.encode("utf-8"), xbmc.LOGNOTICE)

        # Create a list item with a text label and a thumbnail image.
        list_item = xbmcgui.ListItem(label=category.title())

        # Set graphics (thumbnail, fanart, banner, poster, landscape etc.) for the list item.
        # Here we use the same image for all items for simplicity's sake.
        # In a real-life plugin you need to set each image accordingly.
        list_item.setArt({'thumb': "icon.png",
                          'icon': "icon.png",
                          'fanart': "icon.png"})

        # Set additional info for the list item.
        # Here we use a category name for both properties for for simplicity's sake.
        # setInfo allows to set various information for an item.
        # For available properties see the following link:
        # https://codedocs.xyz/xbmc/xbmc/group__python__xbmcgui__listitem.html#ga0b71166869bda87ad744942888fb5f14
        # 'mediatype' is needed for a skin to display info for this ListItem correctly.
        list_item.setInfo('video', {'title': category.title(),
                                    'genre': category.title(),
                                    'mediatype': 'video'})

        # Create a URL for a plugin recursive call.
        # Example: plugin://plugin.video.example/?action=listing&category=[category name]
        url = get_url(action="listing", category=category)
        # is_folder = True means that this item opens a sub-list of lower level items.
        is_folder = True

        # Add our item to the Kodi virtual folder listing.
        xbmcplugin.addDirectoryItem(_handle, url, list_item, is_folder)

    # Add a sort method for the virtual folder items (alphabetically, ignore articles)
    xbmcplugin.addSortMethod(_handle, xbmcplugin.SORT_METHOD_LABEL_IGNORE_THE)
    # Finish creating a virtual folder.
    xbmcplugin.endOfDirectory(_handle)


def list_videos(category):
    """
    Create the list of playable videos in the Kodi interface.

    :param category: Category name
    :type category: str
    """
    # Set plugin category. It is displayed in some skins as the name
    # of the current section.
    xbmcplugin.setPluginCategory(_handle, category)
    # Set plugin content. It allows Kodi to select appropriate views
    # for this type of content.
    xbmcplugin.setContent(_handle, 'videos')
    # Get the list of videos in the category.
    videos = get_videos(category)

    # Iterate through each video.
    for video_id in videos:
        # Get the video item to process.
        video_item = videos[video_id]

        # Create a list item with a text label and a thumbnail image.
        list_item = xbmcgui.ListItem(label=video_item["title"])

        # Set additional info for the list item.
        # 'mediatype' is needed for skin to display info for this ListItem correctly.
        list_item.setInfo('video', {'title': video_item["title"],
                                    'genre': category.title(),
                                    'mediatype': 'video'})

        # Set graphics (thumbnail, fanart, banner, poster, landscape etc.) for the list item.
        # Here we use the same image for all items for simplicity's sake.
        # In a real-life plugin you need to set each image accordingly.
        list_item.setArt({'thumb': video_item["thumbnail"],
                          'icon': video_item["thumbnail"],
                          'fanart': video_item["thumbnail"]
                          })

        # Set 'IsPlayable' property to 'true'.
        # This is mandatory for playable items!
        list_item.setProperty('IsPlayable', 'true')

        referer_header = urllib.urlencode({"Referer": video_item["location"]})
        video_item['url'] += '|%s&amp;%s' % (user_agent_headers, referer_header)

        # Create a URL for a plugin recursive call.
        # Example: plugin://plugin.video.example/?action=play&
        #                           video=[video url]
        url = get_url(action='play', video=video_item['url'])

        # video_url = 'plugin://plugin.video.f4mTester/?url=' + urllib.quote_plus(video['video']) + \
        #            '&amp;streamtype=HLSRETRY&name=' + urllib.quote_plus(video['name']) + \
        #            '&amp;|User-Agent=Mozilla/5.0 (Windows NT 10.0; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0 ' \
        #            'FirePHP/0.7.4&amp;X-Requested-With=ShockwaveFlash/22.0.0.192&amp;Referer=' + \
        #             urllib.quote_plus(video['reference'])
        # url = get_url(action='play', video=video_url)

        # Add the list item to a virtual Kodi folder.
        # is_folder = False means that this item won't open any sub-list.
        is_folder = False
        # Add our item to the Kodi virtual folder listing.
        xbmcplugin.addDirectoryItem(_handle, url, list_item, is_folder)

    # Add a sort method for the virtual folder items (alphabetically, ignore articles)
    xbmcplugin.addSortMethod(_handle, xbmcplugin.SORT_METHOD_LABEL_IGNORE_THE)
    # Finish creating a virtual folder.
    xbmcplugin.endOfDirectory(_handle)


def play_video(path):
    """
    Play a video by the provided path.

    :param path: Fully-qualified video URL
    :type path: str
    """
    # Create a playable item with a path to play.
    play_item = xbmcgui.ListItem(path=path)

    # Play with inputstream addon.
    play_item.setProperty('inputstreamaddon', 'inputstream.adaptive')
    play_item.setProperty('inputstream.adaptive.manifest_type', 'hls')

    # Pass the item to the Kodi player.
    xbmcplugin.setResolvedUrl(_handle, True, listitem=play_item)


def router(paramstring):
    """
    Router function that calls other functions
    depending on the provided paramstring

    :param paramstring: URL encoded plugin paramstring
    :type paramstring: str
    """
    # Parse a URL-encoded paramstring to the dictionary of
    # {<parameter>: <value>} elements
    params = dict(urlparse.parse_qsl(paramstring))
    # Check the parameters passed to the plugin
    if params:
        if params['action'] == 'listing':
            # Load the videos for aci.
            if params['category'] == "shows":
                ATV.update_aci_shows()
                print("Updated from main shows.")
            elif params['category'] == "cable":
                ATV.update_aci_cable()
                print("Updated from main cable.")
            elif params['category'] == "movies":
                ATV.update_aci_movies()
                print("Updated from main movies.")

            # Display the list of videos in a provided category.
            list_videos(params['category'])
        elif params['action'] == 'play':
            # Play a video from a provided URL.
            play_video(params['video'])
        else:
            # If the provided paramstring does not contain a supported action
            # we raise an exception. This helps to catch coding errors,
            # e.g. typos in action names.
            raise ValueError('Invalid paramstring: {0}!'.format(paramstring))
    else:
        # Load ATV.
        ATV.load_aci()

        # If the plugin is called from Kodi UI without any parameters,
        # display the list of video categories
        list_categories()


if __name__ == '__main__':
    # Call the router function and pass the plugin call parameters to it.
    # We use string slicing to trim the leading '?' from the plugin call paramstring
    router(sys.argv[2][1:])

[((19, 6, 19, 15), 'aci.ACI', 'aci.ACI', ({}, {}), '()', False, 'import aci\n'), ((23, 21, 26, 41), 'urllib.urlencode', 'urllib.urlencode', ({(23, 38, 26, 40): "{'User-Agent':\n    'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0 FirePHP/0.7.4'\n    , 'X-Requested-With': 'ShockwaveFlash/22.0.0.192'}"}, {}), "({'User-Agent':\n    'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0 FirePHP/0.7.4'\n    , 'X-Requested-With': 'ShockwaveFlash/22.0.0.192'})", False, 'import urllib\n'), ((82, 4, 82, 48), 'xbmcplugin.setPluginCategory', 'xbmcplugin.setPluginCategory', ({(82, 33, 82, 40): '_handle', (82, 42, 82, 47): '"""ACI"""'}, {}), "(_handle, 'ACI')", False, 'import xbmcplugin\n'), ((85, 4, 85, 44), 'xbmcplugin.setContent', 'xbmcplugin.setContent', ({(85, 26, 85, 33): '_handle', (85, 35, 85, 43): '"""videos"""'}, {}), "(_handle, 'videos')", False, 'import xbmcplugin\n'), ((123, 4, 123, 78), 'xbmcplugin.addSortMethod', 'xbmcplugin.addSortMethod', ({(123, 29, 123, 36): '_handle', (123, 38, 123, 77): 'xbmcplugin.SORT_METHOD_LABEL_IGNORE_THE'}, {}), '(_handle, xbmcplugin.SORT_METHOD_LABEL_IGNORE_THE)', False, 'import xbmcplugin\n'), ((125, 4, 125, 38), 'xbmcplugin.endOfDirectory', 'xbmcplugin.endOfDirectory', ({(125, 30, 125, 37): '_handle'}, {}), '(_handle)', False, 'import xbmcplugin\n'), ((137, 4, 137, 51), 'xbmcplugin.setPluginCategory', 'xbmcplugin.setPluginCategory', ({(137, 33, 137, 40): '_handle', (137, 42, 137, 50): 'category'}, {}), '(_handle, category)', False, 'import xbmcplugin\n'), ((140, 4, 140, 44), 'xbmcplugin.setContent', 'xbmcplugin.setContent', ({(140, 26, 140, 33): '_handle', (140, 35, 140, 43): '"""videos"""'}, {}), "(_handle, 'videos')", False, 'import xbmcplugin\n'), ((192, 4, 192, 78), 'xbmcplugin.addSortMethod', 'xbmcplugin.addSortMethod', ({(192, 29, 192, 36): '_handle', (192, 38, 192, 77): 'xbmcplugin.SORT_METHOD_LABEL_IGNORE_THE'}, {}), '(_handle, xbmcplugin.SORT_METHOD_LABEL_IGNORE_THE)', False, 'import xbmcplugin\n'), ((194, 4, 194, 38), 'xbmcplugin.endOfDirectory', 'xbmcplugin.endOfDirectory', ({(194, 30, 194, 37): '_handle'}, {}), '(_handle)', False, 'import xbmcplugin\n'), ((205, 16, 205, 43), 'xbmcgui.ListItem', 'xbmcgui.ListItem', (), '', False, 'import xbmcgui\n'), ((212, 4, 212, 64), 'xbmcplugin.setResolvedUrl', 'xbmcplugin.setResolvedUrl', (), '', False, 'import xbmcplugin\n'), ((38, 34, 38, 58), 'urllib.urlencode', 'urllib.urlencode', ({(38, 51, 38, 57): 'kwargs'}, {}), '(kwargs)', False, 'import urllib\n'), ((120, 8, 120, 71), 'xbmcplugin.addDirectoryItem', 'xbmcplugin.addDirectoryItem', ({(120, 36, 120, 43): '_handle', (120, 45, 120, 48): 'url', (120, 50, 120, 59): 'list_item', (120, 61, 120, 70): 'is_folder'}, {}), '(_handle, url, list_item, is_folder)', False, 'import xbmcplugin\n'), ((150, 20, 150, 63), 'xbmcgui.ListItem', 'xbmcgui.ListItem', (), '', False, 'import xbmcgui\n'), ((170, 25, 170, 78), 'urllib.urlencode', 'urllib.urlencode', ({(170, 42, 170, 77): "{'Referer': video_item['location']}"}, {}), "({'Referer': video_item['location']})", False, 'import urllib\n'), ((189, 8, 189, 71), 'xbmcplugin.addDirectoryItem', 'xbmcplugin.addDirectoryItem', ({(189, 36, 189, 43): '_handle', (189, 45, 189, 48): 'url', (189, 50, 189, 59): 'list_item', (189, 61, 189, 70): 'is_folder'}, {}), '(_handle, url, list_item, is_folder)', False, 'import xbmcplugin\n'), ((225, 18, 225, 49), 'urlparse.parse_qsl', 'urlparse.parse_qsl', ({(225, 37, 225, 48): 'paramstring'}, {}), '(paramstring)', False, 'import urlparse\n')]