using Common.Request;
using Common.Request.Label;
using Utils;
using Common;
using Newtonsoft.Json.Linq;
using System.IO;
using System;
using System.Text;
using System.Collections.Generic;
/// <summary>
/// 商家寄件（官方快递）
/// </summary>
public static class LabelV2
{

    /// <summary>
    /// 下单
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
     public static string order(BaseReq<OrderParam> param){
        
        param.method = ApiInfoConstant.ORDER;
        var request = ObjectToDictionaryUtils.ObjectToMap(param);
        
        if(request == null){
            return null;
        }
        var result = HttpUtils.doPostForm(ApiInfoConstant.NEW_TEMPLATE_URL,request);
        return result;
    }

    /// <summary>
    /// 复打
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
     public static string repeatPrint(BaseReq<RepeatPrintParam> param){
        
        param.method = ApiInfoConstant.CLOUD_PRINT_OLD_METHOD;
        var request = ObjectToDictionaryUtils.ObjectToMap(param);
        
        if(request == null){
            return null;
        }
        var result = HttpUtils.doPostForm(ApiInfoConstant.NEW_TEMPLATE_URL,request);
        return result;
    }

    /// <summary>
    /// 自定义打印
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
     public static string customPrint(BaseReq<CustomPrintParam> param){
        
        param.method = ApiInfoConstant.CUSTOM;
        var request = ObjectToDictionaryUtils.ObjectToMap(param);
        
        if(request == null){
            return null;
        }
        var result = HttpUtils.doPostForm(ApiInfoConstant.NEW_TEMPLATE_URL,request);
        return result;
    }

    public static string callOrder(BaseReq<OrderParam> param)
    {
        int code = 0;
        string msg = "";
        string data = "";
        string errorStr = "";
        Dictionary<string, string> logReq = null;
        try
        {
            param.method = ApiInfoConstant.ORDER;
            var request = ObjectToDictionaryUtils.ObjectToMap(param);

            logReq = request;

            if (request == null)
            {
                msg = "body参数错误";
            }
            if (HttpUtils.doPostForm(ApiInfoConstant.NEW_TEMPLATE_URL, request, out data, out errorStr))
            {
                code = 1;
                msg = "操作成功";
            }
            else
            {
                msg = errorStr;
            }
        }
        catch (System.Exception ex)
        {
            msg = "操作失败:" + ex.ToString();
        }

        try
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("----" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            sb.AppendLine("POST "+ ApiInfoConstant.NEW_TEMPLATE_URL);
            sb.AppendLine("Request Body");
            sb.AppendLine(Newtonsoft.Json.JsonConvert.SerializeObject(logReq));
            sb.AppendLine("Response Body");
            sb.AppendLine(data);
            sb.AppendLine("");
            WriteTxt(sb.ToString());
        }
        catch (Exception)
        {

        }

        JObject result = new JObject();
        result.Add("code", code);
        result.Add("msg", msg);
        result.Add("data", Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(data));

        return result.ToString();
    }

    #region 将字符串写入txt文档
    private static void WriteTxt(string src)
    {
        string rFilePath = System.Environment.CurrentDirectory + "\\log" + DateTime.Today.ToString("yyyyMMdd");
        FileStream fs = new FileStream(rFilePath, FileMode.Append);
        byte[] data = Encoding.Default.GetBytes(src);
        try
        {
            fs.Write(data, 0, data.Length);
            fs.Flush();
            fs.Close();
        }
        catch (Exception)
        {
        }
    }
    #endregion
}