using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using RecrownedAthenaeum.Input;
using RecrownedAthenaeum.Render;
using System;

namespace RecrownedAthenaeum.UI.Modular
{

    /// <summary>
    /// Module for UI layout.
    /// </summary>
    public abstract class UIModule : IInputListener
    {
        /// <summary>
        /// The width of the module.
        /// </summary>
        public virtual int Width { get; set; }

        /// <summary>
        /// The height of the module.
        /// </summary>
        public virtual int Height { get; set; }

        /// <summary>
        /// The X position of the module.
        /// </summary>
        public virtual int X { get; set; }

        /// <summary>
        /// The Y position of the module.
        /// </summary>
        public virtual int Y { get; set; }

        /// <summary>
        /// Bounds of this module (after factoring in the origin).
        /// </summary>
        public Rectangle Boundaries
        {
            get
            {
                return new Rectangle((int)(X - origin.X), (int)(Y - origin.Y), Width, Height);
            }
        }

        /// <summary>
        /// Origin of this module.
        /// </summary>
        public Vector2 origin;

        /// <summary>
        /// The parent of this module. May be null.
        /// </summary>
        public UIModuleGroup parent;

        /// <summary>
        /// Name of this module. For organizational/referencial purposes mostly.
        /// </summary>
        public string name;

        /// <summary>
        /// The color tint of this module.
        /// </summary>
        public Color color = Color.White;

        /// <summary>
        /// Called every frame to update this module. Calculations and movement should go here.
        /// </summary>
        /// <param name="gameTime">Game time information.</param>
        public virtual void Update(GameTime gameTime)
        {

        }

        /// <summary>
        /// Called every frame to draw this module. Anything that needs to be drawn should go here.
        /// </summary>
        /// <param name="batch">Batch used to draw.</param>
        public virtual void Draw(ConsistentSpriteBatch batch)
        {
        }

        /// <summary>
        /// Converts the given rectangle to the coordinates of the parent.
        /// </summary>
        /// <param name="rectangle">Rectangle to convert.</param>
        /// <returns></returns>
        public Rectangle ConvertToParentCoordinates(Rectangle rectangle)
        {
            if (parent != null)
            {
                Rectangle parentHitbox = parent.ConvertToParentCoordinates(rectangle);
                int tX = rectangle.X + parentHitbox.X;
                int tY = rectangle.Y + parentHitbox.Y;
                return new Rectangle(tX, tY, rectangle.Width, rectangle.Height);
            }
            else
            {
                return rectangle;
            }
        }

        /// <summary>
        /// Removes this module from the parent.
        /// </summary>
        public void RemoveFromParent()
        {
            if (parent == null) throw new InvalidOperationException("Parent is null.");
            parent.RemoveModule(this);
        }

        /// <summary>
        /// Called whenever the keyboard state is changed.
        /// </summary>
        /// <param name="state">The current keyboard state.</param>
        /// <returns>Returning whether or not to continue to call the next listener.</returns>
        public virtual bool KeyboardStateChanged(KeyboardState state)
        {
            return true;
        }

        /// <summary>
        /// Called whenever the state of the mouse changes. This includes movement.
        /// </summary>
        /// <param name="state">The current state of the mouse.</param>
        /// <returns>Returning whether or not to continue to call the next listener.</returns>
        public virtual bool MouseStateChanged(MouseState state)
        {
            return true;
        }

        /// <summary>
        /// Sets the origin to be the center using the <see cref="Width"/> and <see cref="Height"/>. 
        /// </summary>
        public virtual void CenterOrigin()
        {
            origin.X = Width / 2f;
            origin.Y = Height / 2f;
        }

        /// <summary>
        /// Centers this module's on the horizontal axis relative to the parent <see cref="UIModuleGroup"/>.
        /// </summary>
        /// <returns>True if possible and false if not.</returns>
        public bool CenterHorizontally()
        {
            if (parent != null)
            {
                Rectangle rectangle = parent.Boundaries;
                if (parent != null && rectangle.Width >= Boundaries.Width)
                {
                    X = rectangle.Width / 2 + X;
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// Centers this module's origin on the vertical axis relative to the parent <see cref="UIModuleGroup"/>.
        /// </summary>
        /// <returns>True if possible.</returns>
        public virtual bool CenterVertically()
        {
            if (parent != null)
            {
                Rectangle rectangle = parent.Boundaries;
                if (rectangle.Height >= Boundaries.Height)
                {
                    Y = rectangle.Height / 2 + Y;
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// Sets the position and dimension of this module.
        /// </summary>
        /// <param name="rectangle">The rectangle that represents the position and dimensions of the module.</param>
        public virtual void SetPositionAndDimensions(Rectangle rectangle)
        {
            X = rectangle.X;
            Y = rectangle.Y;
            Width = rectangle.Width;
            Height = rectangle.Height;
        }
    }
}
