using IRaCIS.Core.Domain.Share;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using StackExchange.Redis;
using System;
using System.Globalization;

namespace IRaCIS.Core.API
{
    /// <summary>
    /// 序列化，反序列化的时候，处理时间 时区转换
    /// </summary>
    public class JSONTimeZoneConverter : DateTimeConverterBase
    {
        private readonly IHttpContextAccessor _httpContextAccessor;

        private readonly TimeZoneInfo _clientTimeZone;
        public JSONTimeZoneConverter(IHttpContextAccessor httpContextAccessor)
        {
            _httpContextAccessor = httpContextAccessor;

            //默认是UTC
            //var timeZoneId = "Etc/UTC";
            var timeZoneId = "Asia/Shanghai";

            var timeZoneIdHeader = _httpContextAccessor?.HttpContext?.Request?.Headers["TimeZoneId"];

            if (timeZoneIdHeader is not null && !string.IsNullOrEmpty(timeZoneIdHeader.Value))
            {
                timeZoneId = timeZoneIdHeader.Value;
            }

            _clientTimeZone = TimeZoneInfo.FindSystemTimeZoneById(timeZoneId);

        }

        public override bool CanConvert(Type objectType)
        {
            // 仅支持 DateTime 类型的转换
            return objectType == typeof(DateTime)|| objectType == typeof(DateTime?);
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader.Value == null)
            {
                return null;
            }

            DateTime dateTime;

            if (reader.ValueType == typeof(DateTime) || reader.ValueType == typeof(DateTime?))
            {
                DateTime? nullableDateTime = reader.Value as DateTime?;


                if (nullableDateTime != null && nullableDateTime.HasValue)
                {
                    dateTime = nullableDateTime.Value;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                if (DateTime.TryParse((string)reader.Value, out dateTime) == false)
                {
                    return null;
                }
            }
            
          

            // 将客户端时间转换为服务器时区的时间
            var serverZoneTime = TimeZoneInfo.ConvertTime(dateTime, _clientTimeZone, TimeZoneInfo.Local);

            return serverZoneTime;

           
                      
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {                  
            DateTime? nullableDateTime = value as DateTime?;

            if (nullableDateTime != null && nullableDateTime.HasValue)
            {
                //第一个参数默认使用系统本地时区 也就是应用服务器的时区
                DateTime clientZoneTime = TimeZoneInfo.ConvertTime(nullableDateTime.Value, _clientTimeZone);
                writer.WriteValue(clientZoneTime);
            }
            else
            {
                writer.WriteNull();
            }            
        }

    }
}
