#include <Item/IEquipment.hpp>
#include <MapTool/MapPanTool/MapPanTool.hpp>
#include <MapTool/MapPinTool/MapPinTool.hpp>
#include <Widget/Button/Button.hpp>
#include <Widget/IconLabel/IconLabel.hpp>
#include <Widget/Label/Label.hpp>
#include <Widget/Layout/Center/Center.hpp>
#include <Widget/Layout/CompoundWidget/Deck/Deck.hpp>
#include <Widget/Layout/Margin/Margin.hpp>
#include <Widget/Layout/Row/Row.hpp>
#include <Widget/Panel/Panel.hpp>
#include <Widget/TextField/TextField.hpp>

MapPinTool MapPinTool::instance;

Material& MapPinTool::getIcon()
{
    return Material::mapPin;
}

std::string MapPinTool::getName()
{
    return "Place pin";
}

void MapPinTool::onMouseUp(MapView &mapView, Uint8 button, glm::ivec2 pos)
{
    if (button == SDL_BUTTON_LEFT)
    {
        glm::ivec2 mapPos = mapView.getHoveredMapPos();

        static std::string label; // TODO
        label = "";

        auto onAccept = [&mapView, mapPos]()
        {
            Widget::closePopup();
            mapView.placePin(label, mapPos);
        };

        mapView.openPopupAtCursor(
            glm::ivec2(-7, -8),
            new Row(
                new Margin<0, 2>(
                    new Center(new IconLabel(Material::mapPin))
                ),
                (Widget::focused = new TextField(label, onAccept)),
                (new Button("", onAccept))
                    ->withFaceColor(Color::success)
                    ->withToolTip("Create pin")
                    ->withIcon(Material::accept),
                (new Button("", [](){ Widget::closePopup(); }))
                    ->withFaceColor(Color::danger)
                    ->withToolTip("Cancel")
                    ->withIcon(Material::trash)
            )
        );
    }
}

IEquipment::MapLevel MapPinTool::getRequiredMapLevel()
{
    return IEquipment::MapLevel::PaperMap;
}