using Newtonsoft.Json;
using System;
using System.Net.Http;
using System.Text;
using TsSFCDeivceClient.Common;
using TsSFCDeivceClient.Model;

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

        public APIResponseData Get(string url)
        {
            try
            {
                using (HttpClient client = new HttpClient())
                {
                    SetClientToken(client);
                    StringContent content = new StringContent("", Encoding.UTF8, "application/json");

                    HttpResponseMessage response = client.GetAsync(url).Result;
                    if (response.IsSuccessStatusCode)
                    {
                        string stringRtn = response.Content.ReadAsStringAsync().Result;
                        APIResponseData apiResponse = JsonConvert.DeserializeObject<APIResponseData>(stringRtn);
                        return apiResponse;
                    }
                    else
                    {
                        throw new Exception(response.StatusCode.ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                return new APIResponseData { Code = -1, Message = ex.Message };
            }
        }

        public APIResponseData Post(string url, string postData)
        {
            try
            {
                using (HttpClient client = new HttpClient())
                {
                    SetClientToken(client);
                    client.DefaultRequestHeaders.Add("Operator", Runtime.CurrentUser.Id + "");
                    client.DefaultRequestHeaders.Add("OperatorCode", Runtime.CurrentUser.UserCode);
                    client.DefaultRequestHeaders.Add("OperatorGUID", Runtime.CurrentUser.GUID + "");
                    client.DefaultRequestHeaders.Add("OperatorName", System.Web.HttpUtility.UrlEncode(Runtime.CurrentUser.UserName));
                    client.DefaultRequestHeaders.Add("IPAddress", ComputerHelper.Instance().GetIPAddress());
                    client.DefaultRequestHeaders.Add("ClientMac", ComputerHelper.Instance().GetMacAddress());
                    client.DefaultRequestHeaders.Add("ClientName", ComputerHelper.Instance().GetComputerName());

                    StringContent content = new StringContent(postData, Encoding.UTF8, "application/json");

                    HttpResponseMessage response = client.PostAsync(url, content).Result;
                    if (response.IsSuccessStatusCode)
                    {
                        string stringRtn = response.Content.ReadAsStringAsync().Result;
                        APIResponseData apiResponse = JsonConvert.DeserializeObject<APIResponseData>(stringRtn);
                        return apiResponse;
                    }
                    else
                    {
                        throw new Exception(response.StatusCode.ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                return new APIResponseData { Code = -1, Message = ex.Message };
            }
        }

        void SetClientToken(HttpClient client)
        {
            object token = new
            {
                LoginCode = Runtime.CurrentUser.UserCode,
                Password = Runtime.CurrentUser.Password,
                inParams = Runtime.inParams
            };

            string jsonToken = JsonConvert.SerializeObject(token);
            string desToken = DESEncrypt.Encrypt(jsonToken, "^2020#!_@MaYonglong@_!#2021^");

            client.DefaultRequestHeaders.Add("sfc", desToken);
        }
    }
}
