// <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 InternalResponsesInputImageContentPart : IJsonModel<InternalResponsesInputImageContentPart>
    {
        void IJsonModel<InternalResponsesInputImageContentPart>.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<InternalResponsesInputImageContentPart>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(InternalResponsesInputImageContentPart)} does not support writing '{format}' format.");
            }
            base.JsonModelWriteCore(writer, options);
            if (Optional.IsDefined(ImageUrl) && _additionalBinaryDataProperties?.ContainsKey("image_url") != true)
            {
                writer.WritePropertyName("image_url"u8);
                writer.WriteStringValue(ImageUrl);
            }
            if (Optional.IsDefined(FileId) && _additionalBinaryDataProperties?.ContainsKey("file_id") != true)
            {
                writer.WritePropertyName("file_id"u8);
                writer.WriteStringValue(FileId);
            }
            if (Optional.IsDefined(Detail) && _additionalBinaryDataProperties?.ContainsKey("detail") != true)
            {
                writer.WritePropertyName("detail"u8);
                writer.WriteStringValue(Detail.Value.ToString());
            }
        }

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

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

        internal static InternalResponsesInputImageContentPart DeserializeInternalResponsesInputImageContentPart(JsonElement element, ModelReaderWriterOptions options)
        {
            if (element.ValueKind == JsonValueKind.Null)
            {
                return null;
            }
            InternalResponsesContentType internalType = default;
            IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
            string imageUrl = default;
            string fileId = default;
            ResponseImageDetailLevel? detail = default;
            foreach (var prop in element.EnumerateObject())
            {
                if (prop.NameEquals("type"u8))
                {
                    internalType = new InternalResponsesContentType(prop.Value.GetString());
                    continue;
                }
                if (prop.NameEquals("image_url"u8))
                {
                    imageUrl = prop.Value.GetString();
                    continue;
                }
                if (prop.NameEquals("file_id"u8))
                {
                    fileId = prop.Value.GetString();
                    continue;
                }
                if (prop.NameEquals("detail"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    detail = new ResponseImageDetailLevel(prop.Value.GetString());
                    continue;
                }
                additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
            }
            return new InternalResponsesInputImageContentPart(internalType, additionalBinaryDataProperties, imageUrl, fileId, detail);
        }

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

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

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

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

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

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

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