using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.ComponentModel;

namespace VisimBox.Scene
{
    /// <summary>
    /// Unknown node type from importing the scene file, used to store user data so they won't be lost when opened without
    /// support for user nodes loaded.
    /// </summary>
    public class UnknownNode : Node
    {
        [Category("Unknown Node")]
        [Description("Name of the node in the original data")]
        public string OriginalName { get; set; }

        //protected StringBuilder XMLData = new StringBuilder();

        protected XmlDocument XMLData = new XmlDocument();


        [Category("Unknown Node")]
        [Description("Original data in the node")]
        public XmlDocument OriginalData { get { return XMLData; } }

        public override RenderFlags DefaultRenderFlags
        {
            get
            {
                return Scene.RenderFlags.DataObject;
            }
        }


        public UnknownNode(string originalname)
        {
            OriginalName = originalname;

            XMLData.AppendChild(XMLData.CreateElement("root"));
        }

        public UnknownNode(XmlReader reader) : this("UnknownNode")
        {
            ReadXml(reader);
        }


        protected override void CopyData(Node targetNode)
        {
            base.CopyData(targetNode);

            UnknownNode mynode = targetNode as UnknownNode;
            if (mynode == null)
            {
                VisimBox.Console.DebugLine(FullName + " asked to do copy to different node type!");
                return; 
            }
            if (XMLData == null)
                return;
            XmlDocument xmld = (XmlDocument) XMLData.CloneNode(true);
            mynode.XMLData = xmld;
        }

        public override bool ReadInternalData(System.Xml.XmlReader reader)
        {
            if (base.ReadInternalData(reader))
                return true;

            //we have to save ALL data from the xml reader
            //note - should probably add setting to read whole unknown node as xml document....
            //XMLData.AppendLine(reader.ReadOuterXml());

            string internalstr = reader.ReadOuterXml();
            
            //todo: find some smarter way...
            XmlReader innerreader = new XmlTextReader(internalstr, XmlNodeType.Element, null);
            XmlNode xmlnode = XMLData.ReadNode(innerreader);
            XMLData.FirstChild.AppendChild(xmlnode);
            innerreader.Close();
            return true;
        }

        public override void WriteInternalData(System.Xml.XmlWriter writer)
        {
            base.WriteInternalData(writer);
            //save original XML data


            foreach (XmlNode node in XMLData.FirstChild.ChildNodes)
            {
                node.WriteTo(writer);
            }

            //writer.WriteRaw(XMLData.ToString());
        }

        public override string XMLNodeName
        {
            get
            {
                if (OriginalName != null)
                    return OriginalName;
                else return base.XMLNodeName;
            }
        }

    }
}

