from pygametk.logger import Logger

from pylaunchr.service.handler import ServiceCommandHandler

from plugin.twitch.data import TwitchGame


class TwitchHandler(ServiceCommandHandler):

    # TODO: Use inherited variant
    def handle_event(self, event):
        Logger("twitch").debug("Received event")

        callback = event.args["callback"]
        callback_args = event.args["callback_args"]
        result = event.args["result"]

        if "exception" in result:
            Logger("twitch").warning("Failed with {}", result["exception"])

        callback(result, *callback_args)

    def get_game(self, name, callback, callback_args=[]):
        Logger("twitch").debug("Get game {}", name)
        self._put("get_game", {"name": name}, callback, callback_args)

    def get_game_streams(self, game, limit, offset, callback, callback_args=[]):
        Logger("twitch").debug("Get game streams for {}", game)
        name = game.name if isinstance(game, TwitchGame) else game
        args = {"name": name, "limit": limit, "offset": offset}
        self._put("get_game_streams", args, callback, callback_args)

    def tick(self, tick):
        return False
