#include <Entity/PhysicsEntity/CameraEntity/Player/Player.hpp>
#include <Item/IEquipment.hpp>
#include <Widget/StaticItemSlot/ItemSlot/ItemSlot.hpp>
#include <Controller/InventoryController/InventoryController.hpp>

std::string ItemSlot::getShiftClickDestInv()
{
    if (invId.substr(0, 7) == "player.")
    {
        return "machine";
    }

    return "player.inventory";
}

glm::vec4 ItemSlot::getHighlight()
{
    if (invId == "player.trash")
    {
        return Color::error;
    }

    if (invId == "player.equipment")
    {
        if (ctrl->getMovingStack().getItemAs<IEquipment>() == nullptr)
        {
            return glm::vec4(0.0f);
        }

        return Color::equipmentHighlight;
    }

    ItemStack stack = ctrl->getStack(invId, m_slot);

    glm::vec4 color = ctrl->getSlotHighlight(invId, m_slot, stack);
    if (std::max(color.r, std::max(color.g, color.b)) > 1e-5)
    {
        return color;
    }

    if (stack.getItemAs<IEquipment>() != nullptr)
    {
        return Color::equipmentHighlight;
    }

    return color;
}

void ItemSlot::getExtraToolTipLines(ToolTip &tip)
{
    ItemStack stack = ctrl->getStack(invId, m_slot);
    return ctrl->getExtraToolTipLines(invId, m_slot, stack, tip);
}

ItemSlot::ItemSlot(InventoryController *inv, const std::string &invId, int slot) :
    StaticItemSlot([this]()
    {
        return this->ctrl->getStack(this->invId, m_slot);
    }),
    ctrl(inv),
    invId(invId),
    m_slot(slot)
{}

void ItemSlot::onMouseDown(Uint8 button)
{
    Widget::onMouseDown(button);

    if (isMouseInside())
    {
        switch (button)
        {
        case SDL_BUTTON_LEFT:
            Sound::itemClick.play();
            ctrl->onSlotLeftClick(invId, m_slot, getShiftClickDestInv());
            break;
        case SDL_BUTTON_RIGHT:
            Sound::itemClick.play();
            ctrl->onSlotRightClick(invId, m_slot);
            break;
        }
    }
}