using CsharpHttpHelper;
using DeviceRepairAndOptimization.Data;
using DeviceRepairAndOptimization.Models;
using DeviceRepairAndOptimization.Utils;
using Newtonsoft.Json;
using System;

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

        /// <summary>
        /// 获取单条设备信息
        /// </summary>
        /// <param name="AutoID"></param>
        /// <returns></returns>
        public APIResponseData DeviceQuerySingle(int AutoID)
        {
            APIResponseData result = new APIResponseData { Code = -1, Message = "未能获取到数据！" };
            try
            {
                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        result = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = ServiceRoute.DeviceQuerySingle,
                            Method = "Post",
                            ContentType = "application/json;charset=utf-8",
                            Postdata = JsonConvert.SerializeObject(new { autoid = AutoID })
                        });
                        break;

                    default:
                        result = DeviceAccess.Instance.DeviceQuerySingle(AutoID);
                        break;
                }
                return result;
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 设备台账数据获取
        /// </summary>
        /// <param name="FilterValue"></param>
        /// <returns></returns>
        public APIResponseData GetQuery(string FilterValue)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "查询失败！" };

            try
            {
                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        apiResponseData = ApiHelper.Instance.SendMessage(new HttpItem
                        {
                            URL = ServiceRoute.QueryDeviceData,
                            Method = "Post",
                            ContentType = "application/json;charset=utf-8",
                            Postdata = JsonConvert.SerializeObject(new { querystring = FilterValue })
                        });
                        break;

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


        /// <summary>
        /// 获取设备信息树形结构 -- 按名称分类
        /// </summary>
        /// <returns></returns>
        public APIResponseData GetDeviceInformationTreeView()
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1 };
            try
            {
                switch (Models.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":

                        apiResponseData = ApiHelper.Instance.SendMessage(new CsharpHttpHelper.HttpItem
                        {
                            URL = ServiceRoute.GetDeviceInformationTreeView,
                            Method = "post",
                            ContentType = "application/json; charset=utf-8",
                        });

                        break;
                    case "sql":

                        apiResponseData = DeviceAccess.Instance.GetDeviceInformationTreeView();

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