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

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

        /// <summary>
        /// 根据编码查询字段信息
        /// </summary>
        /// <param name="Code"></param>
        /// <returns></returns>
        public APIResponseData GetQuery(string Code)
        {
            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.Field_Query + "?Code=" + EncryptionHelper.UrlEncry(Code),
                            Method = "Get",
                            ContentType = "application/json; charset=utf-8"
                        });
                        break;
                    case "sql":
                        apiResponseData = FieldAccess.Instance.GetFieldsByCodes(Code);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 检验数据是否存在
        /// </summary>
        /// <param name="Code"></param>
        /// <returns></returns>
        public APIResponseData Exists(string Code, string Text)
        {
            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.Field_Exists}?Code={EncryptionHelper.UrlEncry(Code)}&Text={EncryptionHelper.UrlEncry(Text)}",
                            Method = "Get",
                            ContentType = "application/json; charset=utf-8"
                        });
                        break;
                    case "sql":
                        apiResponseData = FieldAccess.Instance.TextIsExists(Code, Text);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 数据新增或者编辑
        /// </summary>
        /// <param name="Code"></param>
        /// <returns></returns>
        public APIResponseData InsertOrEdit(FieldsInfo entity)
        {
            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.Field_InsertOrEdit}",
                            Method = "Post",
                            ContentType = "application/json; charset=utf-8",
                            Postdata = JsonConvert.SerializeObject(entity)
                        });
                        break;
                    case "sql":
                        apiResponseData = FieldAccess.Instance.InsertOrEdit(entity, GlobalInfo.OperationInfo);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="AutoID"></param>
        /// <param name="Status"></param>
        /// <returns></returns>
        public APIResponseData ChangeStatus(int AutoID, bool Status, string Description)
        {
            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.Field_ChangeStatus}?AutoID={AutoID}&Status={Status}&Description={EncryptionHelper.UrlEncry(Description)}",
                            Method = "Post",
                            ContentType = "application/json; charset=utf-8"
                        });
                        break;
                    case "sql":
                        apiResponseData = FieldAccess.Instance.ChangeStatus(AutoID, Status, Description, GlobalInfo.OperationInfo);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }
    }
}
