# Path: resources/lib/controller/basecontroller.py
# class BaseController(object):
#     def render(self, name, args=None):
#         if args:
#             return router.render(name, args=args)
#         else:
#             return router.render(name)
# 
#     def route_exists(self, name):
#         return router.route_exists(name)
# 
#     def cleanup(self):
#         raise NotImplementedError("Cleanup needs to be implemented.")
# 
# def route(name):
#     def decorator(func):
#         xbmc.log("[script.luna.router]: Adding route with name %s to cache" % name)
#         assert name not in router._routes_cache, "Route with name %s already exists on the same class" % name
#         router._routes_cache[name] = func
#         return func
# 
#     return decorator
# 
# Path: resources/lib/views/gamelist.py
# class GameList(xbmcgui.WindowXML):
#     def __new__(cls, *args, **kwargs):
#         return super(GameList, cls).__new__(cls, "gamelist.xml", xbmcaddon.Addon().getAddonInfo('path'))
# 
#     def __init__(self, controller, host, game_list):
#         super(GameList, self).__init__("gamelist.xml", xbmcaddon.Addon().getAddonInfo('path'))
#         self.controller = controller
#         self.host = host
#         self.games = game_list
#         self.list = None
#         self.host_online_img = None
#         self.host_offline_img = None
#         self.host_name_label = None
# 
#     def onInit(self):
#         self.games.sort(key=lambda x: x['label'], reverse=False)
#         self.list = self.getControl(50)
#         self.host_online_img = self.getControl(2)
#         self.host_offline_img = self.getControl(3)
#         self.host_name_label = self.getControl(4)
# 
#         if self.host.state == self.host.STATE_OFFLINE:
#             self.host_online_img.setVisible(False)
#         else:
#             self.host_offline_img.setVisible(False)
# 
#         self.host_name_label.setLabel("%s | GPU: %s" % (self.host.name, self.host.gpu_type))
# 
#         self.build_list()
#         self.setFocusId(50)
# 
#     def build_list(self):
#         items = []
# 
#         for game in self.games:
#             item = xbmcgui.ListItem()
#             item.setLabel(game['label'])
#             item.setIconImage(game['icon'])
#             item.setThumbnailImage(game['thumbnail'])
#             item.setInfo('video', game['info'])
#             item.setProperty('icon', game['thumbnail'])
#             item.setProperty('fanart', game['properties']['fanart_image'])
#             item.setProperty('id', game['properties']['id'])
# 
#             items.append(item)
# 
#         self.list.addItems(items)
# 
#     def onAction(self, action):
#         focus_item_id = self.getFocusId()
#         focus_item = None
# 
#         if focus_item_id:
#             focus_item = self.getControl(focus_item_id)
# 
#         if action == xbmcgui.ACTION_NAV_BACK:
#             self.close()
# 
#         elif focus_item and focus_item == self.list and action.getId() == xbmcgui.ACTION_CONTEXT_MENU:
#             current_item = self.list.getListItem(self.list.getSelectedPosition())
#             fanart_cache = current_item.getProperty('fanart')
#             cover_cache = current_item.getProperty('icon')
# 
#             current_game = self.controller.get_game_by_id(self.host, current_item.getProperty('id'))
# 
#             refresh = self.controller.render('gamecontext_menu', {'host': self.host, 'list_item': current_item, 'game': current_game})
# 
#             loaded_game = self.controller.get_game_by_id(self.host, current_item.getProperty('id'))
# 
#             if fanart_cache != loaded_game.get_selected_fanart().get_original():
#                 self.list.getSelectedItem().setProperty('fanart', loaded_game.get_selected_fanart().get_original())
# 
#             if cover_cache != loaded_game.get_selected_poster():
#                 self.list.getSelectedItem().setProperty('icon', loaded_game.get_selected_poster())
#                 self.list.getSelectedItem().setThumbnailImage(loaded_game.get_selected_poster())
# 
#             if refresh:
#                 self.controller.refresh_list(self.host)
# 
#         elif focus_item and focus_item == self.list and action == xbmcgui.ACTION_SELECT_ITEM:
#             current_item = self.list.getListItem(self.list.getSelectedPosition())
#             loaded_game = self.controller.get_game_by_id(self.host, current_item.getProperty('id'))
# 
#             self.controller.launch_game(loaded_game)
# 
#     def update(self, games):
#         self.games = games
#         self.games.sort(key=lambda x: x['label'], reverse=False)
#         self.list.reset()
#         self.build_list()
#         return


import threading
        import xbmcgui
from resources.lib.controller.basecontroller import BaseController, route
from resources.lib.views.gamelist import GameList



class GameListController(BaseController):
    def __init__(self, game_manager, game_helper, moonlight_helper, logger):
        self.game_manager = game_manager
        self.game_helper = game_helper
        self.moonlight_helper = moonlight_helper
        self.logger = logger
        self.window = None

    @route(name='list')
    def index_action(self, host):
        self.window = GameList(controller=self, host=host, game_list=self.game_helper.get_games_as_list(host))
        self.window.doModal()
        del self.window


