// <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> A nested model for XML testing. </summary>
    public partial class XmlNestedModel : IPersistableModel<XmlNestedModel>
    {
        /// <summary> Initializes a new instance of <see cref="XmlNestedModel"/> for deserialization. </summary>
        internal XmlNestedModel()
        {
        }

        /// <param name="data"> The data to parse. </param>
        /// <param name="options"> The client options for reading and writing models. </param>
        protected virtual XmlNestedModel PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options)
        {
            string format = options.Format == "W" ? ((IPersistableModel<XmlNestedModel>)this).GetFormatFromOptions(options) : options.Format;
            switch (format)
            {
                case "X":
                    using (Stream dataStream = data.ToStream())
                    {
                        return DeserializeXmlNestedModel(XElement.Load(dataStream, LoadOptions.PreserveWhitespace), options);
                    }
                default:
                    throw new FormatException($"The model {nameof(XmlNestedModel)} 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<XmlNestedModel>)this).GetFormatFromOptions(options) : options.Format;
            switch (format)
            {
                case "X":
                    using (MemoryStream stream = new MemoryStream(256))
                    {
                        using (XmlWriter writer = XmlWriter.Create(stream, ModelSerializationExtensions.XmlWriterSettings))
                        {
                            Write(writer, options, "XmlNestedModel");
                        }
                        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(XmlNestedModel)} does not support writing '{options.Format}' format.");
            }
        }

        /// <param name="options"> The client options for reading and writing models. </param>
        BinaryData IPersistableModel<XmlNestedModel>.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>
        XmlNestedModel IPersistableModel<XmlNestedModel>.Create(BinaryData data, ModelReaderWriterOptions options) => PersistableModelCreateCore(data, options);

        /// <param name="options"> The client options for reading and writing models. </param>
        string IPersistableModel<XmlNestedModel>.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 Write(XmlWriter writer, ModelReaderWriterOptions options, string nameHint)
        {
            if (nameHint != null)
            {
                writer.WriteStartElement(nameHint);
            }

            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>
        protected virtual void XmlModelWriteCore(XmlWriter writer, ModelReaderWriterOptions options)
        {
            string format = options.Format == "W" ? ((IPersistableModel<XmlNestedModel>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "X")
            {
                throw new FormatException($"The model {nameof(XmlNestedModel)} does not support writing '{format}' format.");
            }

            writer.WriteStartAttribute("nestedId");
            writer.WriteValue(NestedId);
            writer.WriteEndAttribute();
            writer.WriteStartElement("value");
            writer.WriteValue(Value);
            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 XmlNestedModel DeserializeXmlNestedModel(XElement element, ModelReaderWriterOptions options)
        {
            if (element == null)
            {
                return null;
            }

            string value = default;
            int nestedId = default;
            IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();

            foreach (var attr in element.Attributes())
            {
                string localName = attr.Name.LocalName;
                if (localName == "nestedId")
                {
                    nestedId = (int)attr;
                    continue;
                }
            }

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