﻿using Quest.ExpressQ.Properties;
using System;
using System.Diagnostics;
using System.Reflection;
using System.ServiceProcess;

namespace Quest.ExpressQ
{
    class Program
    {
        static void Main(string[] args)
        {
            var desktopMode = Parameters.ParameterExists(args, "-desktop");

            ExpressQManagerParams parameters = new ExpressQManagerParams
            {
                ActiveMQ = Parameters.GetParameter(args, "-activemq", Settings.Default.ActiveMQ), 
                InactivityTimeout = Parameters.GetParameter(args, "-timeout", Settings.Default.InactivityTimeout),
                IniTemplate = Parameters.GetParameter(args, "-ini", Settings.Default.IniFilePath),
                Persistent = Parameters.GetParameter(args, "-persist", Settings.Default.Persistent),
                Topic = Parameters.GetParameter(args, "-topic", Settings.Default.Topic),
                TTL = Parameters.GetParameter(args, "-ttl", Settings.Default.TTL),
                Environment = Settings.Default.Environment,
                EnvironmentOverride = Parameters.GetParameter<string>(args, "-env", null),
                Format = Parameters.GetParameter(args, "-format", Settings.Default.Format),
                QueueName = Parameters.GetParameter(args, "-queue", Settings.Default.QueueName),
                Password = Parameters.GetParameter(args, "-pwd", Settings.Default.Password),
                Username = Parameters.GetParameter(args, "-user", Settings.Default.Username),
                ProcessorQueueName = Parameters.GetParameter(args, "-target", Settings.Default.ProcessorQueueName),
                Device = Parameters.GetParameter<string>(args, "-device", null),
                RestartClients = Parameters.GetParameter<bool>(args, "-restartmode", Settings.Default.RestartClients)
            };

            // single client mode
            if (parameters.Device != null)
            {
                Logger.Write("Starting in single client mode", "ExpressQGateway");
                var client = new ExpressQClient(parameters);
                client.Start();
                Console.ReadLine();
            }

            ServiceBase[] ServicesToRun;
            EqService thisService = new EqService(parameters);

            ServicesToRun = new ServiceBase[] { thisService };

            if (desktopMode)
            {
                var maj = Assembly.GetExecutingAssembly().GetName().Version.Major;
                var min = Assembly.GetExecutingAssembly().GetName().Version.Minor;
                Logger.Write($"-----------------------------------------", TraceEventType.Information, "Quest.Cmd");
                Logger.Write($"--    Q U E S T  EXPRESSQ GATEWAY        ", TraceEventType.Information, "Quest.Cmd");
                Logger.Write($"--               V {maj}.{min}           ", TraceEventType.Information, "Quest.Cmd");
                Logger.Write($"--  Copyright (C) 2019  Marcus Poulton   ", TraceEventType.Information, "Quest.Cmd");
                Logger.Write($"", TraceEventType.Information, "Quest.Cmd");
                Logger.Write($"-- This program is free software: you can redistribute it and / or modify", TraceEventType.Information, "Quest.Cmd");
                Logger.Write($"-- it under the terms of the GNU General Public License as published by", TraceEventType.Information, "Quest.Cmd");
                Logger.Write($"-- the Free Software Foundation, either version 3 of the License, or", TraceEventType.Information, "Quest.Cmd");
                Logger.Write($"-- (at your option) any later version.", TraceEventType.Information, "Quest.Cmd");
                Logger.Write($"", TraceEventType.Information, "Quest.Cmd");
                Logger.Write($"-- This program is distributed in the hope that it will be useful,", TraceEventType.Information, "Quest.Cmd");
                Logger.Write($"-- but WITHOUT ANY WARRANTY; without even the implied warranty of", TraceEventType.Information, "Quest.Cmd");
                Logger.Write($"-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the", TraceEventType.Information, "Quest.Cmd");
                Logger.Write($"-- GNU General Public License for more details.", TraceEventType.Information, "Quest.Cmd");
                Logger.Write($"-----------------------------------------", TraceEventType.Information, "Quest.Cmd");

                Logger.Write("Running", "ExpressQGateway");
                thisService.StartRunning();
                Console.ReadLine();
                thisService.StopRunning();
            }
            else
            {
                ServiceBase.Run(ServicesToRun);
            }
        }
    }
}
