// <copyright file="JsonGuidConverter.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>

#pragma warning disable SA1600 // Elements should be documented

namespace Jellyfin.Extensions.Json.Converters
{
    using System;
    using System.Globalization;
    using System.Text.Json;
    using System.Text.Json.Serialization;

    /// <summary>
    /// Converts a GUID object or value to/from JSON.
    /// </summary>
    public class JsonGuidConverter : JsonConverter<Guid>
    {
        /// <inheritdoc />
        public override Guid Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
            => reader.IsNull()
                ? Guid.Empty
                : ReadInternal(ref reader);

        /// <inheritdoc />
        public override void Write(Utf8JsonWriter writer, Guid value, JsonSerializerOptions options)
        {
            ArgumentNullException.ThrowIfNull(writer);
            WriteInternal(writer, value);
        }

        // TODO: optimize by parsing the UTF8 bytes instead of converting to string first
        internal static Guid ReadInternal(ref Utf8JsonReader reader)
            => Guid.Parse(reader.GetString()!); // null got handled higher up the call stack

        internal static void WriteInternal(Utf8JsonWriter writer, Guid value)
            => writer.WriteStringValue(value.ToString("N", CultureInfo.InvariantCulture));
    }
}
