﻿using ShellInput;
using ShellUi.Systems;
using ShellUtil;

namespace EnergyShell.Input
{
    /// <summary>
    /// Routes input events into various input systems routing system.
    /// </summary>
    public sealed class ShellUiInputEventToInputRouting : IInputEventObserver
    {
        /// <summary>
        /// The mouse routing system which will recieve the input events.
        /// </summary>
        private readonly IMouseRoutingSystem mouseRouting;

        /// <summary>
        /// Initializes a new instance of the <see cref="ShellUiInputEventToInputRouting"/> class.
        /// </summary>
        /// <param name="mouseRouting">The mouse routing system.</param>
        public ShellUiInputEventToInputRouting(IMouseRoutingSystem mouseRouting)
        {
            this.mouseRouting = mouseRouting;
        }

        /// <summary>
        /// Notifies the keyboard handlers that a key was pressed.
        /// </summary>
        /// <param name="key">The key which was pressed.</param>
        public void NotifyKeyboardKeyPressed(Key key)
        {
            // TODO implement
        }

        /// <summary>
        /// Notifies the keyboard handlers that a key was released.
        /// </summary>
        /// <param name="key">The key which was released.</param>
        public void NotifyKeyboardKeyReleased(Key key)
        {
            // TODO implement
        }

        /// <summary>
        /// Notifies the handlers that a mouse button was pressed.
        /// </summary>
        /// <param name="button">The mouse button.</param>
        /// <param name="x">The new mouse x axis position.</param>
        /// <param name="y">The new mouse y axis position.</param>
        public void NotifyMouseButtonPressed(MouseButton button, int x, int y)
        {
            Point2 newMousePosition = new Point2(x, y);
            this.mouseRouting.MouseEventRouterComponent.NotifyMouseButtonPressed(
                newMousePosition - this.mouseRouting.DimensionComponent.ClientPosition, button);
        }

        /// <summary>
        /// Notifies the handlers that a mouse button was released.
        /// </summary>
        /// <param name="button">The mouse button.</param>
        /// <param name="x">The new mouse x axis position.</param>
        /// <param name="y">The new mouse y axis position.</param>
        public void NotifyMouseButtonReleased(MouseButton button, int x, int y)
        {
            Point2 newMousePosition = new Point2(x, y);
            this.mouseRouting.MouseEventRouterComponent.NotifyMouseButtonReleased(
                newMousePosition - this.mouseRouting.DimensionComponent.ClientPosition, button);
        }

        /// <summary>
        /// Notifies the handlers that a mouse axis was changed.
        /// </summary>
        /// <param name="axis">The mouse axis type for this notification.</param>
        /// <param name="x">The new mouse x axis position.</param>
        /// <param name="y">The new mouse y axis position.</param>
        public void NotifyMouseAxis(MouseAxis axis, int x, int y)
        {
            if (axis == MouseAxis.ClientPosition)
            {
                Point2 newMousePosition = new Point2(x, y);
                this.mouseRouting.MouseEventRouterComponent.NotifyMouseMoved(
                    newMousePosition - this.mouseRouting.DimensionComponent.ClientPosition);
            }
        }
    }
}
