#!./bin/python3

from handler import BaseHandler
from handler.lib.pymplb import MPlayer

class StreamHandler(BaseHandler):

    def __init__(self, config, display):
        super().__init__(config, display)
        self._mplayer = None


    def setPlaylistItem(self, item):
        '''
        Called with the selected item.
        '''
        super().setPlaylistItem(item)

        if self._mplayer is None:
            self._mplayer = MPlayer()
            self._mplayer.loadfile(self.item["url"])


    def update(self, text=None, highlight=-1, title=None, img=None):
        '''
        Called when the handler should update the display.
        '''
        super().update(title=[
            "",
            "Stream:",
            self.item["name"]
        ], img=self.item.get("img"))


    def stop(self):
        '''
        Called when the handler should stop.
        '''
        if self._mplayer is not None:
            self._mplayer.quit()
            self._mplayer = None


    def emitEvent(self, event):
        '''
        Called when an event has happened.
        '''
        super().emitEvent(event)
