using BarBusinessLogic.BusinessLogics;
using BarBusinessLogic.OfficePackage.Implements;
using BarBusinessLogic.OfficePackage;
using BarContracts.BusinessLogicContracts;
using BarView.Forms;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
using BarContracts.BusinessLogicsContracts;
using BarBusinessLogic.MailWorker;
using BarContracts.BindingModels;
using BarContracts.DI;

namespace BarView
{
	internal static class Program
	{
		[STAThread]
		static void Main()
		{
			ApplicationConfiguration.Initialize();
			InitDependency();

			try
			{
				var MailSender = DependencyManager.Instance.Resolve<AbstractMailWorker>();
				MailSender?.MailConfig(new MailConfigBindingModel
				{
					MailLogin = System.Configuration.ConfigurationManager.AppSettings["MailLogin"] ?? string.Empty,
					MailPassword = System.Configuration.ConfigurationManager.AppSettings["MailPassword"] ?? string.Empty,
					SmtpClientHost = System.Configuration.ConfigurationManager.AppSettings["SmtpClientHost"] ?? string.Empty,
					SmtpClientPort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["SmtpClientPort"]),
					PopHost = System.Configuration.ConfigurationManager.AppSettings["PopHost"] ?? string.Empty,
					PopPort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["PopPort"])
				});

				var Timer = new System.Threading.Timer(new TimerCallback(MailCheck!), null, 0, 100000);
			}
			catch (Exception ex)
			{
				var Logger = DependencyManager.Instance.Resolve<ILogger>();
				Logger?.LogError(ex, "Ошибка работы с почтой");
			}

			Application.Run(DependencyManager.Instance.Resolve<FormMain>());
		}

		private static void MailCheck(object obj) => DependencyManager.Instance.Resolve<AbstractMailWorker>()?.CheckMailAsync();

		private static void InitDependency()
		{
			DependencyManager.InitDependency();
			DependencyManager.Instance.AddLogging(Option =>
			{
				Option.SetMinimumLevel(LogLevel.Information);
				Option.AddNLog("nlog.config");
			});

			DependencyManager.Instance.RegisterType<IComponentLogic, ComponentLogic>();
			DependencyManager.Instance.RegisterType<IOrderLogic, OrderLogic>();
			DependencyManager.Instance.RegisterType<ICocktailLogic, CocktailLogic>();
			DependencyManager.Instance.RegisterType<IReportLogic, ReportLogic>();
			DependencyManager.Instance.RegisterType<IClientLogic, ClientLogic>();
			DependencyManager.Instance.RegisterType<IImplementerLogic, ImplementerLogic>();
			DependencyManager.Instance.RegisterType<IWorkProcess, WorkModeling>();
			DependencyManager.Instance.RegisterType<IMessageInfoLogic, MessageInfoLogic>();
			DependencyManager.Instance.RegisterType<IBackUpLogic, BackUpLogic>();

			DependencyManager.Instance.RegisterType<AbstractSaveToWord, SaveToWord>();
			DependencyManager.Instance.RegisterType<AbstractSaveToExcel, SaveToExcel>();
			DependencyManager.Instance.RegisterType<AbstractSaveToPdf, SaveToPdf>();
			DependencyManager.Instance.RegisterType<AbstractMailWorker, MailKitWorker>(true);

			DependencyManager.Instance.RegisterType<FormMain>();
			DependencyManager.Instance.RegisterType<FormComponent>();
			DependencyManager.Instance.RegisterType<FormComponents>();
			DependencyManager.Instance.RegisterType<FormCreateOrder>();
			DependencyManager.Instance.RegisterType<FormCocktail>();
			DependencyManager.Instance.RegisterType<FormCocktailComponent>();
			DependencyManager.Instance.RegisterType<FormCocktails>();
			DependencyManager.Instance.RegisterType<FormReportCocktailComponents>();
			DependencyManager.Instance.RegisterType<FormReportOrders>();
			DependencyManager.Instance.RegisterType<FormClients>();
			DependencyManager.Instance.RegisterType<FormImplementers>();
			DependencyManager.Instance.RegisterType<FormImplementer>();
			DependencyManager.Instance.RegisterType<FormMail>();
		}
	}
}
