// <auto-generated/>

#nullable disable

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

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

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

        protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
        {
            string format = options.Format == "W" ? ((IPersistableModel<StreamingResponseUpdate>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(StreamingResponseUpdate)} does not support writing '{format}' format.");
            }
            if (_additionalBinaryDataProperties?.ContainsKey("type") != true)
            {
                writer.WritePropertyName("type"u8);
                writer.WriteStringValue(Type.ToString());
            }
            if (_additionalBinaryDataProperties != null)
            {
                foreach (var item in _additionalBinaryDataProperties)
                {
                    if (ModelSerializationExtensions.IsSentinelValue(item.Value))
                    {
                        continue;
                    }
                    writer.WritePropertyName(item.Key);
#if NET6_0_OR_GREATER
                    writer.WriteRawValue(item.Value);
#else
                    using (JsonDocument document = JsonDocument.Parse(item.Value))
                    {
                        JsonSerializer.Serialize(writer, document.RootElement);
                    }
#endif
                }
            }
        }

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

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

        internal static StreamingResponseUpdate DeserializeStreamingResponseUpdate(JsonElement element, ModelReaderWriterOptions options)
        {
            if (element.ValueKind == JsonValueKind.Null)
            {
                return null;
            }
            if (element.TryGetProperty("type"u8, out JsonElement discriminator))
            {
                switch (discriminator.GetString())
                {
                    case "error":
                        return StreamingResponseErrorUpdate.DeserializeStreamingResponseErrorUpdate(element, options);
                    case "response.completed":
                        return StreamingResponseCompletedUpdate.DeserializeStreamingResponseCompletedUpdate(element, options);
                    case "response.content_part.added":
                        return StreamingResponseContentPartAddedUpdate.DeserializeStreamingResponseContentPartAddedUpdate(element, options);
                    case "response.content_part.done":
                        return StreamingResponseContentPartDoneUpdate.DeserializeStreamingResponseContentPartDoneUpdate(element, options);
                    case "response.created":
                        return StreamingResponseCreatedUpdate.DeserializeStreamingResponseCreatedUpdate(element, options);
                    case "response.failed":
                        return StreamingResponseFailedUpdate.DeserializeStreamingResponseFailedUpdate(element, options);
                    case "response.file_search_call.completed":
                        return StreamingResponseFileSearchCallCompletedUpdate.DeserializeStreamingResponseFileSearchCallCompletedUpdate(element, options);
                    case "response.file_search_call.in.progress":
                        return StreamingResponseFileSearchCallInProgressUpdate.DeserializeStreamingResponseFileSearchCallInProgressUpdate(element, options);
                    case "response.file_search_call.searching":
                        return StreamingResponseFileSearchCallSearchingUpdate.DeserializeStreamingResponseFileSearchCallSearchingUpdate(element, options);
                    case "response.function_call_arguments.delta":
                        return StreamingResponseFunctionCallArgumentsDeltaUpdate.DeserializeStreamingResponseFunctionCallArgumentsDeltaUpdate(element, options);
                    case "response.function_call_arguments.done":
                        return StreamingResponseFunctionCallArgumentsDoneUpdate.DeserializeStreamingResponseFunctionCallArgumentsDoneUpdate(element, options);
                    case "response.incomplete":
                        return StreamingResponseIncompleteUpdate.DeserializeStreamingResponseIncompleteUpdate(element, options);
                    case "response.in_progress":
                        return StreamingResponseInProgressUpdate.DeserializeStreamingResponseInProgressUpdate(element, options);
                    case "response.output_item.added":
                        return StreamingResponseOutputItemAddedUpdate.DeserializeStreamingResponseOutputItemAddedUpdate(element, options);
                    case "response.output_item.done":
                        return StreamingResponseOutputItemDoneUpdate.DeserializeStreamingResponseOutputItemDoneUpdate(element, options);
                    case "response.output_text.annotation.added":
                        return StreamingResponseTextAnnotationAddedUpdate.DeserializeStreamingResponseTextAnnotationAddedUpdate(element, options);
                    case "response.output_text.delta":
                        return StreamingResponseOutputTextDeltaUpdate.DeserializeStreamingResponseOutputTextDeltaUpdate(element, options);
                    case "response.output_text.done":
                        return StreamingResponseOutputTextDoneUpdate.DeserializeStreamingResponseOutputTextDoneUpdate(element, options);
                    case "response.refusal.delta":
                        return StreamingResponseRefusalDeltaUpdate.DeserializeStreamingResponseRefusalDeltaUpdate(element, options);
                    case "response.refusal.done":
                        return StreamingResponseRefusalDoneUpdate.DeserializeStreamingResponseRefusalDoneUpdate(element, options);
                    case "response.web_search_call.completed":
                        return StreamingResponseWebSearchCallCompletedUpdate.DeserializeStreamingResponseWebSearchCallCompletedUpdate(element, options);
                    case "response.web_search_call.in_progress":
                        return StreamingResponseWebSearchCallInProgressUpdate.DeserializeStreamingResponseWebSearchCallInProgressUpdate(element, options);
                    case "response.web_search_call.searching":
                        return StreamingResponseWebSearchCallSearchingUpdate.DeserializeStreamingResponseWebSearchCallSearchingUpdate(element, options);
                }
            }
            return UnknownResponsesResponseStreamEvent.DeserializeUnknownResponsesResponseStreamEvent(element, options);
        }

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

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

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

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

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

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

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