using System;
using System.Configuration;
using System.Linq;
using System.Threading;
using log4net;
using NightScout;
using NightscoutLibrary.Configuration;
using Quobject.SocketIoClientDotNet.Client;

namespace NightScoutAlert
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            if (args.Any(arg => arg.Equals("--test-audio")))
            {
                Console.WriteLine("Starting audio test...");
                RaspberryPiAudioAlert alerter = new RaspberryPiAudioAlert();
                alerter.StartAlert();
                Thread.Sleep(3000);
                alerter.StopAlerts();
                return;
            }
            Quobject.EngineIoClientDotNet.Modules.LogManager.Enabled = true;

            //NightscoutTestConfiguration configuration = new NightscoutTestConfiguration();
            String nightscoutEndpoint = ConfigurationManager.AppSettings["NightscoutEndpoint"];
            String nightscoutCredentials = ConfigurationManager.AppSettings["NightscoutCredentials"];
            if (String.IsNullOrEmpty(nightscoutEndpoint) || String.IsNullOrEmpty(nightscoutCredentials))
            {
                Console.WriteLine("The nightscout endpoint and credentials must be filled into the App.config");
                return;
            }
            MonoCompatibleNightscoutAlertConfiguration configuration = new MonoCompatibleNightscoutAlertConfiguration(nightscoutEndpoint, nightscoutCredentials);
            RaspberryPIButtonSource buttonSource = null;

            try
            {
                if (!String.IsNullOrEmpty(ConfigurationManager.AppSettings["ButtonSourceEnabled"]) &&
                    Boolean.Parse(ConfigurationManager.AppSettings["ButtonSourceEnabled"]))
                {
                    buttonSource = new RaspberryPIButtonSource(1);
                    Console.WriteLine("Button source made");
                }
            }
            catch (Exception)
            {}
            
            NightscoutDataSource dataSource =null;
            RaspberryPiAudioAlert audioAlerter = null;

            if (buttonSource != null)
            {
                dataSource = new NightscoutDataSource(configuration,
                    log4net.LogManager.GetLogger("NightscoutAlert"), buttonSource);
                audioAlerter = new RaspberryPiAudioAlert(buttonSource);
            }
            else
            {
                dataSource = new NightscoutDataSource(configuration,
                    log4net.LogManager.GetLogger("NightscoutAlert"));
                audioAlerter = new RaspberryPiAudioAlert();
            }
            ILog log = log4net.LogManager.GetLogger("NightscoutAlertMain");
            log.Debug("Log started");
            ConsoleAlerter consoleAlerter = new ConsoleAlerter();
            //
            
            NightscoutAlerter nightscoutAlerter = new NightscoutAlerter(dataSource,
                new MultiAlerter(new IAlerter[] {consoleAlerter, audioAlerter, buttonSource}));

            //var socket = IO.Socket(configuration.NightscoutBaseURL);

            //socket.On(Socket.EVENT_CONNECT, () =>
            //{
            //    Console.WriteLine("Connected...");
            //    JObject authRequest = new JObject();
            //    authRequest.Add("client", "web");

            //    socket.Emit("authorize", authRequest);
            //});
            //socket.On(Socket.EVENT_ERROR, (obj) =>
            //{
            //    Console.WriteLine(obj.ToString());
            //});

            //socket.On("dataUpdate", (data) =>
            //{
            //    Console.WriteLine(data);
            //});

            //socket.On("alarm", (data) =>
            //{
            //    Console.WriteLine(data.ToString());
            //});

            //socket.On(Socket.EVENT_MESSAGE, (data) =>
            //{
            //    Console.WriteLine(data.ToString());
            //});

            //socket.On("urgent_alarm", (data) =>
            //{
            //    Console.WriteLine(data.ToString());
            //});

            //socket.On("clear_alarm", (data) =>
            //{
            //    Console.WriteLine(data.ToString());
            //});
            if (args.Any(arg => arg.Equals("--test-alerts")))
            {
                Console.WriteLine("Starting alert test...");
                //RaspberryPiAudioAlert alerter = new RaspberryPiAudioAlert();
                var alerter = new MultiAlerter(new IAlerter[] { consoleAlerter, audioAlerter, buttonSource });
                alerter.StartAlert();
                Thread.Sleep(3000);
                alerter.StopAlerts();
                return;

            }
            Console.WriteLine("Started");
            Console.ReadLine();
        }
    }
}
