using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VisimBox.Renderer;
using VisimBox.Renderer.RenderObjects;
using VisiMath;
using System.ComponentModel;

namespace VisimBox.Scene
{
    public class SkyBoxNode : Node, IRenderable
    {
        protected SkyboxData skybox;

        [Description("Skybox data")]
        [Category("Rendering data")]
        [TypeConverter(typeof(ExpandableObjectConverter))]
        public SkyboxData Skybox { get { return skybox; } }

        public SkyBoxNode()
        {
        }

        public SkyBoxNode(System.Xml.XmlReader reader)
            : base(reader)
        {
        }

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


        /// <summary>
        /// Creates skybox node from already initializes skyboxdata
        /// </summary>
        /// <param name="owner">Owner node</param>
        /// <param name="skb">Skybox data</param>
        public SkyBoxNode(Node owner, SkyboxData skb)
            : base(owner)
        {
            skybox = skb;
            Visible = true;
            DontSave = false;
        }

        /// <summary>
        /// Creates skybox node from array of the texture names and specified scale.
        /// Loads textures to default renderer upon initializing
        /// </summary>
        /// <param name="owner">Owner node</param>
        /// <param name="textures">array of textures (see SkyboxData for format)</param>
        /// <param name="scale">Required scale (recommended set to visibility of scene)</param>
        public SkyBoxNode(Node owner, string[] textures, Vector3 scale) : base(owner)
        {
            skybox = new SkyboxData(textures, scale);
            skybox.LoadTextures();
            Visible = true;
            DontSave = false;
        }

        public override void WriteInternalData(System.Xml.XmlWriter writer)
        {
            base.WriteInternalData(writer);
            writer.WriteStartElement("SkyboxData");
            skybox.WriteXml(writer);
            writer.WriteEndElement();
        }

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

            SkyBoxNode mynode = targetNode as SkyBoxNode;
            if (mynode == null)
            {
                VisimBox.Console.DebugLine(FullName + " asked to do copy to different node type!");
                return;
            }
            if (skybox == null)
                return;

            mynode.skybox = new SkyboxData(skybox);
        }


        public override bool ReadInternalData(System.Xml.XmlReader reader)
        {
            if (base.ReadInternalData(reader))
                return true;
            switch (reader.Name)
            {
                case "SkyboxData":
                    skybox = new SkyboxData(reader);
                    skybox.LoadTextures();
                    break;
                default:
                    return false;
            }
            return true;
        }


        #region IRenderable Members

        public void OnRender(RendererEventArgs args)
        {
            args.RC.DrawSkybox(ref skybox);
        }

        public void RenderGeometry(RendererEventArgs args)
        {
            //do nothing
        }

        #endregion

        #region IRenderable Members

        public VisiMath.Geometry.IGeometry GetBoundingObject()
        {
            return null;
        }

        public Vector3 GetCenter()
        {
            return new Vector3();
        }

        #endregion

        public override void Dispose()
        {
            if (skybox != null)
            {
                skybox.Dispose();
                skybox = null;
            }
            base.Dispose();
        }
    }
}
