import m3u8


class OriginChannels():

    def __init__(self, fhdhr, origin):
        self.fhdhr = fhdhr
        self.origin = origin

        self.base_api = 'https://localnow.com/api/dsp/live/epg'

    def get_channels(self):

        channel_list = []

        channels_json = self.fhdhr.web.session.get(self.base_api).json()["data"]["channels"]
        for channel_dict in channels_json:

            clean_station_item = {
                                 "name": channel_dict["name"],
                                 "callsign": channel_dict["slug"],
                                 "id": channel_dict["id"],
                                 "thumbnail": channel_dict["logo"],
                                 }
            channel_list.append(clean_station_item)

        return channel_list

    def get_channel_stream(self, chandict, stream_args):
        return None
        channels_json = self.fhdhr.web.session.get(self.base_api).json()
        origin_chandict = self.get_channel_dict(channels_json, "identifier", chandict["origin_id"])
        streamdict = self.get_channel_dict(origin_chandict["streams"], "StreamType", 'website')
        streamurl = streamdict['Url']
        if self.fhdhr.config.dict["origin"]["force_best"]:
            streamurl = self.m3u8_beststream(streamurl)

        stream_info = {"url": streamurl}

        return stream_info

    def get_channel_dict(self, chanlist, keyfind, valfind):
        return next(item for item in chanlist if item[keyfind] == valfind)

    def m3u8_beststream(self, m3u8_url):
        bestStream = None
        videoUrlM3u = m3u8.load(m3u8_url)
        if not videoUrlM3u.is_variant:
            return m3u8_url

        for videoStream in videoUrlM3u.playlists:
            if not bestStream:
                bestStream = videoStream
            elif videoStream.stream_info.bandwidth > bestStream.stream_info.bandwidth:
                bestStream = videoStream

        if not bestStream:
            return bestStream.absolute_uri
        else:
            return m3u8_url
