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

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

        /// <summary>
        /// 通过设备编号查询设备信息
        /// </summary>
        /// <param name="EquipmentID"></param>
        /// <returns></returns>
        public APIResponseData GetModelByEquipmentID(string EquipmentID)
        {
            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.GetDeviceByEquipmentID + "?EquipmentID=" + EncryptionHelper.UrlEncry(EquipmentID),
                            Method = "Get",
                            ContentType = "application/json; charset=utf-8"
                        });
                        break;
                    case "sql":
                        apiResponseData = DeviceAccess.Instance.GetModelByEquipmentID(EquipmentID);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 设备台账数据获取
        /// </summary>
        /// <param name="FilterValue"></param>
        /// <returns></returns>
        public APIResponseData GetQuery(string FilterValue)
        {
            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.GetDeviceQuery}?FilterString={FilterValue}",
                            Method = "Get",
                            ContentType = "application/json;charset=utf-8",
                        });
                        break;

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

        /// <summary>
        /// 获取树形结构
        /// </summary>
        /// <param name="FilterValue"></param>
        /// <returns></returns>
        public APIResponseData GetDeviceTreeDatas(string FilterValue)
        {
            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.GetDeviceTreeDatas}?FilterString={FilterValue}",
                            Method = "Get",
                            ContentType = "application/json;charset=utf-8",
                        });
                        break;

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

        /// <summary>
        /// 修改设备信息
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public APIResponseData UpdateDriveInformation(Dictionary<string, object> data)
        {
            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.UpdateDeviceInformation}",
                            Method = "Post",
                            ContentType = "application/json;charset=utf-8",
                            Postdata = JsonConvert.SerializeObject(data)
                        });
                        break;
                    default:
                        apiResponseData = DeviceAccess.Instance.UpdateDriveInformation(data, GlobalInfo.OperationInfo);
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }


        /// <通过设备表主键编号查询数据>
        /// 查询数据
        /// </summary>
        /// <param name="FilterValue"></param>
        /// <returns></returns>
        public APIResponseData GetDataByAutoID(int AutoID)
        {
            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.GetDeviceDataByAutoID}?pk={AutoID}",
                            Method = "Get",
                            ContentType = "application/json; charset=utf-8",
                        });

                        #endregion
                        break;
                    default:
                        apiResponseData = DeviceAccess.Instance.GetDataByAutoID(AutoID);
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }
    }
}
