// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Microsoft.Generator.CSharp.Input
{
    internal sealed class TypeSpecInputClientConverter : JsonConverter<InputClient>
    {
        private readonly TypeSpecReferenceHandler _referenceHandler;

        public TypeSpecInputClientConverter(TypeSpecReferenceHandler referenceHandler)
        {
            _referenceHandler = referenceHandler;
        }

        public override InputClient? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
            => reader.ReadReferenceAndResolve<InputClient>(_referenceHandler.CurrentResolver) ?? CreateInputClient(ref reader, null, options, _referenceHandler.CurrentResolver);

        public override void Write(Utf8JsonWriter writer, InputClient value, JsonSerializerOptions options)
            => throw new NotSupportedException("Writing not supported");

        private static InputClient? CreateInputClient(ref Utf8JsonReader reader, string? id, JsonSerializerOptions options, ReferenceResolver resolver)
        {
            if (id == null)
            {
                reader.TryReadReferenceId(ref id);
            }
            id = id ?? throw new JsonException();

            var client = new InputClient();
            resolver.AddReference(id, client);

            string? name = null;
            string? description = null;
            IReadOnlyList<InputOperation>? operations = null;
            bool creatable = false;
            IReadOnlyList<InputParameter>? parameters = null;
            string? parent = null;

            while (reader.TokenType != JsonTokenType.EndObject)
            {
                var isKnownProperty = reader.TryReadString(nameof(InputClient.Name), ref name)
                    || reader.TryReadString(nameof(InputClient.Description), ref description)
                    || reader.TryReadWithConverter(nameof(InputClient.Operations), options, ref operations)
                    || reader.TryReadBoolean(nameof(InputClient.Creatable), ref creatable)
                    || reader.TryReadWithConverter(nameof(InputClient.Parameters), options, ref parameters)
                    || reader.TryReadString(nameof(InputClient.Parent), ref parent);

                if (!isKnownProperty)
                {
                    reader.SkipProperty();
                }
            }

            client.Name = name ?? throw new JsonException("InputClient must have name");
            client.Description = description ?? throw new JsonException("InputClient must have description");
            client.Operations = operations ?? Array.Empty<InputOperation>();
            client.Creatable = creatable;
            client.Parameters = parameters ?? Array.Empty<InputParameter>();
            client.Parent = parent;

            return client;
        }
    }
}
