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

namespace AnHuiMI.Common
{
	class StringUtils
	{
		// Token: 0x06000098 RID: 152 RVA: 0x00008CE8 File Offset: 0x00006EE8
		public static long CurrentTimeStamp(bool isMinseconds = false)
		{
			TimeSpan timeSpan = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
			return Convert.ToInt64(isMinseconds ? timeSpan.TotalMilliseconds : timeSpan.TotalSeconds);
		}

		// Token: 0x06000099 RID: 153 RVA: 0x00008D28 File Offset: 0x00006F28
		public static string SortJson(string json)
		{
			var dic = JsonConvert.DeserializeObject<SortedDictionary<string, object>>(json);
			SortedDictionary<string, object> keyValues = new SortedDictionary<string, object>(dic);
			keyValues.OrderBy(m => m.Key);//升序 把Key换成Value 就是对Value进行排序
										  //keyValues.OrderByDescending(m => m.Key);//降序
			SortedDictionary<string, object> tempKeyValues = new SortedDictionary<string, object>(keyValues);
			foreach (KeyValuePair<string, object> kv in tempKeyValues)
			{
				// 判断value是不是JObject类型
				Type t0 = typeof(JObject);
				Type t1 = kv.Value.GetType();
				if (t0 == t1)
				{
					// value是JObject类型
					string jsonItem = JsonConvert.SerializeObject(kv.Value);
					jsonItem = SortJson(jsonItem);
					keyValues[kv.Key] = JsonConvert.DeserializeObject<JObject>(jsonItem);
				}
			}
			return JsonConvert.SerializeObject(keyValues);
		}

		// Token: 0x0600009B RID: 155 RVA: 0x00009080 File Offset: 0x00007280
		public static string Json2sign(string json)
		{
			Dictionary<string, object> dictionary = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
			string text = "";
			foreach (KeyValuePair<string, object> keyValuePair in dictionary)
			{
				text = string.Concat(new object[]
				{
					text,
					keyValuePair.Key,
					"=",
					keyValuePair.Value,
					"&"
				});
			}
			return text.Substring(0, text.Length - 1);
		}
	}
}