# Path: resources/lib/controller/basecontroller.py
# class BaseController(object):
#     def render(self, name, args=None):
#         if args:
#             return router.render(name, args=args)
#         else:
#             return router.render(name)
# 
#     def route_exists(self, name):
#         return router.route_exists(name)
# 
#     def cleanup(self):
#         raise NotImplementedError("Cleanup needs to be implemented.")
# 
# def route(name):
#     def decorator(func):
#         xbmc.log("[script.luna.router]: Adding route with name %s to cache" % name)
#         assert name not in router._routes_cache, "Route with name %s already exists on the same class" % name
#         router._routes_cache[name] = func
#         return func
# 
#     return decorator
# 
# Path: resources/lib/views/gamelist.py
# class GameList(xbmcgui.WindowXML):
#     def __new__(cls, *args, **kwargs):
#         return super(GameList, cls).__new__(cls, "gamelist.xml", xbmcaddon.Addon().getAddonInfo('path'))
# 
#     def __init__(self, controller, host, game_list):
#         super(GameList, self).__init__("gamelist.xml", xbmcaddon.Addon().getAddonInfo('path'))
#         self.controller = controller
#         self.host = host
#         self.games = game_list
#         self.list = None
#         self.host_online_img = None
#         self.host_offline_img = None
#         self.host_name_label = None
# 
#     def onInit(self):
#         self.games.sort(key=lambda x: x['label'], reverse=False)
#         self.list = self.getControl(50)
#         self.host_online_img = self.getControl(2)
#         self.host_offline_img = self.getControl(3)
#         self.host_name_label = self.getControl(4)
# 
#         if self.host.state == self.host.STATE_OFFLINE:
#             self.host_online_img.setVisible(False)
#         else:
#             self.host_offline_img.setVisible(False)
# 
#         self.host_name_label.setLabel("%s | GPU: %s" % (self.host.name, self.host.gpu_type))
# 
#         self.build_list()
#         self.setFocusId(50)
# 
#     def build_list(self):
#         items = []
# 
#         for game in self.games:
#             item = xbmcgui.ListItem()
#             item.setLabel(game['label'])
#             item.setIconImage(game['icon'])
#             item.setThumbnailImage(game['thumbnail'])
#             item.setInfo('video', game['info'])
#             item.setProperty('icon', game['thumbnail'])
#             item.setProperty('fanart', game['properties']['fanart_image'])
#             item.setProperty('id', game['properties']['id'])
# 
#             items.append(item)
# 
#         self.list.addItems(items)
# 
#     def onAction(self, action):
#         focus_item_id = self.getFocusId()
#         focus_item = None
# 
#         if focus_item_id:
#             focus_item = self.getControl(focus_item_id)
# 
#         if action == xbmcgui.ACTION_NAV_BACK:
#             self.close()
# 
#         elif focus_item and focus_item == self.list and action.getId() == xbmcgui.ACTION_CONTEXT_MENU:
#             current_item = self.list.getListItem(self.list.getSelectedPosition())
#             fanart_cache = current_item.getProperty('fanart')
#             cover_cache = current_item.getProperty('icon')
# 
#             current_game = self.controller.get_game_by_id(self.host, current_item.getProperty('id'))
# 
#             refresh = self.controller.render('gamecontext_menu', {'host': self.host, 'list_item': current_item, 'game': current_game})
# 
#             loaded_game = self.controller.get_game_by_id(self.host, current_item.getProperty('id'))
# 
#             if fanart_cache != loaded_game.get_selected_fanart().get_original():
#                 self.list.getSelectedItem().setProperty('fanart', loaded_game.get_selected_fanart().get_original())
# 
#             if cover_cache != loaded_game.get_selected_poster():
#                 self.list.getSelectedItem().setProperty('icon', loaded_game.get_selected_poster())
#                 self.list.getSelectedItem().setThumbnailImage(loaded_game.get_selected_poster())
# 
#             if refresh:
#                 self.controller.refresh_list(self.host)
# 
#         elif focus_item and focus_item == self.list and action == xbmcgui.ACTION_SELECT_ITEM:
#             current_item = self.list.getListItem(self.list.getSelectedPosition())
#             loaded_game = self.controller.get_game_by_id(self.host, current_item.getProperty('id'))
# 
#             self.controller.launch_game(loaded_game)
# 
#     def update(self, games):
#         self.games = games
#         self.games.sort(key=lambda x: x['label'], reverse=False)
#         self.list.reset()
#         self.build_list()
#         return


# Path: resources/lib/controller/gamelistcontroller.py
import threading
        import xbmcgui
from resources.lib.controller.basecontroller import BaseController, route
from resources.lib.views.gamelist import GameList




class GameListController(BaseController):
    def __init__(self, game_manager, game_helper, moonlight_helper, logger):
        self.game_manager = game_manager
        self.game_helper = game_helper
        self.moonlight_helper = moonlight_helper
        self.logger = logger
        self.window = None

    @route(name='list')
    def index_action(self, host):
        self.window = GameList(controller=self, host=host, game_list=self.game_helper.get_games_as_list(host))
        self.window.doModal()
        del self.window


    @route(name='launch')