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

namespace VisimBox.Scene
{
    /// <summary>
    /// Node holding imported model
    /// </summary>
    public class ImportedNode : RenderableNode
    {
        /// <summary>
        /// Imported file name
        /// </summary>
        [Description("Imported file name")]
        [Category("Imported data")]
        public string FileName { get; set; }
        /// <summary>
        /// File format of the imported model
        /// </summary>
        [Description("File format of the imported model")]
        [Category("Imported data")]
        public string FileFormat { get; set; }

        /// <summary>
        /// Settings used to import data
        /// </summary>
        [Description("Settings used to import data")]
        [Category("Imported data")]
        public NodeImportSettings ImportedSettings { get; set; }

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


        public ImportedNode() : base()
        {
            DontSave = false;
        }

        public ImportedNode(string filename, string format = null)
        {
            DontSave = false;
        }

        public ImportedNode(XmlReader reader)
            : base(reader)
        {
            DontSave = false;
        }

        public override void WriteInternalData(System.Xml.XmlWriter writer)
        {
            base.WriteInternalData(writer);
            writer.WriteAttributeString("FileName", FileName);
            if (FileFormat != null)
            {
                writer.WriteAttributeString("FileFormat", FileFormat);
            }
            if (ImportedSettings != null)
            {
                writer.WriteStartElement("ImportSettings");
                ImportedSettings.WriteXml(writer);
                writer.WriteEndElement();
            }
        }

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

            ImportedNode mynode = targetNode as ImportedNode;
            if (mynode == null)
            {
                VisimBox.Console.DebugLine(FullName + " asked to do copy to different node type!");
                return;
            }
            mynode.FileFormat = this.FileFormat;
            mynode.FileName = this.FileName;
            if (ImportedSettings != null)
                mynode.ImportedSettings = new NodeImportSettings(ImportedSettings);
        }


        public override bool ReadInternalData(System.Xml.XmlReader reader)
        {
            if (base.ReadInternalData(reader))
                return true;
            if (reader.Name == FileName)
            {
                FileName = reader.ReadString();
                return true;
            }
            else if (reader.Name == "FileFormat")
            {
                FileFormat = reader.ReadString();
                return true;
            }
            else if (reader.Name == "ImportSettings")
            {
                ImportedSettings = new NodeImportSettings(reader);
            }
            return false;
        }

        /// <summary>
        /// Deserializes object from XML
        /// </summary>
        /// <param name="reader">XML reader</param>
        public override void ReadXml(XmlReader reader)
        {
            base.ReadXml(reader);

            NodeImportSettings setts = NodeFactory.Settings;

            if (ImportedSettings == null)
                NodeFactory.Settings = new NodeImportSettings();
            else
                NodeFactory.Settings = ImportedSettings;

            if (!NodeFactory.ImportToNode(this, FileName, FileFormat))
            {
                Console.DebugLine("Imported node " + FullName + " failed to reimport model!");
            }

            NodeFactory.Settings = setts;
        }


        public override void RenderGeometry(Renderer.RendererEventArgs args)
        {
            return; //do nothing
        }
    }
}
