using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace TEAMModelOS.SDK.Helper.Common.XmlHelper
{
    public class Json2Xml
    {
        [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.7.2612.0")]
        [System.SerializableAttribute()]
        [System.Xml.Serialization.XmlTypeAttribute(Namespace = "urn:ebay:apis:eBLBaseComponents")]
        public enum ItemSpecificSourceCodeType
        {

            /// <remarks/>
            ItemSpecific,

            /// <remarks/>
            Attribute,

            /// <remarks/>
            Product,

            /// <remarks/>
            CustomCode,
        }

        [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.7.2612.0")]
        [System.SerializableAttribute()]
        [System.Diagnostics.DebuggerStepThroughAttribute()]
        [System.ComponentModel.DesignerCategoryAttribute("code")]
        [System.Xml.Serialization.XmlTypeAttribute(Namespace = "urn:ebay:apis:eBLBaseComponents")]
        public partial class NameValueListType
        {

            private string nameField;

            private string[] valueField;

            private ItemSpecificSourceCodeType sourceField;

            private bool sourceFieldSpecified;

            private System.Xml.XmlElement[] anyField;

            /// <remarks/>
            public string Name
            {
                get
                {
                    return this.nameField;
                }
                set
                {
                    this.nameField = value;
                }
            }

            /// <remarks/>
            [System.Xml.Serialization.XmlElementAttribute("Value")]
            public string[] Value
            {
                get
                {
                    return this.valueField;
                }
                set
                {
                    this.valueField = value;
                }
            }

            /// <remarks/>
            public ItemSpecificSourceCodeType Source
            {
                get
                {
                    return this.sourceField;
                }
                set
                {
                    this.sourceField = value;
                }
            }

            /// <remarks/>
            [System.Xml.Serialization.XmlIgnoreAttribute()]
            public bool SourceSpecified
            {
                get
                {
                    return this.sourceFieldSpecified;
                }
                set
                {
                    this.sourceFieldSpecified = value;
                }
            }

            /// <remarks/>
            [System.Xml.Serialization.XmlAnyElementAttribute()]
            public System.Xml.XmlElement[] Any
            {
                get
                {
                    return this.anyField;
                }
                set
                {
                    this.anyField = value;
                }
            }
        }

        public class CustomItemSpecifics
        {
            public NameValueListType[] ItemSpecifics;
        }

        public class Item
        {
            public string Name { get; set; }

            public string Value { get; set; }

            public string[] RecommValues { get; set; }

            //public void DataTable2Entity(Item item, DataRow dr) {
            //    item.Name=dr.
            //}
        }

        public static string ObjectToText(Object inObject, Type inType)
        {
            String XmlizedString = null;
            MemoryStream ms = new MemoryStream();
            XmlSerializer xs = new XmlSerializer(inType);
            XmlTextWriter xmlTextWriter = new XmlTextWriter(ms, Encoding.UTF8);
            xs.Serialize(xmlTextWriter, inObject);
            ms = (MemoryStream)xmlTextWriter.BaseStream;
            XmlizedString = UTF8ByteArrayToString(ms.ToArray());

            return XmlizedString;
        }

        private static String UTF8ByteArrayToString(Byte[] characters)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            String constructedString = encoding.GetString(characters);

            if (constructedString.Length > 0)
            {
                constructedString = constructedString.Substring(1);
            }
            return (constructedString);
        }

        public static Object TextToObject(string inText, Type inType)
        {
            try
            {
                if (string.IsNullOrEmpty(inText))
                {
                    return null;
                }
                else
                {
                    XmlSerializer xs = new XmlSerializer(inType);
                    MemoryStream ms = new MemoryStream(StringToUTF8ByteArray(inText));
                    XmlTextWriter xmlTextWriter = new XmlTextWriter(ms, Encoding.UTF8);
                    return (Object)xs.Deserialize(ms);
                }
            }
            catch
            {
                return null;
            }
        }
        private static byte[] StringToUTF8ByteArray(string inText)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            Byte[] byteArray = encoding.GetBytes(inText);

            return byteArray;
        }
    }
}
