#include <Color/Color.hpp>
#include <Item/StatefulItem/MemoryChip/MemoryChip.hpp>
#include <ChunkWindow/ChunkWindow.hpp>
#include <Display/WidgetDisplay/WorldDisplay/WorldDisplay.hpp>
#include <Material/Material.hpp>

MemoryChip::MemoryChip(std::string name) : StatefulItem(name)
{
    setMaxStackSize(1);
}

void MemoryChip::save(ItemStack &stack, const MachineConfig &config, const RGBABytes &color)
{
    MemoryChipState &state = this->state(stack);
    state.config = config;
    state.color = color;
}

const MachineConfig& MemoryChip::getConfig(ItemStack &stack)
{
    return state(stack).config;
}

void MemoryChip::draw(GraphicsContext &context, int x, int y, const ItemStack &stack)
{
    context.color = glm::vec4(1.0f);
    context.drawMaterial(Material::memoryChip, x, y);

    const MemoryChipState &state = this->state(stack);
    if (state.config.blockName.empty())
    {
        return;
    }

    context.color = glm::vec4(
        state.color.channels[0] / 255.0f,
        state.color.channels[1] / 255.0f,
        state.color.channels[2] / 255.0f,
        1.0f
    );
    context.drawMaterial(Material::memoryChipColorLabel, x, y);
}

std::string MemoryChip::getDisplayName(const ItemStack &stack)
{
    return "Memory Chip";
}

void MemoryChip::draw3D(GraphicsContext &context, WorldDisplay &worldDisplay, glm::vec3 pos, ItemStack &stack)
{
    worldDisplay.drawMeshedIcon(context, Material::memoryChip, pos);
}

void MemoryChip::drawFlat(GraphicsContext &context, WorldDisplay &worldDisplay, glm::mat4 matrix, ItemStack &stack)
{
    worldDisplay.drawMeshedIconFlat(context, Material::memoryChip, matrix);
}

void MemoryChip::getExtraToolTipLines(const ItemStack &stack, ToolTip &tip)
{
    auto &state = this->state(stack);
    if (state.config.blockName.empty())
    {
        tip.addLine(Color::faded, "Empty");
    }
    else
    {
        tip.addLine(Color::actionEmphasis, state.config.label);
    }
}