// <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 InternalComputerUsePreviewTool : IJsonModel<InternalComputerUsePreviewTool>
    {
        internal InternalComputerUsePreviewTool() : this(InternalToolType.ComputerUsePreview, null, default, default, default)
        {
        }

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

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

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

        internal static InternalComputerUsePreviewTool DeserializeInternalComputerUsePreviewTool(JsonElement element, ModelReaderWriterOptions options)
        {
            if (element.ValueKind == JsonValueKind.Null)
            {
                return null;
            }
            InternalToolType kind = default;
            IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
            ComputerToolEnvironment environment = default;
            int displayWidth = default;
            int displayHeight = default;
            foreach (var prop in element.EnumerateObject())
            {
                if (prop.NameEquals("type"u8))
                {
                    kind = new InternalToolType(prop.Value.GetString());
                    continue;
                }
                if (prop.NameEquals("environment"u8))
                {
                    environment = new ComputerToolEnvironment(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;
                }
                // Plugin customization: remove options.Format != "W" check
                additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
            }
            return new InternalComputerUsePreviewTool(kind, additionalBinaryDataProperties, environment, displayWidth, displayHeight);
        }

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

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

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

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

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