#
#  engine
#
#  Playout Daemon for autoradio project
#
#
#  Copyright (C) 2017-2018 Gottfried Gaisbauer <gottfried.gaisbauer@servus.at>
#
#  This file is part of engine.
#
#  engine is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  any later version.
#
#  engine is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with engine. If not, see <http://www.gnu.org/licenses/>.
#

import time
import logging
import threading

from libraries.enum.auraenumerations import ScheduleEntryType


class LiquidSoapInitThread(threading.Thread):
    logger = None
    socket = None
    active_entry = None
    liquidsoapcommunicator = None

    def __init__(self):
        threading.Thread.__init__(self)
        self.logger = logging.getLogger("AuraEngine")

    def run(self):
        try:
            # sleep needed, because the socket is created to slow by liquidsoap
            time.sleep(1)
            self.logger.info("Waited 1s for liquidsoap. Jez soit a si gspian")

            # enable lqs transaction
            self.liquidsoapcommunicator.enable_transaction()

            # reset channels and reload them
            self.liquidsoapcommunicator.channels = None
            channels = self.liquidsoapcommunicator.get_all_channels()

            # set every volume to 0
            for c in channels:
                self.liquidsoapcommunicator.channel_volume(c, "0")

            # select all channels
#            for c in channels:
#                self.liquidsoapcommunicator.channel_activate(c, True)

            # setting init params
            self.liquidsoapcommunicator.playlist_push(self.liquidsoapcommunicator.config.get("install_dir") + "/configuration/blank.flac")
            self.liquidsoapcommunicator.set_http_url("http://stream.fro.at/fro-128.ogg")

            # wait another second. lqs really starts slow..
            time.sleep(1)

            # set active
            if self.active_entry is not None:
                self.logger.info("LiquidSoapInitThread sets activechannel: "+str(self.active_entry))

                channel = self.active_entry.type
                if channel != "" and channel is not None:
                    self.liquidsoapcommunicator.http_start_stop(channel == ScheduleEntryType.STREAM)
                    self.liquidsoapcommunicator.channel_volume(channel.value, self.active_entry.volume)
            else:
                self.logger.warning("No active entry in the scheduler! Is a programme loaded?")

            self.liquidsoapcommunicator.disable_transaction()
        except Exception as e:
            self.logger.critical("Liquidsoap connection ERROR! Restart LQ Server! Reason: "+str(e))