// <auto-generated/>

#nullable disable

using System;
using System.ClientModel.Primitives;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Linq;

namespace SampleTypeSpec
{
    /// <summary> The XmlModelWithNamespace. </summary>
    public partial class XmlModelWithNamespace : IPersistableModel<XmlModelWithNamespace>
    {
        /// <summary> Initializes a new instance of <see cref="XmlModelWithNamespace"/> for deserialization. </summary>
        internal XmlModelWithNamespace()
        {
        }

        /// <param name="data"> The data to parse. </param>
        /// <param name="options"> The client options for reading and writing models. </param>
        protected virtual XmlModelWithNamespace PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options)
        {
            string format = options.Format == "W" ? ((IPersistableModel<XmlModelWithNamespace>)this).GetFormatFromOptions(options) : options.Format;
            switch (format)
            {
                case "X":
                    using (Stream dataStream = data.ToStream())
                    {
                        return DeserializeXmlModelWithNamespace(XElement.Load(dataStream, LoadOptions.PreserveWhitespace), options);
                    }
                default:
                    throw new FormatException($"The model {nameof(XmlModelWithNamespace)} does not support reading '{options.Format}' format.");
            }
        }

        /// <param name="options"> The client options for reading and writing models. </param>
        protected virtual BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options)
        {
            string format = options.Format == "W" ? ((IPersistableModel<XmlModelWithNamespace>)this).GetFormatFromOptions(options) : options.Format;
            switch (format)
            {
                case "X":
                    using (MemoryStream stream = new MemoryStream(256))
                    {
                        using (XmlWriter writer = XmlWriter.Create(stream, ModelSerializationExtensions.XmlWriterSettings))
                        {
                            WriteXml(writer, options, "XmlModelWithNamespace");
                        }
                        if (stream.Position > int.MaxValue)
                        {
                            return BinaryData.FromStream(stream);
                        }
                        else
                        {
                            return new BinaryData(stream.GetBuffer().AsMemory(0, (int)stream.Position));
                        }
                    }
                default:
                    throw new FormatException($"The model {nameof(XmlModelWithNamespace)} does not support writing '{options.Format}' format.");
            }
        }

        /// <param name="options"> The client options for reading and writing models. </param>
        BinaryData IPersistableModel<XmlModelWithNamespace>.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options);

        /// <param name="data"> The data to parse. </param>
        /// <param name="options"> The client options for reading and writing models. </param>
        XmlModelWithNamespace IPersistableModel<XmlModelWithNamespace>.Create(BinaryData data, ModelReaderWriterOptions options) => PersistableModelCreateCore(data, options);

        /// <param name="options"> The client options for reading and writing models. </param>
        string IPersistableModel<XmlModelWithNamespace>.GetFormatFromOptions(ModelReaderWriterOptions options) => "X";

        /// <param name="writer"> The XML writer. </param>
        /// <param name="options"> The client options for reading and writing models. </param>
        /// <param name="nameHint"> An optional name hint. </param>
        private void WriteXml(XmlWriter writer, ModelReaderWriterOptions options, string nameHint)
        {
            if (nameHint != null)
            {
                writer.WriteStartElement("ns1", nameHint, "http://www.example.com/namespace");
            }

            XmlModelWriteCore(writer, options);

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

        /// <param name="writer"> The XML writer. </param>
        /// <param name="options"> The client options for reading and writing models. </param>
        internal virtual void XmlModelWriteCore(XmlWriter writer, ModelReaderWriterOptions options)
        {
            string format = options.Format == "W" ? ((IPersistableModel<XmlModelWithNamespace>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "X")
            {
                throw new FormatException($"The model {nameof(XmlModelWithNamespace)} does not support writing '{format}' format.");
            }

            writer.WriteStartElement("foo");
            writer.WriteValue(Foo);
            writer.WriteEndElement();
        }

        /// <param name="element"> The xml element to deserialize. </param>
        /// <param name="options"> The client options for reading and writing models. </param>
        internal static XmlModelWithNamespace DeserializeXmlModelWithNamespace(XElement element, ModelReaderWriterOptions options)
        {
            if (element == null)
            {
                return null;
            }

            string foo = default;
            IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();

            foreach (var child in element.Elements())
            {
                string localName = child.Name.LocalName;
                if (localName == "foo")
                {
                    foo = (string)child;
                    continue;
                }
            }
            return new XmlModelWithNamespace(foo, additionalBinaryDataProperties);
        }
    }
}
