using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace HoneyBox.Admin.Business.Extensions;

/// <summary>
/// 灵活的日期时间 JSON 转换器
/// 支持多种日期格式：ISO 8601、"yyyy-MM-dd HH:mm:ss"、"yyyy-MM-dd HH:mm" 等
/// </summary>
public class FlexibleDateTimeConverter : JsonConverter<DateTime?>
{
    private static readonly string[] DateFormats = new[]
    {
        "yyyy-MM-dd HH:mm:ss",
        "yyyy-MM-dd HH:mm",
        "yyyy-MM-dd",
        "yyyy/MM/dd HH:mm:ss",
        "yyyy/MM/dd HH:mm",
        "yyyy/MM/dd",
        "MM/dd/yyyy HH:mm:ss",
        "MM/dd/yyyy",
    };

    public override DateTime? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        if (reader.TokenType == JsonTokenType.Null)
        {
            return null;
        }

        if (reader.TokenType == JsonTokenType.String)
        {
            var dateString = reader.GetString();
            
            if (string.IsNullOrWhiteSpace(dateString))
            {
                return null;
            }

            // 尝试标准 ISO 8601 格式
            if (DateTime.TryParse(dateString, CultureInfo.InvariantCulture, DateTimeStyles.None, out var result))
            {
                return result;
            }

            // 尝试自定义格式
            foreach (var format in DateFormats)
            {
                if (DateTime.TryParseExact(dateString, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out result))
                {
                    return result;
                }
            }

            throw new JsonException($"无法解析日期时间: {dateString}");
        }

        throw new JsonException($"意外的 JSON 令牌类型: {reader.TokenType}");
    }

    public override void Write(Utf8JsonWriter writer, DateTime? value, JsonSerializerOptions options)
    {
        if (value.HasValue)
        {
            writer.WriteStringValue(value.Value.ToString("yyyy-MM-dd HH:mm:ss"));
        }
        else
        {
            writer.WriteNullValue();
        }
    }
}

/// <summary>
/// 非空日期时间转换器
/// </summary>
public class FlexibleDateTimeNonNullableConverter : JsonConverter<DateTime>
{
    private static readonly string[] DateFormats = new[]
    {
        "yyyy-MM-dd HH:mm:ss",
        "yyyy-MM-dd HH:mm",
        "yyyy-MM-dd",
        "yyyy/MM/dd HH:mm:ss",
        "yyyy/MM/dd HH:mm",
        "yyyy/MM/dd",
    };

    public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        if (reader.TokenType == JsonTokenType.String)
        {
            var dateString = reader.GetString();
            
            if (string.IsNullOrWhiteSpace(dateString))
            {
                return default;
            }

            if (DateTime.TryParse(dateString, CultureInfo.InvariantCulture, DateTimeStyles.None, out var result))
            {
                return result;
            }

            foreach (var format in DateFormats)
            {
                if (DateTime.TryParseExact(dateString, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out result))
                {
                    return result;
                }
            }

            throw new JsonException($"无法解析日期时间: {dateString}");
        }

        throw new JsonException($"意外的 JSON 令牌类型: {reader.TokenType}");
    }

    public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
    {
        writer.WriteStringValue(value.ToString("yyyy-MM-dd HH:mm:ss"));
    }
}
