using CsharpHttpHelper;
using DeviceRepair.Models;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using DeviceRepair.Utils;

namespace DeviceRepairAndOptimization
{
    /// <summary>
    /// 后台任务调度
    /// </summary>
    public class Scheduler
    {
        public delegate void CallBackEventHandler(string args, object value);
        public event CallBackEventHandler CallBack;
        HttpHelper http = new HttpHelper();

        private static Scheduler scheduler;
        public static Scheduler Instance
        {
            get
            {
                if (scheduler == null)
                    scheduler = new Scheduler();
                return scheduler;
            }
        }

        Dictionary<string, HttpItem> httpItems = new Dictionary<string, HttpItem>
        {
            {
                "SchedulerPlanTips",new HttpItem  { URL = $"http://{DeviceRepair.Utils.Config.Configurations.Properties.ServiceIP}/{DeviceRepair.Utils.Config.Configurations.Properties.ServiceName}/Api/Plan/SchedulerPlanTips"}
            },
            {
                "CurrentYearPlanSchedule",new HttpItem { URL = $"http://{DeviceRepair.Utils.Config.Configurations.Properties.ServiceIP}/{DeviceRepair.Utils.Config.Configurations.Properties.ServiceName}/Api/Plan/GetCurrentYearPlanSchedule", ContentType = "application/json;charset=utf-8"}
            }
        };


        /// <summary>
        /// 启动
        /// </summary>
        public void Run()
        {
            foreach (HttpItem item in httpItems.Values)
            {
                item.Header.Add("auth", GlobalInfo.token);
            }

            Task.Run(() =>
            {
                while (true)
                {
                    Console.WriteLine(DateTime.Now.ToLongTimeString());
                    GetSchedulerPlanTips();
                    GetCurrentYearPlanSchedule();
                    Task.Delay(3000).Wait();
                }
            });
        }

        /// <summary>
        /// 获取当年度 - 当月待保养计划 - 数量
        /// </summary>
        protected void GetSchedulerPlanTips()
        {
            HttpResult Rtn = http.GetHtml(httpItems["SchedulerPlanTips"]);
            try
            {
                APIResponseData apiResponseData = Rtn.GetApiResponseData();
                if (apiResponseData.IsSuccess)
                {
                    CallBack?.Invoke("SchedulerPlanTips", apiResponseData.Data);
                }
                else
                {
                    CallBack?.Invoke("SchedulerPlanTips", apiResponseData.Message);
                }
            }
            catch (Exception)
            {
                CallBack?.Invoke("SchedulerPlanTips", HttpHelper.StripHTML(Rtn.Html));
            }
        }

        protected void GetCurrentYearPlanSchedule()
        {
            HttpResult Rtn = http.GetHtml(httpItems["CurrentYearPlanSchedule"]);
            try
            {
                APIResponseData apiResponseData = Rtn.GetApiResponseData();
                if (apiResponseData.IsSuccess)
                {
                    object Value = apiResponseData.ToDeserializeObject<DeviceRepair.Models.Common.CurrentYearPlanSchedule>();
                    CallBack?.Invoke("CurrentYearPlanSchedule", Value);
                }
                else
                {
                    CallBack?.Invoke("CurrentYearPlanSchedule", apiResponseData.Message);
                }
            }
            catch (Exception)
            {
                CallBack?.Invoke("CurrentYearPlanSchedule", HttpHelper.StripHTML(Rtn.Html));
            }
        }
    }
}
