using Microsoft.Extensions.Logging;
using Quartz;
using SolarPower.Models;
using SolarPower.Repository.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace SolarPower.Quartz.Jobs
{
    [DisallowConcurrentExecution]
    public class ExceptionSchedule : IJob
    {
        private readonly ILogger<ExceptionSchedule> logger;
        private readonly IOverviewRepository overviewRepository;
        private readonly INoticeScheduleRepository noticeScheduleRepository;
        private readonly IUserRepository userRepository;

        public ExceptionSchedule(ILogger<ExceptionSchedule> logger, IOverviewRepository overviewRepository,INoticeScheduleRepository noticeScheduleRepository,IUserRepository userRepository)
        {
            this.logger = logger;
            this.overviewRepository = overviewRepository;
            this.noticeScheduleRepository = noticeScheduleRepository;
            this.userRepository = userRepository;
        }

        public async Task Execute(IJobExecutionContext context)
        {
            try
            {
                var ExceptionList = await overviewRepository.GetEmailExceptionList();
                if(ExceptionList.Count > 0 )
                {
                    var ExceptionListex = ExceptionList.Where(x => x.normalTime == "1970-01-01 08:00:00" && DateTime.Now.Subtract(DateTime.Parse(x.dev_time)).Hours >= 4).ToList();


                    foreach (var Exception in ExceptionListex)
                    {
                        var UserListWithPowerstation = await overviewRepository.GetUserListWithPowerstation(Exception.PowerStationId);

                        foreach (var user in UserListWithPowerstation)
                        {
                            var Content = $"電站名稱:{Exception.PowerStationName}" + "<br>" +
                                $"設備編號:{Exception.errDevice}" + "<br>" +
                                $"異常ID編號:{Exception.id}" + "<br>" +
                                $"異常類別:{Exception.alarmClassName}" + "<br>" +
                                $"設備訊息:{Exception.errMsg}" + "<br>" +
                                $"發生時間:{Exception.dev_time}" ;
                            NoticeSchedule DaySchedule = new NoticeSchedule()
                            {
                                UserId = user.Id,
                                EmailType = 4,
                                RecipientEmail = user.Email,
                                Subject = "異常通知",
                                Content = Content,
                                RecipientName = user.Name,
                                Type = 1,
                                ExceptionId = Exception.id
                            };
                            List<string> properties = new List<string>()
                            {
                                "UserId",
                                "EmailType",
                                "RecipientEmail",
                                "Subject",
                                "Content",
                                "RecipientName",
                                "Type",
                                "ExceptionId"
                            };
                            await noticeScheduleRepository.AddOneAsync(DaySchedule, properties);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                logger.LogError("【{0}】{1}", nameof(logger), exception.Message);
            }
        }
    }
}