__author__ = 'dev'
import subprocess
from config.stream_list import web_stream_list as wsl
from config.commands import web_stream_commands as wcmd
from utility.subprocess_dict import SubprocessDict

SDT_OUT_DST = open('/dev/null', 'w')
SDT_ERR_DST = open('/dev/null', 'w')

class WebStreamPlayer:

    def __init__(self, player_command=wcmd["web_stream_player_command"], stream_list=wsl):
        self.__player_command = player_command
        self.__stream_list = stream_list
        self.__process_list = SubprocessDict()
        self.__current_stream_name = None

    def get_current_stream_name(self):
        return self.__current_stream_name

    def play_stream(self, stream_name):
        if stream_name in self.__stream_list:
            self.stop_stream()
            self.__process_list.add_process([self.__player_command, self.__stream_list[stream_name]],
                                            stdout=SDT_OUT_DST, stderr=SDT_ERR_DST)
            self.__current_stream_name = stream_name

    def stop_stream(self):
        self.__process_list.clear_dict()
        self.__current_stream_name = None