using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
using FishFactoryBusinessLogic.BusinessLogics;
using FishFactoryBusinessLogic.OfficePackage.Implements;
using FishFactoryBusinessLogic.OfficePackage;
using FishFactoryContracts.BusinessLogicsContracts;
using FishFactoryContracts.StoragesContracts;
using FishFactoryDatabaseImplement.Implements;
using FishFactoryBusinessLogic.MailWorker;
using FishFactoryContracts.BindingModels;


namespace FishFactoryView
{
    internal static class Program
    {
        private static ServiceProvider? _serviceProvider;
        public static ServiceProvider? ServiceProvider => _serviceProvider;
        /// <summary>
        /// Главная точка входа для приложения.
        /// </summary>
        [STAThread]
        static void Main()
        {
            // To customize application configuration such as set high DPI settings or default font,
            // see https://aka.ms/applicationconfiguration.
            ApplicationConfiguration.Initialize();
            
            var services = new ServiceCollection();
            ConfigureServices(services);
            _serviceProvider = services.BuildServiceProvider();
            try
            {
                var mailSender = _serviceProvider.GetService<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 = _serviceProvider.GetService<ILogger>();
                logger?.LogError(ex, "ошибка работы с почтой");
            }
            Application.Run(_serviceProvider.GetRequiredService<FormMain>());
        }

        private static void ConfigureServices(ServiceCollection services)
        {
            services.AddLogging(option =>
            {
                option.SetMinimumLevel(LogLevel.Information);
                option.AddNLog("nlog.config");
            });
            services.AddTransient<IClientStorage, ClientStorage>();
            services.AddTransient<IIngredientStorage, IngredientStorage>();
            services.AddTransient<IOrderStorage, OrderStorage>();
            services.AddTransient<ICannedStorage, CannedStorage>();
            services.AddTransient<IImplementerStorage, ImplementerStorage>();
            services.AddTransient<IMessageInfoStorage, MessageInfoStorage>();
            services.AddTransient<IClientLogic, ClientLogic>();
            services.AddTransient<IIngredientLogic, IngredientLogic>();
            services.AddTransient<IOrderLogic, OrderLogic>();
            services.AddTransient<ICannedLogic, CannedLogic>();
            services.AddTransient<IReportLogic, ReportLogic>();
            services.AddTransient<IImplementerLogic, ImplementerLogic>();
            services.AddTransient<IMessageInfoLogic, MessageInfoLogic>();
            services.AddTransient<IWorkProcess, WorkModeling>();
            services.AddSingleton<AbstractMailWorker, MailKitWorker>();

            services.AddTransient<AbstractSaveToExcel, SaveToExcel>();
            services.AddTransient<AbstractSaveToWord, SaveToWord>();
            services.AddTransient<AbstractSaveToPdf, SaveToPdf>();

            services.AddTransient<FormMain>();
            services.AddTransient<FormClients>();
            services.AddTransient<FormIngredient>();
            services.AddTransient<FormIngredients>();
            services.AddTransient<FormCreateOrder>();
            services.AddTransient<FormCanned>();
            services.AddTransient<FormCannedIngredients>();
            services.AddTransient<FormListCanned>();
            services.AddTransient<FormReportCannedIngredients>();
            services.AddTransient<FormReportOrders>();
            services.AddTransient<FormImplementers>();
            services.AddTransient<FormImplementer>();
            services.AddTransient<FormMails>();
        }
		
		private static void MailCheck(object obj) => ServiceProvider?.GetService<AbstractMailWorker>()?.MailCheck();
    }
}
