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

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

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public APIResponseData GetDatas(TagRecordFilter filter)
        {
            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.Tag_Get_Datas,
                            Method = "Post",
                            ContentType = "application/json; charset=utf-8",
                            Postdata = JsonConvert.SerializeObject(filter)
                        });

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

                        apiResponseData = TagAccess.Instance.GetDatas(filter, GlobalInfo.CurrentUser.LoginCode);

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

        public APIResponseData Edit(TagRecordInfo tag)
        {
            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.Tag_Creat,
                            Method = "Post",
                            ContentType = "application/json; charset=utf-8",
                            Postdata = HttpHelper.ObjectToJson(tag)
                        });

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

                        apiResponseData = TagAccess.Instance.Creat(tag, GlobalInfo.OperationInfo);

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

        /// <summary>
        /// 接受/维修
        /// </summary>
        /// <param name="tag"></param>
        /// <returns></returns>
        public APIResponseData Receipt(TagRecordInfo tag, int userID, string userName, string userCode)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "接受/维修失败！" };
            try
            {
                switch (DeviceRepair.Utils.Config.Configurations.Properties.ConnType?.ToLower())
                {
                    case "api":
                        #region api
                        HttpItem item = new HttpItem
                        {
                            URL = ServiceRouteConstValue.Tag_Receipt,
                            Method = "Post",
                            ContentType = "application/json; charset=utf-8",
                            Postdata = JsonConvert.SerializeObject(tag)
                        };

                        HttpHelper http = new HttpHelper();

                        item.Header.Add("auth", GlobalInfo.token);
                        item.Header.Add("Operator", userID + "");
                        item.Header.Add("OperatorCode", HttpHelper.URLEncode(userCode, System.Text.Encoding.UTF8));
                        item.Header.Add("OperatorName", HttpHelper.URLEncode(userName, System.Text.Encoding.UTF8));
                        item.Header.Add("ClientMac", DeviceRepair.Utils.ComputerHelper.GetMacAddress);
                        item.Header.Add("IPAddress", DeviceRepair.Utils.ComputerHelper.GetIPAddress);
                        item.Header.Add("ClientName", DeviceRepair.Utils.ComputerHelper.GetComputerName);

                        item.Timeout = 30000;
                        item.PostEncoding = System.Text.Encoding.UTF8;

                        string ul = item.URL.TrimStart('/');

                        // 使用 UriKind.Absolute 表示 baseUrl 是绝对路径
                        var baseUri = new Uri(ApiHelper.ServiceUrl, UriKind.Absolute);

                        var relativeUri = new Uri(baseUri, ul);
                        item.URL = relativeUri.ToString();


                        //请求的返回值对象
                        HttpResult result = http.GetHtml(item);

                        if (result.StatusCode == 0)
                            return new APIResponseData { Code = -1, Message = result.Html };
                        else if (result.StatusCode != System.Net.HttpStatusCode.OK)
                            throw new Exception(result.Html);

                        //获取返回值
                        apiResponseData = JsonConvert.DeserializeObject<APIResponseData>(result.Html);

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

                        apiResponseData = TagAccess.Instance.Receipt(tag, GlobalInfo.OperationInfo);

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