﻿// Gui.cs

#region Using Statements

using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Sandbox.Events;

#endregion

namespace Sandbox
{
    public class Gui : IGameObject, IKeyboardListener, IMouseListener
    {
        private List<Control> m_Controls;
        private Control m_FocusedControl;

        public List<Control> Controls
        {
            get { return m_Controls; }
            set { m_Controls = value; }
        }

        public Gui()
        {
            m_Controls = new List<Control>();
        }

        public void SetFocus(Control p_Control)
        {
            if (m_Controls.Contains(p_Control))
            {
                AssignFocusTo(p_Control);
            }
        }

        public void Update(GameTime p_GameTime)
        {
            foreach (Control control in m_Controls)
            {
                control.Update(p_GameTime);
            }
        }

        public void Draw(Renderer p_Renderer)
        {
            foreach (Control control in m_Controls)
            {
                control.Draw(p_Renderer);
            }
        }

        public void OnKeyPressed(KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Keys.Tab:
                    NextFocus();
                    break;
            }

            foreach (Control control in m_Controls)
            {
                control.OnKeyPressed(e);
            }
        }

        public void OnKeyReleased(KeyEventArgs e)
        {
            foreach (Control control in m_Controls)
            {
                control.OnKeyReleased(e);
            }
        }

        public void OnMouseMoved(MouseMoveEventArgs e)
        {
            foreach (Control control in m_Controls)
            {
                control.OnMouseMoved(e);
            }
        }

        public void OnMousePressed(MouseStateEventArgs e)
        {
            foreach (Control control in m_Controls)
            {
                control.OnMousePressed(e);
            }
        }

        public void OnMouseReleased(MouseStateEventArgs e)
        {
            foreach (Control control in m_Controls)
            {
                control.OnMouseReleased(e);
            }
        }

        public void OnMouseClicked(MouseClickEventArgs e)
        {
            foreach (Control control in m_Controls)
            {
                control.OnMouseClicked(e);
            }
        }

        public void OnMouseWheelScrolled(MouseWheelEventArgs e)
        {
            foreach (Control control in m_Controls)
            {
                control.OnMouseWheelScrolled(e);
            }
        }

        private void AssignFocusTo(Control p_Control)
        {
            if (m_FocusedControl != null)
            {
                m_FocusedControl.HasFocus = false;
            }

            m_FocusedControl = p_Control;
            m_FocusedControl.HasFocus = true;
        }

        private void AssignFocusTo(int p_Index)
        {
            if (p_Index >= 0 && p_Index < m_Controls.Count)
            {
                AssignFocusTo(m_Controls[p_Index]);
            }
        }

        private void NextFocus()
        {
            if (m_FocusedControl != null)
            {
                int index = m_Controls.IndexOf(m_FocusedControl);

                AssignFocusTo((index + 1) % m_Controls.Count);
            }
            else
            {
                AssignFocusTo(0);
            }
        }
    }
}
