using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;

namespace ExtensiblePortfolioSite.SDK
{
    public static class Json
    {
        private static class DefaultConverters
        {
            public sealed class UriConverter : JsonConverter<Uri>
            {
                public override Uri? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
                {
                    String? s = reader.GetString();
                    return s == null ? null : new Uri(s);
                }

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

        private static readonly JsonSerializerOptions Options = new()
        {
            AllowTrailingCommas = true,
            DefaultBufferSize = 4098,
            DefaultIgnoreCondition = JsonIgnoreCondition.Never,
            DictionaryKeyPolicy = JsonNamingPolicy.CamelCase,
            IgnoreReadOnlyFields = true,
            IgnoreReadOnlyProperties = true,
            IncludeFields = true,
            MaxDepth = 64,
            NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals | JsonNumberHandling.AllowReadingFromString,
            PropertyNameCaseInsensitive = true,
            PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
            ReadCommentHandling = JsonCommentHandling.Skip,
            UnknownTypeHandling = JsonUnknownTypeHandling.JsonElement,
            WriteIndented = false,
        };
        private static readonly IList<JsonConverter> JsonConverters = Options.Converters;
        static Json()
        {
            JsonConverters.Add(new DefaultConverters.UriConverter());
        }

        internal static void Register(JsonConverter Converter) => JsonConverters.Add(Converter);
        internal static void Unregister(JsonConverter Converter) => JsonConverters.Remove(Converter);




        public static T? Deserialize<T>(String String) => JsonSerializer.Deserialize<T>(String, Options);
        public static T? Deserialize<T>(Stream STM) => JsonSerializer.Deserialize<T>(STM, Options);
        public static T? Deserialize<T>(ref Utf8JsonReader Reader) => JsonSerializer.Deserialize<T>(ref Reader, Options);
        public static T? Deserialize<T>(JsonElement Element) => JsonSerializer.Deserialize<T>(Element, Options);
        public static ValueTask<T?> DeserializeAsync<T>(Stream STM) => JsonSerializer.DeserializeAsync<T>(STM, Options);
        public static ValueTask<T?> DeserializeAsync<T>(Stream STM, CancellationToken cancellationToken) => JsonSerializer.DeserializeAsync<T>(STM, Options, cancellationToken);


        public static String Serialize<T>(T Obj) => JsonSerializer.Serialize<T>(Obj, Options);
        public static void Serialize<T>(T Obj, Stream Outp) => JsonSerializer.Serialize<T>(Outp, Obj, Options);
        public static void Serialize<T>(T Obj, Utf8JsonWriter Writer) => JsonSerializer.Serialize<T>(Writer, Obj, Options);

        public static Task SerializeAsync<T>(T Obj, Stream Outp) => JsonSerializer.SerializeAsync<T>(Outp, Obj, Options);
        public static Task SerializeAsync<T>(T Obj, Stream Outp, CancellationToken cancellationToken) => JsonSerializer.SerializeAsync<T>(Outp, Obj, Options, cancellationToken);

    }
}
