// <auto-generated/>

#nullable disable

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

namespace OpenAI.Responses
{
    internal partial class InternalImageGenTool : IJsonModel<InternalImageGenTool>
    {
        void IJsonModel<InternalImageGenTool>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options)
        {
            writer.WriteStartObject();
            JsonModelWriteCore(writer, options);
            writer.WriteEndObject();
        }

        [Experimental("OPENAI001")]
        protected override void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
        {
            string format = options.Format == "W" ? ((IPersistableModel<InternalImageGenTool>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(InternalImageGenTool)} does not support writing '{format}' format.");
            }
            base.JsonModelWriteCore(writer, options);
            if (Optional.IsDefined(Model) && _additionalBinaryDataProperties?.ContainsKey("model") != true)
            {
                writer.WritePropertyName("model"u8);
                writer.WriteStringValue(Model);
            }
            if (Optional.IsDefined(Quality) && _additionalBinaryDataProperties?.ContainsKey("quality") != true)
            {
                writer.WritePropertyName("quality"u8);
                writer.WriteStringValue(Quality.Value.ToString());
            }
            if (Optional.IsDefined(Size) && _additionalBinaryDataProperties?.ContainsKey("size") != true)
            {
                writer.WritePropertyName("size"u8);
                writer.WriteStringValue(Size.Value.ToString());
            }
            if (Optional.IsDefined(OutputFormat) && _additionalBinaryDataProperties?.ContainsKey("output_format") != true)
            {
                writer.WritePropertyName("output_format"u8);
                writer.WriteStringValue(OutputFormat.Value.ToString());
            }
            if (Optional.IsDefined(OutputCompression) && _additionalBinaryDataProperties?.ContainsKey("output_compression") != true)
            {
                writer.WritePropertyName("output_compression"u8);
                writer.WriteNumberValue(OutputCompression.Value);
            }
            if (Optional.IsDefined(Moderation) && _additionalBinaryDataProperties?.ContainsKey("moderation") != true)
            {
                writer.WritePropertyName("moderation"u8);
                writer.WriteStringValue(Moderation.Value.ToString());
            }
            if (Optional.IsDefined(Background) && _additionalBinaryDataProperties?.ContainsKey("background") != true)
            {
                writer.WritePropertyName("background"u8);
                writer.WriteStringValue(Background.Value.ToString());
            }
            if (Optional.IsDefined(InputImageMask) && _additionalBinaryDataProperties?.ContainsKey("input_image_mask") != true)
            {
                writer.WritePropertyName("input_image_mask"u8);
                writer.WriteObjectValue(InputImageMask, options);
            }
            if (Optional.IsDefined(PartialImages) && _additionalBinaryDataProperties?.ContainsKey("partial_images") != true)
            {
                writer.WritePropertyName("partial_images"u8);
                writer.WriteNumberValue(PartialImages.Value);
            }
        }

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

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

        internal static InternalImageGenTool DeserializeInternalImageGenTool(JsonElement element, ModelReaderWriterOptions options)
        {
            if (element.ValueKind == JsonValueKind.Null)
            {
                return null;
            }
            InternalToolType kind = default;
            IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
            string model = default;
            InternalImageGenToolQuality? quality = default;
            InternalImageGenToolSize? size = default;
            InternalImageGenToolOutputFormat? outputFormat = default;
            int? outputCompression = default;
            InternalImageGenToolModeration? moderation = default;
            InternalImageGenToolBackground? background = default;
            InternalImageGenToolInputImageMask inputImageMask = default;
            int? partialImages = default;
            foreach (var prop in element.EnumerateObject())
            {
                if (prop.NameEquals("type"u8))
                {
                    kind = new InternalToolType(prop.Value.GetString());
                    continue;
                }
                if (prop.NameEquals("model"u8))
                {
                    model = prop.Value.GetString();
                    continue;
                }
                if (prop.NameEquals("quality"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    quality = new InternalImageGenToolQuality(prop.Value.GetString());
                    continue;
                }
                if (prop.NameEquals("size"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    size = new InternalImageGenToolSize(prop.Value.GetString());
                    continue;
                }
                if (prop.NameEquals("output_format"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    outputFormat = new InternalImageGenToolOutputFormat(prop.Value.GetString());
                    continue;
                }
                if (prop.NameEquals("output_compression"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    outputCompression = prop.Value.GetInt32();
                    continue;
                }
                if (prop.NameEquals("moderation"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    moderation = new InternalImageGenToolModeration(prop.Value.GetString());
                    continue;
                }
                if (prop.NameEquals("background"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    background = new InternalImageGenToolBackground(prop.Value.GetString());
                    continue;
                }
                if (prop.NameEquals("input_image_mask"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    inputImageMask = InternalImageGenToolInputImageMask.DeserializeInternalImageGenToolInputImageMask(prop.Value, options);
                    continue;
                }
                if (prop.NameEquals("partial_images"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    partialImages = prop.Value.GetInt32();
                    continue;
                }
                // Plugin customization: remove options.Format != "W" check
                additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
            }
            return new InternalImageGenTool(
                kind,
                additionalBinaryDataProperties,
                model,
                quality,
                size,
                outputFormat,
                outputCompression,
                moderation,
                background,
                inputImageMask,
                partialImages);
        }

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

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

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

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

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