// <auto-generated/>

#nullable disable

using System;
using System.ClientModel.Primitives;
using System.IO;
using System.Xml;
using Sample;

namespace Sample.Models
{
    public partial class TestXmlModel : global::System.ClientModel.Primitives.IPersistableModel<global::Sample.Models.TestXmlModel>
    {
        internal TestXmlModel()
        {
        }

        protected virtual global::System.BinaryData PersistableModelWriteCore(global::System.ClientModel.Primitives.ModelReaderWriterOptions options)
        {
            string format = (options.Format == "W") ? ((global::System.ClientModel.Primitives.IPersistableModel<global::Sample.Models.TestXmlModel>)this).GetFormatFromOptions(options) : options.Format;
            switch (format)
            {
                case "X":
                    using (global::System.IO.MemoryStream stream = new global::System.IO.MemoryStream(256))
                    {
                        using (global::System.Xml.XmlWriter writer = global::System.Xml.XmlWriter.Create(stream, global::Sample.ModelSerializationExtensions.XmlWriterSettings))
                        {
                            this.WriteXml(writer, options, "TestXmlModel");
                        }
                        if ((stream.Position > int.MaxValue))
                        {
                            return global::System.BinaryData.FromStream(stream);
                        }
                        else
                        {
                            return new global::System.BinaryData(stream.GetBuffer().AsMemory(0, ((int)stream.Position)));
                        }
                    }
                default:
                    throw new global::System.FormatException($"The model {nameof(global::Sample.Models.TestXmlModel)} does not support writing '{options.Format}' format.");
            }
        }

        global::System.BinaryData global::System.ClientModel.Primitives.IPersistableModel<global::Sample.Models.TestXmlModel>.Write(global::System.ClientModel.Primitives.ModelReaderWriterOptions options) => this.PersistableModelWriteCore(options);

        private void WriteXml(global::System.Xml.XmlWriter writer, global::System.ClientModel.Primitives.ModelReaderWriterOptions options, string nameHint)
        {
            if ((nameHint != null))
            {
                writer.WriteStartElement(nameHint);
            }

            this.XmlModelWriteCore(writer, options);

            if ((nameHint != null))
            {
                writer.WriteEndElement();
            }
        }

        internal virtual void XmlModelWriteCore(global::System.Xml.XmlWriter writer, global::System.ClientModel.Primitives.ModelReaderWriterOptions options)
        {
            string format = (options.Format == "W") ? ((global::System.ClientModel.Primitives.IPersistableModel<global::Sample.Models.TestXmlModel>)this).GetFormatFromOptions(options) : options.Format;
            if ((format != "X"))
            {
                throw new global::System.FormatException($"The model {nameof(global::Sample.Models.TestXmlModel)} does not support writing '{format}' format.");
            }

            writer.WriteStartElement("counts");
            foreach (int item in Counts)
            {
                writer.WriteStartElement("int32");
                writer.WriteValue(item);
                writer.WriteEndElement();
            }
            writer.WriteEndElement();
        }
    }
}
