using System;
using System.IO;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using ChuPiao.Common.Utils;
using ChuPiao.Common.Core.Security;

namespace ChuPiao.Common.Communication.GtdJson
{
    /// <summary>
    /// 消息包基类
    /// </summary>
    public abstract class PackBase
    {

        /// <summary>
        /// 报文命令
        /// </summary>
        public abstract string Action { get; }
        /// <summary>
        /// 包体打包
        /// </summary>
        /// <returns></returns>
        public abstract void PackBody(JsonWriter jsonWriter);
        /// <summary>
        /// 打包
        /// </summary>
        /// <returns></returns>
        public string Pack()
        {
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);
            //string jsonText;
            using (JsonWriter jsonWriter = new JsonTextWriter(sw))
            {
                //jsonWriter.Formatting = Formatting.Indented;
                jsonWriter.WriteStartObject();

                jsonWriter.WritePropertyName("body");
                jsonWriter.WriteStartObject();
                PackBody(jsonWriter);
                jsonWriter.WriteEndObject();//end body

                jsonWriter.Flush();
                _pack = sw.GetStringBuilder().ToString();
                int index_start = _pack.IndexOf("\"body\":");
                int index_end = _pack.LastIndexOf("}");
                string body = _pack.Substring(index_start + 7, index_end - index_start - 7 + 1);   //{"lotId":911}
                Logger.Log.Debug(string.Format("{0} 发送-->{1}", AgentId, body));
                //Logger.Log.Info(string.Format("{0}{1}{2}{3}{4}", MessageId, AgentId, Timestamp, Salt, body));

                jsonWriter.WritePropertyName("head");
                jsonWriter.WriteStartObject();
                jsonWriter.WritePropertyName("agentId");
                jsonWriter.WriteValue(AgentId);
                jsonWriter.WritePropertyName("msgId");
                jsonWriter.WriteValue(MessageId);
                jsonWriter.WritePropertyName("timestamp");
                jsonWriter.WriteValue(Timestamp);
                jsonWriter.WritePropertyName("sign");
                jsonWriter.WriteValue(MD5Util.Encrypt(string.Format("{0}{1}{2}{3}{4}", MessageId, AgentId, Timestamp, Salt, body), Encoding.GetEncoding("utf-8")));
                jsonWriter.WriteEndObject();//end head

                jsonWriter.WriteEndObject();//end

                jsonWriter.Flush();
                _pack = sw.GetStringBuilder().ToString();
            }
            return _pack;
        }

        public override string ToString()
        {
            return _pack;
        }

        #region 属性
        /// <summary>
        /// 包体字符串
        /// </summary>
        private string _pack;

        /// <summary>
        /// 站点编号
        /// </summary>
        public string AgentId
        {
            get { return _agentId; }
            set { _agentId = value; }
        }
       
        ///<summary>
        ///密钥
        ///</summary>
        public string Salt
        {
            get { return _salt; }
            set { _salt = value; }
        }
        /// <summary>
        /// 消息ID
        /// </summary>
        public string MessageId
        {
            get { return _messageId; }
            set { _messageId = value; }
        }
        /// <summary>
        /// 时间戳
        /// </summary>
        public string Timestamp
        {
            get { return _timestamp; }
            set { _timestamp = value; }
        }

        private string _salt = "";
        private string _messageId="";
        private string _timestamp = "";
        private string _agentId = "";
        #endregion
    }
}
