#include <MapTool/MapPanTool/MapPanTool.hpp>

MapPanTool MapPanTool::instance;

Material& MapPanTool::getIcon()
{
    return Material::mapPanTool;
}

std::string MapPanTool::getName()
{
    return "Pan";
}

void MapPanTool::onMouseDown(MapView &mapView, Uint8 button, glm::ivec2 pos)
{
    if (button == SDL_BUTTON_LEFT)
    {
        mouseAnchor = pos;
        cameraAnchor = glm::ivec2(mapView.cameraPos);
        dragging = true;
        return;
    }
}

void MapPanTool::onMouseUp(MapView &mapView, Uint8 button, glm::ivec2 pos)
{
    if (button == SDL_BUTTON_LEFT)
    {
        dragging = false;
    }
}

void MapPanTool::onMouseMoved(MapView &mapView, glm::ivec2 pos)
{
    if (!dragging)
    {
        return;
    }

    glm::ivec2 delta = mouseAnchor - pos;
    delta.y = -delta.y;
    glm::ivec2 newCamera = cameraAnchor + delta;

    mapView.cameraPos.x = newCamera.x;
    mapView.cameraPos.y = newCamera.y;
}

IEquipment::MapLevel MapPanTool::getRequiredMapLevel()
{
    return IEquipment::MapLevel::CarvedMap;
}