#!/usr/bin/python3

#
#  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/>.
#

from libraries.database.broadcasts import *
import json
import logging
import sqlalchemy
import decimal

from modules.communication.liquidsoap.communicator import LiquidSoapCommunicator
from modules.monitoring.diskspace_watcher import DiskSpaceWatcher
from libraries.base.config import AuraConfig
from libraries.database.broadcasts import Schedule, ScheduleEntry
from modules.scheduling.scheduler import AuraScheduler, AuraCalendarService

def alchemyencoder(obj):
    """JSON encoder function for SQLAlchemy special classes."""
    if isinstance(obj, datetime.date):
        return obj.isoformat()
    elif isinstance(obj, decimal.Decimal):
        return float(obj)
    elif isinstance(obj, sqlalchemy.orm.state.InstanceState):
        return ""
    #elif isinstance(obj, Schedule):
    #    return json.dumps([obj._asdict()], default=alchemyencoder)
    else:
        return str(obj)

#    programme_as_string = json.dumps([se[0]._asdict()], default=alchemyencoder)
#    print(programme_as_string)

def start_diskspace_watcher():
    config = AuraConfig()
    config.read_config()
    diskspace_watcher = DiskSpaceWatcher(config.config, logging.getLogger("AuraEngine"), LiquidSoapCommunicator(config.config))
    diskspace_watcher.run()

def select_act_programme():
    # start_diskspace_watcher()
    # select_act_programme()
    config = AuraConfig()
    config.read_config()

    liquidsoapcommunicator = LiquidSoapCommunicator(config.config)
    sched = AuraScheduler(config.config)

    liquidsoapcommunicator.scheduler = sched
    sched.liquidsoapcommunicator = liquidsoapcommunicator

    programme = sched.load_programme_from_db()

    for show in programme:
        print(show)

def fadeout(lsc):
    entry = ScheduleEntry.select_act_programme()
    lsc.fade_out(entry, 2)

def fadein(lsc):
    entry = ScheduleEntry.select_act_programme()
    lsc.fade_in(entry, 1)

def fetch_new_programme():
    config = AuraConfig()
    config.read_config()
    acs = AuraCalendarService(config.config)


    queue = acs.get_queue()

    # start fetching thread
    acs.start()

    # wait for the end
    response = queue.get()

# # ## ## ## ## ## # #
# # ENTRY FUNCTION # #
# # ## ## ## ## ## # #
def main():
    fetch_new_programme()


# # ## ## ## ## ## ## # #
# # End ENTRY FUNCTION # #
# # ## ## ## ## ## ## # #


if __name__ == "__main__":
    main()