using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using Quartz;
using SolarPower.Models;
using SolarPower.Repository.Interface;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
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;
        private readonly IPowerStationRepository powerStationRepository;
        private readonly IConfiguration Configuration;

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

        public async Task Execute(IJobExecutionContext context)
        {
            try
            {
                var ExceptionTimes = this.Configuration.GetValue<int>("ExceptionTimes");
                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)).Minutes >= ExceptionTimes).ToList();


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

                        var Content = $"電站名稱:{Exception.PowerStationName}" + "<br>" +
                                $"設備編號:{Exception.errDevice}" + "<br>" +
                                $"異常ID編號:{Exception.id}" + "<br>" +
                                $"異常類別:{Exception.alarmClassName}" + "<br>" +
                                $"設備訊息:{Exception.errMsg}" + "<br>";
                        if(Exception.errMsgT == "d")
                        {
                            Content += $"當前數值:{Exception.errValue}" + "<br>";
                        }
                        Content += $"發生時間:{Exception.dev_time}";
                        foreach (var user in UserListWithPowerstation)
                        {
                            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);

                            
                        }

                        var powerstation = await powerStationRepository.GetOneAsync(Exception.PowerStationId);
                        if (powerstation.line_token != null)
                        {
                            Content = " %0D%0A " + Content.Replace("<br>", " %0D%0A ");
                            CallLineToken(Content, powerstation.line_token);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                logger.LogError("【{0}】{1}", nameof(logger), exception.Message);
            }
        }

        public string CallLineToken(string message, string LineToken)
        {
            try
            {
                HttpWebRequest Postrequest = (HttpWebRequest)WebRequest.Create("https://notify-api.line.me/api/notify?message=" + message);
                Postrequest.Method = "POST";
                Postrequest.Headers.Add("Authorization", "Bearer " + LineToken);
                Postrequest.PreAuthenticate = true;
                HttpWebResponse response = (HttpWebResponse)Postrequest.GetResponse();
                var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
                var final = JObject.Parse(responseString);
                var get = final["status"].ToString();
                if (get != "200")
                {
                    logger.LogError("CallLineToken發送Line接收失敗");
                    //Logger.LogError("【" + controllerName + "/" + actionName + "】" + "CallLineToken發送Line接收失敗");
                }
                return get;
            }
            catch (Exception ex)
            {
                logger.LogError("【{0}】CallLineToken: {1}", nameof(logger), ex.Message);
                //Logger.LogError("【" + controllerName + "/" + actionName + "】" + "CallLineToken:" + ex);
                throw ex;
            }
        }

    }
}