#include "CameraManipulator.h"
#include "../../Core/AppConfig.h"

float CameraManipulator::_mouseSensitivity = 1.0f;
float CameraManipulator::_mouseSmoothingInterval = 30.0f;
int   CameraManipulator::_keyBindings[256];

CameraManipulator::CameraManipulator(Camera* camera)
: _camera(camera), _speed(0.02f), _active(false)
{
    _eye = Vector3(0.0f, 0.0f, 0.0f);
    _center = Vector3(0.0f, 0.0f, 1.0f);

    memset(&_keyBindings[0], 0, 256);

    try {
        _mouseSensitivity = AppConfig::instance().get<float>(L"Input", L"MouseSensitivity");
        _mouseSmoothingInterval = AppConfig::instance().get<float>(L"Input", L"MouseSmoothingInterval");

        int moveForward = AppConfig::instance().get<int>(L"Input", L"MoveForward");
        int moveBackward = AppConfig::instance().get<int>(L"Input", L"MoveBackward");
        int moveLeft = AppConfig::instance().get<int>(L"Input", L"MoveLeft");
        int moveRight = AppConfig::instance().get<int>(L"Input", L"MoveRight");

        _keyBindings[moveForward] = MoveForward;
        _keyBindings[moveBackward] = MoveBackward;
        _keyBindings[moveLeft] = MoveLeft;
        _keyBindings[moveRight] = MoveRight;

    }
    catch(AppConfig::InvalidEntryException&) {
        AppConfig::instance().set<float>(L"Input", L"MouseSensitivity", _mouseSensitivity);
        AppConfig::instance().set<float>(L"Input", L"MouseSmoothingInterval", _mouseSmoothingInterval);

        int moveForward = 87; 
        int moveBackward = 83;
        int moveLeft = 65; 
        int moveRight = 68;

        _keyBindings[moveForward] = MoveForward;
        _keyBindings[moveBackward] = MoveBackward;
        _keyBindings[moveLeft] = MoveLeft;
        _keyBindings[moveRight] = MoveRight;

        AppConfig::instance().set<int>(L"Input", L"MoveForward", moveForward);
        AppConfig::instance().set<int>(L"Input", L"MoveBackward", moveBackward);
        AppConfig::instance().set<int>(L"Input", L"MoveLeft", moveLeft);
        AppConfig::instance().set<int>(L"Input", L"MoveRight", moveRight);
    }
}


Camera* CameraManipulator::getCamera()
{
    return _camera;
}

void CameraManipulator::setCamera(Camera* camera)
{
    _camera = camera;
}