using IRaCIS.Core.Domain.Share;
using Newtonsoft.Json;
using System;
using System.Globalization;

namespace IRaCIS.Core.API
{


    //public class CustomContractResolver : DefaultContractResolver
    //{
    //    protected override JsonContract CreateContract(Type objectType)
    //    {
    //        var contract = base.CreateContract(objectType);

    //        // 检查类是否有 LowercaseJsonAttribute 标记
    //        if (objectType.GetCustomAttribute<LowerCamelCaseJsonAttribute>() != null)
    //        {
    //            contract.NamingStrategy = new IRCCamelCaseNamingStrategy();
    //        }

    //        return contract;
    //    }
    //}



    #region 废弃

    public class MyDateTimeConverter : JsonConverter<DateTime>
    {
        public override DateTime ReadJson(JsonReader reader, Type objectType, DateTime existingValue, bool hasExistingValue, JsonSerializer serializer)
        {
            return reader.ReadAsDateTime().Value;
        }

        public override void WriteJson(JsonWriter writer, DateTime value, JsonSerializer serializer)
        {
            var isEn_US = CultureInfo.CurrentCulture.Name == StaticData.CultureInfo.en_US;

            string dateFormat;
            if (!isEn_US)
            {
                // Chinese date format
                dateFormat = "yyyy-MM-dd HH:mm:ss";
            }
            else
            {
                // Default or English date format
                dateFormat = "MM/dd/yyyy HH:mm:ss";
            }

            writer.WriteValue(value.ToString(dateFormat));

        }
    }

    public class MyNullableDateTimeConverter : JsonConverter<DateTime?>
    {
        public override DateTime? ReadJson(JsonReader reader, Type objectType, DateTime? existingValue, bool hasExistingValue, JsonSerializer serializer)
        {
            var val = reader.ReadAsDateTime();
            return val;
        }

        public override void WriteJson(JsonWriter writer, DateTime? value, JsonSerializer serializer)
        {
            var isEn_US = CultureInfo.CurrentCulture.Name == StaticData.CultureInfo.en_US;

            string dateFormat;
            if (!isEn_US)
            {
                // Chinese date format
                dateFormat = "yyyy-MM-dd HH:mm:ss";
            }
            else
            {
                // Default or English date format
                dateFormat = "MM/dd/yyyy HH:mm:ss";
            }

            if (value.HasValue)
            {
                writer.WriteValue(value.Value.ToString(dateFormat));

            }
            else
            {
                writer.WriteValue(default(DateTime?));
            }

        }
    }

    #endregion
}
