using System;
using System.Collections.Generic;
using System.Configuration;
using Gebhardt.Shared;
using Gebhardt.Shared.Process;
using Gebhardt.StoreWare.Wcs.Common.Unity;
using Unity;
using Unity.Resolution;

namespace Gebhardt.StoreWare.Wcs.ConveyorDispo
{
	internal class Haupt
	{
		[STAThread]
		private static void Main()
		{
			try
			{
				if (AppConfigVerifier.CheckAndWriteToLog())
				{
					ProcessManager manager = new(Convert.ToInt32(ConfigurationManager.AppSettings["ctrlTimer"]), true, ProcessClass.None, null);
					ProcessParameter parameter = new();
					var connections = new List<string> {parameter.Conn1, parameter.Conn2, parameter.Conn3, parameter.Conn4, parameter.Conn5, parameter.Conn6};
					string[] usedConnections = connections.FindAll(x => x != "leer").ToArray();

					IUnityContainer unityContainer = WcsContainerFactory.GetInstance();

					foreach (string connection in usedConnections)
					{
						string[] parts = connection.Split(':');
						string className = parts[0];

						switch (className)
						{
							case nameof(ToEmptyLeBuffer):
								manager.RegisterWorker(unityContainer.Resolve<ToEmptyLeBuffer>(new ParameterOverride("workInterval", Convert.ToInt32(ConfigurationManager.AppSettings["Intervall_ToEmptyLeBuffer"]))));
								break;

							case nameof(LoopOverloadDistribution):
								// Do not use LoopOverloadDistribution for ETRA
								break;
								manager.RegisterWorker(unityContainer.Resolve<LoopOverloadDistribution>(new ParameterOverride("workInterval", Convert.ToInt32(ConfigurationManager.AppSettings["Intervall_LoopOverloadDistribution"]))));
								break;

							case nameof(OrderManager):
								manager.RegisterWorker(unityContainer.Resolve<OrderManager>(new ParameterOverride("workInterval", Convert.ToInt32(ConfigurationManager.AppSettings["Intervall_OrderManager"]))));
								break;

							case nameof(StartInitialOrdersHost):
								manager.RegisterWorker(unityContainer.Resolve<StartInitialOrdersHost>(new ParameterOverride("workInterval", Convert.ToInt32(ConfigurationManager.AppSettings["Intervall_StartInitialOrdersHost"]))));
								break;

							default:
								throw new NotImplementedException(className);
						}
					}
					manager.RunWorkers();
				}
			}
			catch (Exception exception)
			{
				Log.WriteException(exception);
			}
		}
	}
}