// <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 InternalResponsesComputerTool : IJsonModel<InternalResponsesComputerTool>
    {
        internal InternalResponsesComputerTool()
        {
        }

        void IJsonModel<InternalResponsesComputerTool>.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<InternalResponsesComputerTool>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(InternalResponsesComputerTool)} does not support writing '{format}' format.");
            }
            base.JsonModelWriteCore(writer, options);
            if (_additionalBinaryDataProperties?.ContainsKey("display_width") != true)
            {
                writer.WritePropertyName("display_width"u8);
                writer.WriteNumberValue(DisplayWidth);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("display_height") != true)
            {
                writer.WritePropertyName("display_height"u8);
                writer.WriteNumberValue(DisplayHeight);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("environment") != true)
            {
                writer.WritePropertyName("environment"u8);
                writer.WriteStringValue(Environment.ToString());
            }
        }

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

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

        internal static InternalResponsesComputerTool DeserializeInternalResponsesComputerTool(JsonElement element, ModelReaderWriterOptions options)
        {
            if (element.ValueKind == JsonValueKind.Null)
            {
                return null;
            }
            InternalResponsesToolType @type = default;
            IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
            int displayWidth = default;
            int displayHeight = default;
            ComputerToolEnvironment environment = default;
            foreach (var prop in element.EnumerateObject())
            {
                if (prop.NameEquals("type"u8))
                {
                    @type = new InternalResponsesToolType(prop.Value.GetString());
                    continue;
                }
                if (prop.NameEquals("display_width"u8))
                {
                    displayWidth = prop.Value.GetInt32();
                    continue;
                }
                if (prop.NameEquals("display_height"u8))
                {
                    displayHeight = prop.Value.GetInt32();
                    continue;
                }
                if (prop.NameEquals("environment"u8))
                {
                    environment = new ComputerToolEnvironment(prop.Value.GetString());
                    continue;
                }
                additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
            }
            return new InternalResponsesComputerTool(@type, additionalBinaryDataProperties, displayWidth, displayHeight, environment);
        }

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

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

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

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

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

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

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