__author__ = 'dev'

import urwid
from ui.console.button.exit_button import ExitButton
from ui.console.button.play_stream_button import PlayStreamButton
from config.stream_list import web_stream_list as wsl
from web_streams.web_stream_player import WebStreamPlayer
from ui.console.menu.console_menu import ConsoleMenu, FOCUS_MAP_REVERSED

LISTBOX_TITLE = "Streamlist"

class StreamListMenu(ConsoleMenu):

    def __init__(self):
        self.__player = WebStreamPlayer()
        super().__init__()

    def _create_widget(self):
        body = [urwid.Text(LISTBOX_TITLE), urwid.Divider()]
        for stream in wsl.keys():
            body.append(urwid.AttrMap(PlayStreamButton(stream, self.__player), None, focus_map=FOCUS_MAP_REVERSED))
        body.append(urwid.AttrMap(ExitButton(), None, focus_map=FOCUS_MAP_REVERSED))
        listbox = urwid.ListBox(urwid.SimpleFocusListWalker(body))

        txt = urwid.AttrMap(urwid.Text(u"Hello World"),None)
        txtBox = urwid.Pile([txt])
        txtBox.widget_list.append(txt)

        return urwid.Pile([listbox, txtBox])

    def _clean_menu(self):
        if self.__player is not None:
            self.__player.stop_stream()