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

namespace Download_Faturas
{
    public class DefaultDateTimeConverter : JsonConverter<DateTime>
    {
        public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            if (reader.TokenType == JsonTokenType.String)
            {
                string? str = reader.GetString();

                if (string.IsNullOrWhiteSpace(str))
                {
                    return DateTime.MinValue;
                }

                if (DateTime.TryParse(str, out var date))
                {
                    return date;
                }
            }

            return DateTime.MinValue;
        }

        public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
        {
            writer.WriteStringValue(value);
        }
    }
}
