using System;
using Microsoft.DirectX.Direct3D;
using MTV3D65;

namespace ComponentFramework.Modules
{
    /// <summary>
    /// A module with rendering support and render-order awareness.
    /// </summary>
    public abstract class Component : Module, IComparable<Component>
    {
        /// <summary>
        /// Raised when the <see cref"Order"/> property is changed
        /// </summary>
        internal event Action OrderChanged;

        /// <summary>
        /// Call this method when you change the <see cref"Order"/> property after the 
        /// <see cref"Component.Initialize()"/> method has been called.
        /// </summary>
        protected void OnOrderChanged()
        {
            OrderChanged();
        }

        /// <summary>
        /// Protected parameterized constructor.
        /// </summary>
        /// <param name="core">The core interface used to load this component.</param>
        protected Component(ICore core) : base(core) 
        {
            Enabled = true;
        }

        /// <summary>
        /// Performs pre-drawing operations. 
        /// This method is called before <see cref="TVEngine.Clear()"/>.
        /// </summary>
        public virtual void PreDraw() { }
        /// <summary>
        /// Performs drawing operations. 
        /// This method is called between <see cref="TVEngine.Clear()"/> and <see cref="TVEngine.RenderToScreen()"/>.
        /// </summary>
        public virtual void Draw() { }
        /// <summary>
        /// Performs post-drawing operations. 
        /// This method is called after <see cref="TVEngine.RenderToScreen()"/>.
        /// </summary>
        public virtual void PostDraw() { }

        /// <summary>
        /// The render (and update) order of this component among other components.
        /// Its value should not change during the component's lifetime.
        /// The default is 0.
        /// </summary>
        public int Order;

        /// <summary>
        /// Compares the current object with another object of the same type.
        /// </summary>
        /// <returns>
        /// A 32-bit signed integer that indicates the relative order of the objects being compared. 
        /// The return value has the following meanings: 
        /// Value Meaning Less than zero This object is less than the <paramref name="other" /> parameter.
        /// Zero This object is equal to <paramref name="other" />. 
        /// Greater than zero This object is greater than <paramref name="other" />. 
        /// </returns>
        /// <param name="other">An object to compare with this object.</param>
        public int CompareTo(Component other)
        {
            return Order - other.Order;
        }

        /// <summary>
        /// The Managed DirectX graphics device reference.
        /// </summary>
        protected Device Device { get; set; }

        /// <summary>
        /// Determines if this componenent will be updated and drawn.
        /// </summary>
        public bool Enabled { get; set; }
    }
}