// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT License.

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

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

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

        /// <param name="options"> The client options for reading and writing models. </param>
        string IPersistableModel<XmlItem>.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(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<XmlItem>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "X")
            {
                throw new FormatException($"The model {nameof(XmlItem)} does not support writing '{format}' format.");
            }

            writer.WriteStartAttribute("itemId");
            writer.WriteValue(ItemId);
            writer.WriteEndAttribute();
            writer.WriteStartElement("itemName");
            writer.WriteValue(ItemName);
            writer.WriteEndElement();
            writer.WriteStartElement("itemValue");
            writer.WriteValue(ItemValue);
            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 XmlItem DeserializeXmlItem(XElement element, ModelReaderWriterOptions options)
        {
            if (element == null)
            {
                return null;
            }

            string itemName = default;
            int itemValue = default;
            string itemId = default;
            IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();

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

            foreach (var child in element.Elements())
            {
                string localName = child.Name.LocalName;
                if (localName == "itemName")
                {
                    itemName = (string)child;
                    continue;
                }
                if (localName == "itemValue")
                {
                    itemValue = (int)child;
                    continue;
                }
            }
            return new XmlItem(itemName, itemValue, itemId, additionalBinaryDataProperties);
        }
    }
}
