using CsharpHttpHelper;
using DeviceRepair.DataAccess;
using DeviceRepair.Models;
using DeviceRepair.Models.Common;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace DeviceRepairAndOptimization.Biz
{
    /// <summary>
    /// 保养计划
    /// </summary>
    public class PlanManager
    {
        private static PlanManager manager;
        public static PlanManager Instance
        {
            get
            {
                if (manager == null)
                    manager = new PlanManager();
                return manager;
            }
        }

        /// <summary>
        /// 保养计划批量新增数据
        /// </summary>
        /// <param name="lst"></param>
        /// <returns></returns>
        public APIResponseData InsertDatas(List<DriveMaintencePlanInfo> lst)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "操作失败!" };
            try
            {
                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = ServiceRouteConstValue.PlanDataInsertRange,
                            Method = "Post",
                            ContentType = "application/json;charset=utf-8",
                            Postdata = JsonConvert.SerializeObject(lst)
                        });
                        break;
                    default:
                        apiResponseData = PlanAccess.Instance.InsertDatas(lst, GlobalInfo.OperationInfo);
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }


        /// <summary>
        /// 获取当前年月的待保修项
        /// </summary>
        /// <returns></returns>
        public APIResponseData GetCurrentMonthPlanTips()
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "未能获取到数据！" };
            try
            {
                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = ServiceRouteConstValue.GetCurrentMonthPlanItems,
                            Method = "Get",
                            ContentType = "application/json;charset=utf-8"
                        });
                        break;

                    default:
                        apiResponseData = PlanAccess.Instance.GetCurrentMonthPlanTips();
                        break;
                }
                return apiResponseData;
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 当前年度计划完成进度
        /// </summary>
        /// <param name="handler"></param>
        /// <returns></returns>
        public CurrentYearPlanSchedule GetCurrentYearPlanSchedule()
        {
            CurrentYearPlanSchedule schedule = null;
            APIResponseData apiResponseData = new APIResponseData { Code = -1 };
            try
            {
                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = ServiceRouteConstValue.GetCurrentYearPlanSchedule,
                            Method = "GET",
                            ContentType = "application/json;charset=utf-8"
                        });
                        break;
                    default:
                        apiResponseData = PlanAccess.Instance.GetCurrentYearPlanSchedule();
                        break;
                }

                if (apiResponseData.Code == 1)
                    schedule = JsonConvert.DeserializeObject<CurrentYearPlanSchedule>(apiResponseData.Data + "");
                return schedule;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }
            return schedule;
        }

        /// <summary>
        /// 获取当前年月的待保修项数量
        /// </summary>
        /// <param name="handler"></param>
        /// <returns></returns>
        public async Task<int> GetCurrentMonthPlanTipsCountAsync(ResultHandler handler)
        {
            int Count = -1;
            try
            {
                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        ApiHelper.Instance.SendMessageAsync(new HttpItem
                        {
                            URL = ServiceRouteConstValue.GetCurrentMonthPlanItemCount,
                            Method = "GET",
                            ContentType = "application/json;charset=utf-8"
                        }, handler);
                        break;

                    default:
                        APIResponseData apiResponseData = PlanAccess.Instance.GetCurrentMonthPlanTipsCountAsync();
                        Count = apiResponseData.ToInt();
                        break;
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }
            return Count;
        }

        /// <summary>
        /// 根据设备主键ID取指定年份计划数据
        /// </summary>
        /// <param name="Year"></param>
        /// <param name="EquipmentAutoID"></param>
        /// <returns></returns>
        public AnnualMaintenancePlan GetPlanByEquipmentPkAndYear(int Year, int EquipmentAutoID)
        {
            APIResponseData apiResponseData = null;
            try
            {
                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = $"{ServiceRouteConstValue.GetPlanByEquipmentPkAndYear}?Year={Year}&EquipmentAutoID={EquipmentAutoID}",
                            Method = "Get",
                            ContentType = "application/json; charset=utf-8"
                        });
                        break;
                    default:
                        apiResponseData = PlanAccess.Instance.GetAllPlans();
                        break;
                }

                if (apiResponseData.Code == 1)
                    return JsonConvert.DeserializeObject<AnnualMaintenancePlan>(apiResponseData.Data + "");
                else
                    throw new Exception(apiResponseData.Message);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取所有年计划
        /// </summary>
        /// <returns></returns>
        public List<AnnualMaintenancePlan> GetDatas()
        {
            APIResponseData apiResponseData = null;

            try
            {
                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = ServiceRouteConstValue.GetAllPlans,
                            Method = "Get",
                            ContentType = "application/json; charset=utf-8"
                        });
                        break;
                    default:
                        apiResponseData = PlanAccess.Instance.GetAllPlans();
                        break;
                }

                if (apiResponseData.Code == 1)
                    return JsonConvert.DeserializeObject<List<AnnualMaintenancePlan>>(apiResponseData.Data + "");
                else
                    throw new Exception(apiResponseData.Message);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 导出EXCEL的数据
        /// </summary>
        /// <param name="Year"></param>
        /// <returns></returns>
        public List<View_YearsMaintenancePlansExport> ExportXlsxDatas(int Year)
        {
            APIResponseData apiResponseData = null;

            try
            {
                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = ServiceRouteConstValue.GetExcelExportDatasByYear + "?Year=" + Year,
                            Method = "Get",
                            ContentType = "application/json; charset=utf-8"
                        });
                        break;
                    default:
                        apiResponseData = PlanAccess.Instance.ExportXlsxDatas(Year);
                        break;
                }

                if (apiResponseData.Code == 1)
                    return JsonConvert.DeserializeObject<List<View_YearsMaintenancePlansExport>>(apiResponseData.Data + "");
                else
                    throw new Exception(apiResponseData.Message);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取当前计划是否存的保养记录
        /// </summary>
        /// <param name="EquipmentAutoID"></param>
        /// <param name="Year"></param>
        /// <returns></returns>
        public APIResponseData GetPlanRecordProgress(int EquipmentAutoID, int Year)
        {
            APIResponseData apiResponseData = null;
            try
            {
                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        apiResponseData = ApiHelper.Instance.SendMessage(
                            new HttpItem
                            {
                                URL = $"{ServiceRouteConstValue.GetPlanRecordProgress}?EquipmentAutoID={EquipmentAutoID}&Year={Year}",
                                Method = "Get",
                                ContentType = "application/json; charset=utf-8",
                            }
                        );
                        break;
                    default:
                        apiResponseData = PlanAccess.Instance.GetPlanRecordProgress(EquipmentAutoID, Year);
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 删除计划
        /// </summary>
        /// <param name="Year"></param>
        /// <param name="EquipmentAutoID"></param>
        /// <returns></returns>
        public APIResponseData DeleteByYearAndEquipmentPk(int Year, int EquipmentAutoID)
        {
            APIResponseData apiResponseData = null;
            try
            {
                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        apiResponseData = ApiHelper.Instance.SendMessage(
                            new HttpItem
                            {
                                URL = $"{ServiceRouteConstValue.DeleteByYearAndEquipmentPk}?Year={Year}&EquipmentAutoID={EquipmentAutoID}",
                                Method = "Post",
                                ContentType = "application/json; charset=utf-8",
                                Postdata = JsonConvert.SerializeObject(new { Year, EquipmentAutoID })
                            }
                        );
                        break;
                    default:
                        apiResponseData = PlanAccess.Instance.DeleteByYearAndEquipmentPk(Year, EquipmentAutoID);
                        break;
                }

                return apiResponseData;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取设备在年度的全部保养信息
        /// </summary>
        /// <param name="Year"></param>
        /// <param name="EquipmentAutoID"></param>
        /// <returns></returns>
        public APIResponseData GetDeviceInformationPlans(int Year, int EquipmentAutoID)
        {
            APIResponseData apiResponseData = null;
            try
            {
                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        apiResponseData = ApiHelper.Instance.SendMessage(
                            new HttpItem
                            {
                                URL = $"{ServiceRouteConstValue.GetDeviceInformationPlans}?Year={Year}&EquipmentAutoID={EquipmentAutoID}",
                                Method = "Get",
                                ContentType = "application/json; charset=utf-8",
                                Postdata = JsonConvert.SerializeObject(new { Year, EquipmentAutoID })
                            }
                        );
                        break;
                    default:
                        apiResponseData = PlanAccess.Instance.GetDeviceInformationPlans(EquipmentAutoID, Year);
                        break;
                }

                return apiResponseData;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
