using CsharpHttpHelper;
using DeviceRepair.DataAccess;
using DeviceRepair.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

        /// <summary>
        /// 获取当前建议书文件 -- 下载
        /// </summary>
        /// <param name="AutoID"></param>
        /// <returns></returns>
        public string GetFileSingle(int PrimaryKey)
        {
            string filePath = string.Empty;
            try
            {
                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        filePath = ApiHelper.Instance.DownLoadFile(new HttpItem()
                        {
                            URL = ServiceRouteConstValue.GetPreserveFile + "?pk=" + PrimaryKey,
                            Method = "Get",
                            ContentType = "application/vnd.ms-excel",
                        });
                        break;
                    default:
                        filePath = PreserveAccess.Instance.GetMaintenanceFormVersionSingle(PrimaryKey).FormPath;
                        if (!File.Exists(filePath))
                            filePath = string.Empty;
                        break;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return filePath;
        }

        public string GetPDFFile(int PrimaryKey)
        {
            string filePath = string.Empty;
            try
            {
                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        filePath = ApiHelper.Instance.DownLoadFile(new HttpItem()
                        {
                            URL = ServiceRouteConstValue.GetPDFFile + "?pk=" + PrimaryKey,
                            Method = "Get",
                            ContentType = "application/pdf",
                        });
                        break;
                    default:
                            filePath = string.Empty;
                        break;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return filePath;
        }

        /// <summary>
        /// 获取保养计划及对应的点检表信息
        /// </summary>
        /// <param name="PrimaryKey"></param>
        /// <returns></returns>
        public APIResponseData GetPlanAndFormInfo(int PrimaryKey)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "获取数据失败！" };
            try
            {
                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        #region api

                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = ServiceRouteConstValue.GetPlanAndFormInfo + "?pk=" + PrimaryKey,
                            Method = "Get",
                            ContentType = "application/json; charset=utf-8",
                        });

                        #endregion
                        break;
                    case "sql":
                        #region sql

                        apiResponseData = PreserveAccess.Instance.GetPlanAndFormInfo(PrimaryKey);

                        #endregion
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 根据主键编号获取对象
        /// </summary>
        /// <param name="PrimaryKey"></param>
        /// <returns></returns>
        public APIResponseData GetPreserveRecordItemByPrimaryKey(int PrimaryKey)
        {
            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.GetPreserveRecordItemByPrimaryKey + "?pk=" + PrimaryKey,
                            Method = "Get",
                            ContentType = "application/json;charset=utf-8",
                        });
                        break;

                    default:
                        apiResponseData = PreserveAccess.Instance.GetPreserveRecordItemByPrimaryKey(PrimaryKey);
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }

            return apiResponseData;
        }

        /// <summary>
        /// 获取单条设备保养记录信息
        /// </summary>
        /// <param name="EquipmentPk"></param>
        /// <param name="FormPk"></param>
        /// <returns></returns>
        public APIResponseData GetPreserveRecordItem(int EquipmentPk, int FormPk)
        {
            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.GetSingleByEquipmentPKAndFormPK + "?EquipmentID=" + EquipmentPk + "&FormID=" + FormPk,
                            Method = "Get",
                            ContentType = "application/json"
                        });
                        break;
                    default:
                        apiResponseData = PreserveAccess.Instance.GetSingleByEquipmentPrimaryIDAndFormPrimaryID(EquipmentPk, FormPk);
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Data = null;
                apiResponseData.Message = ex.Message;
            }

            return apiResponseData;
        }

        /// <summary>
        /// 获取所有的保养记录数据
        /// </summary>
        /// <returns></returns>
        public APIResponseData GetPreserveDatas(string filter = "")
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Data = null, Message = "获取数据失败" };
            try
            {
                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = $"{ServiceRouteConstValue.GetPreserveDatas}?filter={filter}",
                            Method = "Get",
                            ContentType = "application/json"
                        });

                        break;
                    default:
                        apiResponseData = PreserveAccess.Instance.GetPreserveDatas(filter);
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Data = null;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 获取保养数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public APIResponseData GetDetailById(int PrimaryKey)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Data = null, Message = "获取数据失败" };
            try
            {
                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = $"{ServiceRouteConstValue.GetPreserveDetailByID}?pk={PrimaryKey}",
                            Method = "Get",
                            ContentType = "application/json",
                        });
                        break;
                    default:
                        apiResponseData = PreserveAccess.Instance.GetPreserveDetailByID(PrimaryKey);
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Data = null;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 新增数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public APIResponseData Insertable(MaintenanceRecordSubmit entity)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Data = null, Message = "新增失败！" };
            if (entity == null)
                return new APIResponseData { Code = -1, Message = "数据获取失败，传入的数据为空！" };

            try
            {
                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = ServiceRouteConstValue.InsertablePreserve,
                            Method = "Post",
                            ContentType = "application/json",
                            Postdata = JsonConvert.SerializeObject(entity)
                        });
                        break;
                    default:
                        apiResponseData = PreserveAccess.Instance.Insertable(entity, GlobalInfo.OperationInfo);
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Data = null;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }
    }
}
