// <auto-generated/>

#nullable disable

using System;
using System.ClientModel;
using System.ClientModel.Primitives;
using System.Collections.Generic;
using System.Text.Json;
using OpenAI;

namespace OpenAI.Responses
{
    internal partial class InternalResponsesFunctionTool : IJsonModel<InternalResponsesFunctionTool>
    {
        internal InternalResponsesFunctionTool()
        {
        }

        void IJsonModel<InternalResponsesFunctionTool>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options)
        {
            writer.WriteStartObject();
            JsonModelWriteCore(writer, options);
            writer.WriteEndObject();
        }

        protected override void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
        {
            string format = options.Format == "W" ? ((IPersistableModel<InternalResponsesFunctionTool>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(InternalResponsesFunctionTool)} does not support writing '{format}' format.");
            }
            base.JsonModelWriteCore(writer, options);
            if (_additionalBinaryDataProperties?.ContainsKey("name") != true)
            {
                writer.WritePropertyName("name"u8);
                writer.WriteStringValue(Name);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("description") != true)
            {
                writer.WritePropertyName("description"u8);
                writer.WriteStringValue(Description);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("parameters") != true)
            {
                writer.WritePropertyName("parameters"u8);
#if NET6_0_OR_GREATER
                writer.WriteRawValue(Parameters);
#else
                using (JsonDocument document = JsonDocument.Parse(Parameters))
                {
                    JsonSerializer.Serialize(writer, document.RootElement);
                }
#endif
            }
            if (_additionalBinaryDataProperties?.ContainsKey("strict") != true)
            {
                writer.WritePropertyName("strict"u8);
                writer.WriteBooleanValue(Strict);
            }
        }

        InternalResponsesFunctionTool IJsonModel<InternalResponsesFunctionTool>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => (InternalResponsesFunctionTool)JsonModelCreateCore(ref reader, options);

        protected override ResponseTool JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options)
        {
            string format = options.Format == "W" ? ((IPersistableModel<InternalResponsesFunctionTool>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(InternalResponsesFunctionTool)} does not support reading '{format}' format.");
            }
            using JsonDocument document = JsonDocument.ParseValue(ref reader);
            return DeserializeInternalResponsesFunctionTool(document.RootElement, options);
        }

        internal static InternalResponsesFunctionTool DeserializeInternalResponsesFunctionTool(JsonElement element, ModelReaderWriterOptions options)
        {
            if (element.ValueKind == JsonValueKind.Null)
            {
                return null;
            }
            InternalResponsesToolType @type = default;
            IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
            string name = default;
            string description = default;
            BinaryData parameters = default;
            bool strict = default;
            foreach (var prop in element.EnumerateObject())
            {
                if (prop.NameEquals("type"u8))
                {
                    @type = new InternalResponsesToolType(prop.Value.GetString());
                    continue;
                }
                if (prop.NameEquals("name"u8))
                {
                    name = prop.Value.GetString();
                    continue;
                }
                if (prop.NameEquals("description"u8))
                {
                    description = prop.Value.GetString();
                    continue;
                }
                if (prop.NameEquals("parameters"u8))
                {
                    parameters = BinaryData.FromString(prop.Value.GetRawText());
                    continue;
                }
                if (prop.NameEquals("strict"u8))
                {
                    strict = prop.Value.GetBoolean();
                    continue;
                }
                additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
            }
            return new InternalResponsesFunctionTool(
                @type,
                additionalBinaryDataProperties,
                name,
                description,
                parameters,
                strict);
        }

        BinaryData IPersistableModel<InternalResponsesFunctionTool>.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options);

        protected override BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options)
        {
            string format = options.Format == "W" ? ((IPersistableModel<InternalResponsesFunctionTool>)this).GetFormatFromOptions(options) : options.Format;
            switch (format)
            {
                case "J":
                    return ModelReaderWriter.Write(this, options);
                default:
                    throw new FormatException($"The model {nameof(InternalResponsesFunctionTool)} does not support writing '{options.Format}' format.");
            }
        }

        InternalResponsesFunctionTool IPersistableModel<InternalResponsesFunctionTool>.Create(BinaryData data, ModelReaderWriterOptions options) => (InternalResponsesFunctionTool)PersistableModelCreateCore(data, options);

        protected override ResponseTool PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options)
        {
            string format = options.Format == "W" ? ((IPersistableModel<InternalResponsesFunctionTool>)this).GetFormatFromOptions(options) : options.Format;
            switch (format)
            {
                case "J":
                    using (JsonDocument document = JsonDocument.Parse(data))
                    {
                        return DeserializeInternalResponsesFunctionTool(document.RootElement, options);
                    }
                default:
                    throw new FormatException($"The model {nameof(InternalResponsesFunctionTool)} does not support reading '{options.Format}' format.");
            }
        }

        string IPersistableModel<InternalResponsesFunctionTool>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J";

        public static implicit operator BinaryContent(InternalResponsesFunctionTool internalResponsesFunctionTool)
        {
            if (internalResponsesFunctionTool == null)
            {
                return null;
            }
            return BinaryContent.Create(internalResponsesFunctionTool, ModelSerializationExtensions.WireOptions);
        }

        public static explicit operator InternalResponsesFunctionTool(ClientResult result)
        {
            using PipelineResponse response = result.GetRawResponse();
            using JsonDocument document = JsonDocument.Parse(response.Content);
            return DeserializeInternalResponsesFunctionTool(document.RootElement, ModelSerializationExtensions.WireOptions);
        }
    }
}
