#include "FlyCamSystem.h"
#include <iostream>

FlyCamSystem::FlyCamSystem(PScene scene)
    : System::ComponentSystem<Component::KeyboardInput, Component::Camera, Component::Transform>(scene) {}

FlyCamSystem::~FlyCamSystem() {}

void FlyCamSystem::update(Component::KeyboardInput& input, Component::Camera& camera, Component::Transform& transform) {
    float cameraMove = static_cast<float>(deltaTime) * 4;
    if(input.keys[KeyCode::KEY_LEFT_SHIFT])
    {
        cameraMove *= 10;
    }
    Vector forward = transform.getForward();
    Vector side = transform.getRight();
    Vector moveVector = Vector();

    if (input.keys[KeyCode::KEY_J])
    {
        std::cout << transform.getPosition() << std::endl;
    }

    if(input.keys[KeyCode::KEY_W])
    {
        moveVector += forward * cameraMove;
    }
    if(input.keys[KeyCode::KEY_S])
    {
        moveVector += forward * -cameraMove;
    }
    if(input.keys[KeyCode::KEY_A])
    {
        moveVector += side * cameraMove;
    }
    if(input.keys[KeyCode::KEY_D])
    {
        moveVector += side * -cameraMove;
    }
    if(input.keys[KeyCode::KEY_E])
    {
        moveVector += Vector(0, cameraMove, 0);
    }      
    if(input.keys[KeyCode::KEY_Q])
    {
        moveVector += Vector(0, -cameraMove, 0);
    }
    transform.translate(moveVector);
    if(input.mouse2)
    {
        transform.setRotation(glm::rotate(transform.getRotation(), input.deltaX / 500, Vector(0, 1, 0)));
        transform.setRotation(glm::rotate(transform.getRotation(), -input.deltaY / 500, transform.getRight()));
    }
}