using EVCB_OCPP.WSServer.Jobs;
using Microsoft.Extensions.DependencyInjection;
using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EVCB_OCPP.WSServer
{
    public static class HostedProtalServer
    {
        public static void AddProtalServer(this IServiceCollection services)
        {
            services.AddSingleton<ProtalServer>();
            services.AddHostedService<ProtalServer>(p => p.GetRequiredService<ProtalServer>());
            services.AddProtalServerJob();
        }

        public static void AddProtalServerJob(this IServiceCollection services)
        {
            services.AddQuartz(q => {
                q.UseMicrosoftDependencyInjectionJobFactory();

                var ServerUpdateJobKey = new JobKey("job1", "group1");
                q.AddJob<ServerUpdateJob>(opts => { opts.WithIdentity(ServerUpdateJobKey); });

                q.AddTrigger(opts =>
                {
                    opts
                    .ForJob(ServerUpdateJobKey)
                    .WithIdentity("trigger1", "group1")
                    .StartNow()
                    .WithSimpleSchedule(x => x
                        .WithIntervalInMinutes(3)
                        .RepeatForever());
                });

                var ServerSetFeeJobKey = new JobKey("job2", "group1");
                q.AddJob<ServerSetFeeJob>(opts => { opts.WithIdentity(ServerSetFeeJobKey); });

                q.AddTrigger(opts =>
                {
                    opts
                    .ForJob(ServerSetFeeJobKey)
                    .WithIdentity("trigger2", "group1")
                    .StartNow()
                    .WithSimpleSchedule(x => x
                        .WithIntervalInMinutes(1)
                        .RepeatForever());
                });

                var ServerMessageJobKey = new JobKey("job3", "group1");
                q.AddJob<ServerMessageJob>(opts => { opts.WithIdentity(ServerMessageJobKey); });

                q.AddTrigger(opts =>
                {
                    opts
                    .ForJob(ServerMessageJobKey)
                    .WithIdentity("trigger3", "group1")
                    .StartNow()
                    .WithSimpleSchedule(x => x
                        .WithInterval(TimeSpan.FromMilliseconds(500))
                        .RepeatForever());
                });

                var HeartBeatCheckJobbKey = new JobKey("job4", "group1");
                q.AddJob<HeartBeatCheckJob>(opts => { opts.WithIdentity(HeartBeatCheckJobbKey); });

                q.AddTrigger(opts =>
                {
                    opts
                    .ForJob(HeartBeatCheckJobbKey)
                    .WithIdentity("trigger4", "group1")
                    .StartNow()
                    .WithSimpleSchedule(x => x
                        .WithIntervalInSeconds(30)
                        .RepeatForever());
                });

                var ServerWeatherNotificationJobKey = new JobKey("job5", "group1");
                q.AddJob<ServerWeatherNotificationJob>(opts => { opts.WithIdentity(ServerWeatherNotificationJobKey); });

                q.AddTrigger(opts =>
                {
                    opts
                    .ForJob(ServerWeatherNotificationJobKey)
                    .WithIdentity("trigger5", "group1")
                    .StartNow()
                    .WithSimpleSchedule(x => x
                        .WithIntervalInMinutes(1)
                        .RepeatForever());
                });

                var HealthCheckTriggerJobKey = new JobKey("job6", "group1");
                q.AddJob<HealthCheckTriggerJob>(opts => { opts.WithIdentity(HealthCheckTriggerJobKey); });

                q.AddTrigger(opts =>
                {
                    opts
                    .ForJob(HealthCheckTriggerJobKey)
                    .WithIdentity("trigger6", "group1")
                    .StartNow()
                    .WithSimpleSchedule(x => x
                        .WithIntervalInMinutes(1)
                        .RepeatForever());
                });


                var SmartChargingJobKey = new JobKey("job7", "group1");
                q.AddJob<SmartChargingJob>(opts => { opts.WithIdentity(SmartChargingJobKey); });

                q.AddTrigger(opts =>
                {
                    opts
                    .ForJob(SmartChargingJobKey)
                    .WithIdentity("trigger7", "group1")
                    .StartNow()
                    .WithSimpleSchedule(x => x
                        .WithIntervalInMinutes(1)
                        .RepeatForever());
                });

                //var DenyModelCheckJobKey = new JobKey("job8", "group1");
                //q.AddJob<DenyModelCheckJob>(opts => { opts.WithIdentity(DenyModelCheckJobKey); });

                //q.AddTrigger(opts =>
                //{
                //    opts
                //    .ForJob(DenyModelCheckJobKey)
                //    .WithIdentity("trigger8", "group1")
                //    .StartNow()
                //    .WithSimpleSchedule(x => x
                //        .WithIntervalInMinutes(5)
                //        .RepeatForever());
                //});
            });

            services.AddQuartzHostedService(opt =>
            {
                opt.WaitForJobsToComplete = true;
            });
        }
    }
}
