using System;
using System.Net.Http;
using System.Collections;
using System.Collections.Generic;
using Newtonsoft.Json;
using Common.Request;
using System.Web;

namespace Utils
{
    class HttpUtils
    {
        public static string doPostForm(string url, Dictionary<string, string> param)
        {
            try
            {
                using (var client = new HttpClient())
                {
                    using (var multipartFormDataContent = new FormUrlEncodedContent(param))
                    {
                        Console.WriteLine(JsonConvert.SerializeObject(param));
                        var result = client.PostAsync(url, multipartFormDataContent).Result.Content.ReadAsStringAsync().Result;
                        Console.WriteLine(result);
                        return result;
                    }
                }
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        public static string doGet(string url)
        {
            try
            {
                using (var client = new HttpClient())
                {
                    Console.WriteLine(JsonConvert.SerializeObject(url));
                    var result = client.GetAsync(url).Result.Content.ReadAsStringAsync().Result;
                    Console.WriteLine(result);
                    return result;

                }
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

         public static string doPostMultipartFormData<T>(string url, String filePath,String filename)
        {
            try
            {
                using (var client = new HttpClient())
                {
                    using (var multipartFormDataContent = new MultipartFormDataContent())
                    {
                        multipartFormDataContent.Add(new ByteArrayContent(System.IO.File.ReadAllBytes(filePath)), "file", filename);
                        var result = client.PostAsync(url, multipartFormDataContent).Result.Content.ReadAsStringAsync().Result;
                        Console.WriteLine(result);
                        return result;
                    }
                }
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        public static string buildUrl<T>(string url, BaseReq<T> baseReq)
        {
            return string.Format(url, baseReq.method, baseReq.t, baseReq.key, baseReq.sign, HttpUtility.UrlEncode(baseReq.param.ToString()));
        }

        #region 电子面单打印接口
        /// <summary>
        /// 调用电子面单打印接口
        /// </summary>
        /// <param name="url"></param>
        /// <param name="param">body的参数</param>
        /// <param name="repStr">接口返回信息</param>
        /// <param name="errorStr">调用接口异常的错误信息</param>
        /// <returns></returns>
        public static Boolean doPostForm(string url, Dictionary<string, string> param, out string repStr,out string errorStr)
        {
            errorStr = "";
            repStr = "";
            try
            {
                using (var client = new HttpClient())
                {
                    using (var multipartFormDataContent = new FormUrlEncodedContent(param))
                    {
                         repStr = client.PostAsync(url, multipartFormDataContent).Result.Content.ReadAsStringAsync().Result;
                    }
                }
            }
            catch (Exception e)
            {
                errorStr = "调用接口失败:" + e.ToString();
            }

            if ((errorStr==string.Empty)&&(repStr != string.Empty))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion

    }
}
