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

namespace AnHuiMI.Common
{
    public class Utils
    {

        /// <summary>
        /// 日期转换为时间戳Timestamp
        /// </summary>
        /// <param name="dateTime">要转换的日期</param>
        /// <returns></returns>
        public static long GetTimeStamp(DateTime dateTime)
        {
            DateTime _dtStart = new DateTime(1970, 1, 1, 8, 0, 0);
            //10位的时间戳
            long timeStamp = Convert.ToInt32(dateTime.Subtract(_dtStart).TotalSeconds);
            //13位的时间戳
            //long timeStamp = Convert.ToInt64(dateTime.Subtract(_dtStart).TotalMilliseconds);
            return timeStamp;
        }

        /// <summary>
        /// UTC时间戳Timestamp转换为北京时间
        /// </summary>
        /// <param name="timestamp">要转换的时间戳</param>
        /// <returns></returns>
        public static DateTime GetDateTime(long timestamp)
        {
            //DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); 
            //使用上面的方式会显示TimeZone已过时
            DateTime dtStart = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1), TimeZoneInfo.Local);
            DateTime targetDt = dtStart.AddMilliseconds(timestamp).AddHours(8);
            return targetDt;
        }

        public static void convertTimestamp(JObject joObject,string key)
        { 
            if (joObject.ContainsKey(key))
            {
                Global.writeLog(string.Format("当前对象[{0}]的类型为:{1}", key, joObject.GetValue(key).Type.GetType()));
            }
            
            if (joObject.ContainsKey(key) && joObject.GetValue(key).Type.GetType() != typeof(string))
            {
                long timestamp = long.Parse(joObject.GetValue(key).ToString());
                string datetime = Utils.GetDateTime(timestamp).ToString("yyyy-MM-dd");
                joObject[key] = datetime;
                Global.writeLog(string.Format("转换后的日期为:{0}",datetime));
            }

        }

        public static JObject Wrapper(JObject jsonInParam)
        {
            dynamic request = new JObject();
            request.arg0 = jsonInParam;
            return request;
        }
            public static JObject removeWrapper(JObject jsonInParam)
        {
            if (jsonInParam.ContainsKey("arg0"))
            {
                return (JObject)jsonInParam.GetValue("arg0");
            }
            return jsonInParam;
        }
    }
}
