using CsharpHttpHelper;
using DeviceRepair.DataAccess;
using DeviceRepair.Models;
using DeviceRepair.Utils.Security;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;

namespace DeviceRepairAndOptimization.Biz.AM
{
    public class PlanManager
    {
        private static PlanManager manager;
        public static PlanManager Instance
        {
            get
            {
                if (manager == null)
                    manager = new PlanManager();
                return manager;
            }
        }

        /// <summary>
        /// 获取所有年计划
        /// </summary>
        /// <returns></returns>
        public APIResponseData GetDatas(string FilterText, int Year = 0)
        {
            APIResponseData apiResponseData = null;
            try
            {
                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = $"{ServiceRouteConstValue.GetAmAnnualEquipmentPlans}?Year={Year}&q={EncryptionHelper.UrlEncry(FilterText)}",
                            Method = "Get",
                            ContentType = "application/json; charset=utf-8"
                        });
                        break;
                    default:
                        apiResponseData = PlanAccess.Instance.GetAMPlans(FilterText, GlobalInfo.CurrentUser.LoginCode, Year);
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData = new APIResponseData { Code = -1, Data = ex.Message };
            }
            return apiResponseData;
        }

        public APIResponseData GetSinge(int AutoID)
        {
            APIResponseData apiResponseData = null;
            try
            {
                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = $"{ServiceRouteConstValue.Plan_Get_Singe}?AutoID={AutoID}",
                            Method = "Get",
                            ContentType = "application/json; charset=utf-8"
                        });
                        break;
                    default:
                        apiResponseData = PlanAccess.Instance.GetSinge(AutoID);
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData = new APIResponseData { Code = -1, Data = ex.Message };
            }
            return apiResponseData;
        }


        /// <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>
        /// 导出EXCEL的数据
        /// </summary>
        /// <param name="Year"></param>
        /// <returns></returns>
        public APIResponseData ExportXlsxDatas(int Year, string RootName = "OEM")
        {
            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 + "&RootName=" + RootName,
                            Method = "Get",
                            ContentType = "application/json; charset=utf-8"
                        });
                        break;
                    default:
                        apiResponseData = PlanAccess.Instance.ExportXlsxDatas(Year, RootName);
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData = new APIResponseData { Code = -1, Data = ex.Message };
            }
            return apiResponseData;
        }

        /// <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>
        /// <param name="PlanAutoID"></param>
        /// <returns></returns>
        public APIResponseData GetDailyPlanCompleteStatus(int PlanAutoID)
        {
            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.GetDailyPlanCompleteStatus}?PlanAutoID={PlanAutoID}",
                            Method = "Get",
                            ContentType = "application/json;charset=utf-8"
                        });
                        break;
                    default:
                        apiResponseData = PlanAccess.Instance.GetDailyPlanCompleteStatus(PlanAutoID);
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }
    }
}
