using Backend.Models;
using BackendWorkerService.Quartz;
using BackendWorkerService.Quartz.Jobs;
using BackendWorkerService.Services.Implement;
using BackendWorkerService.Services.Interface;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Quartz;
using Quartz.Impl;
using Quartz.Spi;
using Repository.BackendRepository.Implement;
using Repository.BackendRepository.Interface;
using Repository.BaseRepository.Implement;
using Repository.BaseRepository.Interface;
using Repository.FrontendRepository.Implement;
using Repository.FrontendRepository.Interface;
using Repository.Helper;
using Repository.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BackendWorkerService
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .UseWindowsService()
                .ConfigureServices((hostContext, services) =>
                {
                    IConfiguration configuration = hostContext.Configuration;

                    services.AddLogging(
                    builder =>
                    {
                        builder.AddFilter("Microsoft", LogLevel.Warning)
                               .AddFilter("System", LogLevel.Warning)
                               .AddFilter("NToastNotify", LogLevel.Warning)
                               .AddConsole();
                    });

                    //services.AddHostedService<Worker>();

                    #region �T���q��

                    #region SMPT�t�m
                    services.AddTransient<ISendEmailService, SendEmailService>();
                    services.Configure<SMTPConfig>(configuration.GetSection("SMTPConfig"));
                    #endregion

                    #region SMS�t�m
                    services.AddTransient<ISendSMSService, SendSMSService>();
                    services.Configure<SMSConfig>(configuration.GetSection("SMSConfig"));
                    #endregion

                    #region Line Notify�t�m
                    services.AddTransient<ISendLineNotifyService, SendLineNotifyService>();
                    #endregion

                    #endregion �T���q��

                    #region DBHelper �`�J
                    services.Configure<DBConfig>(configuration.GetSection("DBConfig"));
                    services.AddTransient<IDatabaseHelper, DatabaseHelper>();
                    #endregion DBHelper �`�J

                    #region Repository �`�J
                    services.AddTransient<IBackendRepository, BackendRepository>();
                    services.AddTransient<IBackgroundServiceRepository, BackgroundServiceRepository>();
                    services.AddTransient<IFrontendRepository, FrontendRepository>();
                    services.AddTransient<IBaseRepository, BaseRepository>();
                    #endregion Repository �`�J

                    //�K�[Quartz�A��
                    services.AddTransient<IJobFactory, SingletonJobFactory>();
                    services.AddTransient<ISchedulerFactory, StdSchedulerFactory>();
                    services.AddHostedService<QuartzHostedService>();

                    



                    #region �I������p�e(�]�w1min����@��)
                    services.AddSingleton<ExecutionBackgroundServicePlanJob>();
                    services.AddSingleton(
                    new JobSchedule(jobType: typeof(ExecutionBackgroundServicePlanJob), cronExpression: configuration.GetValue<string>("BackgroundServiceCron:ExecutionBackgroundServicePlanJob"))
                    );
                    #endregion

                    #region �T���q��(�]�w�C 30min ����@��)
                    services.AddSingleton<MessageNotificationJob>();
                    services.AddSingleton(
                    new JobSchedule(jobType: typeof(MessageNotificationJob), cronExpression: configuration.GetValue<string>("BackgroundServiceCron:MessageNotificationJob"))
                    );
                    #endregion

                    #region �w�ɱN�S�w��ƪ��[�J�ܬ��e����(�]�w�C�� 2AM ����@��)
                    services.AddSingleton<RegularUpdateDBTableJob>();
                    services.AddSingleton(
                    new JobSchedule(jobType: typeof(RegularUpdateDBTableJob), cronExpression: configuration.GetValue<string>("BackgroundServiceCron:RegularUpdateDBTableJob"))
                    );
                    #endregion

                    #region ��Ƭ��e(�]�w�C 5min ����@��)
                    services.AddSingleton<DataDeliveryJob>();
                    services.AddSingleton(
                    new JobSchedule(jobType: typeof(DataDeliveryJob), cronExpression: configuration.GetValue<string>("BackgroundServiceCron:DataDeliveryJob"))
                    );
                    #endregion

                    #region �������޲z(�]�w�C 5�� ����@��)
                    services.AddSingleton<ParkingJob>();
                    services.AddSingleton(
                    new JobSchedule(jobType: typeof(ParkingJob), cronExpression: configuration.GetValue<string>("BackgroundServiceCron:ParkingJob"))
                    );
                    #endregion

                    #region �q���k��(�]�w�C �p�� ����@��)
                    services.AddSingleton<ArchiveElectricMeterHourJob>();
                    services.AddSingleton(
                    new JobSchedule(jobType: typeof(ArchiveElectricMeterHourJob), cronExpression: configuration.GetValue<string>("BackgroundServiceCron:ArchiveElectricMeterHourJob"))
                    );
                    #endregion
                    #region �q���k��(�]�w�C �� ����@��)
                    services.AddSingleton<ArchiveElectricMeterDayJob>();
                    services.AddSingleton(
                    new JobSchedule(jobType: typeof(ArchiveElectricMeterDayJob), cronExpression: configuration.GetValue<string>("BackgroundServiceCron:ArchiveElectricMeterDayJob"))
                    );
                    #endregion

                    #region �w�ɨ��o��HAPI
                    services.AddSingleton<Quartz.Jobs.WeatherAPIJob>();
                    services.AddSingleton(
                    new JobSchedule(jobType: typeof(Quartz.Jobs.WeatherAPIJob), cronExpression: configuration.GetValue<string>("BackgroundServiceCron:WeatherAPIJob"))
                    );
                    #endregion

                }).ConfigureLogging((hostContext, logFactory) => {
                    IConfiguration configuration = hostContext.Configuration;

                    //logFactory.AddFile("Logs/log-{Date}.txt");
                    logFactory.AddFile(configuration.GetValue<string>("LoggerPath") + "/log-{Date}.txt");
                });
    }
}