using Encompass;
using KavTest.Components;
using KavTest.Messages;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace KavTest.Engines
{
    [Reads(typeof(PerspectiveCameraComponent))]
    [Sends(
        typeof(MoveCameraMessage),
        typeof(RotateCameraMessage)
    )]
    public class InputEngine : Engine
    {
        private Game Game { get; }

        public InputEngine(Game game)
        {
            Game = game;
        }

        public override void Update(double dt)
        {
            var keyboardState = Keyboard.GetState();
            var mouseState = Mouse.GetState();

            if (keyboardState.IsKeyDown(Keys.W))
            {
                SendMessage(new MoveCameraMessage(new Vector3(0, 0, -5)));
            }

            if (keyboardState.IsKeyDown(Keys.A))
            {
                SendMessage(new MoveCameraMessage(new Vector3(-5, 0, 0)));
            }

            if (keyboardState.IsKeyDown(Keys.S))
            {
                SendMessage(new MoveCameraMessage(new Vector3(0, 0, 5)));
            }

            if (keyboardState.IsKeyDown(Keys.D))
            {
                SendMessage(new MoveCameraMessage(new Vector3(5, 0, 0)));
            }

            if (Game.IsActive)
            {
                if (SomeComponent<PerspectiveCameraComponent>())
                {
                    var cameraEntity = ReadEntity<PerspectiveCameraComponent>();
                    SendMessage(new RotateCameraMessage(cameraEntity, -mouseState.X, -mouseState.Y));
                }
            }
        }
    }
}
