using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Com.FirmLib.Common
{
    /// <summary>
    /// 提交信息结果
    /// </summary>
    public class PostResult
    {
        private static readonly Dictionary<string, List<string>> m_KeysCollection = new Dictionary<string, List<string>>();
        static PostResult()
        {
            m_KeysCollection.Add( "result", new List<string> { "result", "Result" });
            m_KeysCollection.Add("resultMsg", new List<string> { "resultMsg", "ResultMsg" });
        }
        public PostResult()
        {
        }
        public PostResult(JObject jObject)
        {
            
           
            foreach (var keyCollection in m_KeysCollection)
            {
                foreach (string key in keyCollection.Value)
                {
                    JToken token = null;
                    if (jObject.TryGetValue(key,out token))
                    {
                        SetValue(keyCollection.Key, token.ToString());
                        continue;
                    }
                }
            }
            //if (jObject.ContainsKey("result"))
            //{
            //    Result = jObject["result"].ToString();
            //}
            //if (jObject.ContainsKey("resultMsg"))
            //{
            //    ResultMessage = jObject["resultMsg"].ToString();
            //}
            //if (Result==null&&jObject.ContainsKey("Result"))
            //{
            //    Result = jObject["Result"].ToString();
            //}
            //if (ResultMessage == null&&jObject.ContainsKey("ResultMsg"))
            //{
            //    ResultMessage = jObject["ResultMsg"].ToString();
            //}

        }
        /// <summary>
        /// 提交结果
        /// </summary>
        [JsonProperty("result")]
        public string Result { get; set; }
        /// <summary>
        /// 提交结果信息
        /// </summary>
        [JsonProperty("resultMsg")]
        public string ResultMessage { get; set; }

        /// <summary>
        /// 是否成功
        /// </summary>
        public bool IsSuccess
        {
            get { return Result == "success"; }
        }

        private void SetValue(string propertyName,object value)
        {
            if (propertyName.ToUpper() == "Result".ToUpper())
            {
                this.Result = value.ToString();
            }
            else
            {
                this.ResultMessage = value.ToString();
            }
        }
        public static PostResult CreatePostResult(string str)
        {
            return JsonConvert.DeserializeObject<PostResult>(str);
        }
        public static PostResult CreatePostResult(JObject jObject)
        {
            return new PostResult(jObject);
        }
    }
}
