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

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

        /// <summary>
        /// 获取全部配置数据
        /// </summary>
        /// <returns></returns>
        public APIResponseData GetAllConfigs()
        {
            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.GetConfigs,
                            Method = "Get",
                            ContentType = "application/json; charset=utf-8"
                        });

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

                        apiResponseData = SystemUtil.Instance.GetAll();

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

        /// <summary>
        /// 编辑配置信息
        /// </summary>
        /// <param name="Datas"></param>
        /// <returns></returns>
        public APIResponseData EditConfigs(List<SysConfigInfo> Datas)
        {
            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.EditConfigs,
                            Method = "Post",
                            ContentType = "application/json; charset=utf-8",
                            Postdata = JsonConvert.SerializeObject(Datas)
                        });

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

                        apiResponseData = SystemUtil.Instance.EditConfigs(Datas);

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

        /// <summary>
        /// 获取附件信息
        /// </summary>
        /// <returns></returns>
        public APIResponseData GetAttachments(string TableName, string PrimaryKey, string PrimaryValue)
        {
            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.GetAttachments}?TableName={TableName}&PrimaryKey={PrimaryKey}&PrimaryValue={PrimaryValue}",
                            Method = "Get",
                            ContentType = "application/json; charset=utf-8"
                        });
                        break;
                    default:
                        apiResponseData = SystemUtil.Instance.GetAttachment(TableName, PrimaryKey, PrimaryValue);
                        break;
                }
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 获取附件图片
        /// </summary>
        /// <param name="AutoId"></param>
        /// <returns></returns>
        public Image GetAttaImg(int AutoId)
        {
            try
            {
                HttpHelper http = new HttpHelper();
                HttpItem item = new HttpItem
                {
                    URL = $"{ServiceRouteConstValue.GetAttaImg}?AttachmentId={AutoId}",
                    Method = "Get",
                    ContentType = "application/json; charset=utf-8",
                    ResultType = CsharpHttpHelper.Enum.ResultType.Byte
                };

                ApiHelper.Instance.Init(ref item);
                HttpResult result = http.GetHtml(item);

                using (System.IO.MemoryStream ms = new System.IO.MemoryStream(result.ResultByte))
                {
                    return Bitmap.FromStream(ms, true);
                }
            }
            catch { }
            return null;
        }
    }
}
