using Newtonsoft.Json.Converters;
using Newtonsoft.Json;
using System;
using SharpCompress.Writers;
using IRaCIS.Core.Domain.Share;
using System.Globalization;

namespace IRaCIS.Core.API._ServiceExtensions.NewtonsoftJson
{


    public class ExportExcelDateConverter : DateTimeConverterBase
    {
        private readonly TimeZoneInfo _clientTimeZone;

        public ExportExcelDateConverter(TimeZoneInfo clientTimeZone)
        {
            _clientTimeZone = clientTimeZone;
        }
        public override bool CanConvert(Type objectType)
        {

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

            if (nullableDateTime != null && nullableDateTime.HasValue)
            {
                // 将服务器时间转换为客户端时间
                DateTime clientTime = TimeZoneInfo.ConvertTime(nullableDateTime.Value, TimeZoneInfo.Local, _clientTimeZone);
                writer.WriteValue(clientTime);
            }
            else
            {
                writer.WriteNull();
            }


        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            DateTime? nullableDateTime = reader.Value as DateTime?;

            if (nullableDateTime != null && nullableDateTime.HasValue)

            {    // 服务器时区的时间转为客户端时间
                var clientZoneTime = TimeZoneInfo.ConvertTime(nullableDateTime.Value, TimeZoneInfo.Local, _clientTimeZone);

                return clientZoneTime;
            }
            return reader.Value;


        }


    }
    public class ExportExcelConverterDate
    {
        public static T ConvertToClientTimeInObject<T>(T obj, string timeZoneId)
        {
            var clientTimeZone = TimeZoneInfo.FindSystemTimeZoneById(timeZoneId);
            if (obj == null)
                return obj;

            // 将对象序列化为 JSON 字符串
            string json = JsonConvert.SerializeObject(obj, new JsonSerializerSettings() { DateFormatString = "yyyy-MM-dd HH:mm:ss.fff" });

            // 将 JSON 字符串反序列化回对象
            var deserializedObj = JsonConvert.DeserializeObject<T>(json, new JsonSerializerSettings
            {
                Converters = { new ExportExcelDateConverter(clientTimeZone) },
                DateFormatString = "yyyy-MM-dd HH:mm:ss.fff",
                DateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind
            });

            return deserializedObj!;
        }

        public static string DateTimeInternationalToString(DateTime? dateTime, string? clientZoneId = null)
        {
            var isEn_US = CultureInfo.CurrentCulture.Name == StaticData.CultureInfo.en_US;

            if (dateTime.HasValue)
            {

                var needDealTime = dateTime.Value;

                if (needDealTime.Hour == 0 && needDealTime.Minute == 0 && needDealTime.Second == 0 && needDealTime.Millisecond==0)
                {
                    return needDealTime.ToString("yyyy-MM-dd");
                }
                else
                {
                    if (!string.IsNullOrWhiteSpace(clientZoneId))
                    {
                        needDealTime = TimeZoneInfo.ConvertTime(needDealTime, TimeZoneInfo.Local, TimeZoneInfo.FindSystemTimeZoneById(clientZoneId));
                    }

                    if (isEn_US)
                    {
                        //暂时保持一致，等需求确认，修改英文要展示的日期格式
                        return needDealTime.ToString("yyyy-MM-dd HH:mm:ss");
                    }
                    else
                    {
                        return needDealTime.ToString("yyyy-MM-dd HH:mm:ss");
                    }
                }


            }
            else
            {
                return string.Empty;
            }

        }
    }
}




