#include <Block/SpriteBlock/CombustionGenerator/CombustionGenerator.hpp>
#include <Entity/PhysicsEntity/CameraEntity/Player/Player.hpp>
#include <Milestone/FossilFuelsMilestone/FossilFuelsMilestone.hpp>
#include <World/LocalWorld/LocalWorld.hpp>
#include <Controller/InventoryController/CombustionGeneratorController/CombustionGeneratorController.hpp>

std::vector<CombustionGenerator::Sprite> CombustionGenerator::makeSprites()
{
    std::vector<Sprite> sprites;

    for (uint8_t meta = 0; meta < numStates; meta++)
    {
        Side outputSide(meta & 3);
        Side frontSide = outputSide.opposite();
        const char *frontTexture = (meta & metaActive) ? "combustionGenerator/frontOn" : "combustionGenerator/front";
        const char *activeStateName = (meta & metaActive) ? "On" : "Off";

        Sprite sprite = {
            {
                "combustionGenerator/side",
                "combustionGenerator/side",
                "combustionGenerator/side",
                "combustionGenerator/side",
                "combustionGenerator/side",
                "combustionGenerator/top"
            },
            {{0, 0, 0, 8, 8, 8}},
            formatString("output$+$+", outputSide, activeStateName),
        };

        sprite.texturesOnSides[outputSide.asNumber()] = "combustionGenerator/output";
        sprite.texturesOnSides[frontSide.asNumber()] = frontTexture;

        sprites.push_back(sprite);
    }

    return sprites;
}

CombustionGenerator::CombustionGenerator(std::string name) :
    SpriteBlock(
        name,
        "Combustion generator",
        90.0f,
        "combustionGenerator/front",
        makeSprites()
    )
{
    withMetalSounds();
}

bool CombustionGenerator::canManuallyDig()
{
    return true;
}

void CombustionGenerator::onInteract(LocalWorld &world, Player &player, glm::ivec3 pos, Side side, const RGBABytes &collisionKey)
{
    if (player.isHoldingScrewdriver())
    {
        uint8_t meta = *world.getBlockMeta(pos);
        uint8_t newDir = (meta & 3) + 1;
        uint8_t newRest = meta & ~3;
        world.setBlockMeta(pos, (newDir & 3) | newRest, true);

        return;
    }

    player.achieve(FossilFuelsMilestone::ID, FossilFuelsMilestone::CombustionGenerator);
    player.connectController<CombustionGeneratorController>(
        "main:CombustionGeneratorController",
        world.getMachine(pos)
    );
}

void CombustionGenerator::getHarvestedStacks(LocalWorld &world, glm::ivec3 pos, std::vector<ItemStack> &stacks)
{
    SpriteBlock::getHarvestedStacks(world, pos, stacks);
    
    std::shared_ptr<IInventory> inv = std::dynamic_pointer_cast<IInventory>(world.getMachine(pos));
    if (inv != nullptr)
    {
        int i;
        for (i=0; i<inv->getNumSlots(); i++)
        {
            ItemStack stack = inv->getStackInSlot(i);
            if (!stack.isEmpty())
            {
                stacks.push_back(stack);
            }
        }
    }
}

glm::vec3 CombustionGenerator::getLightColor(uint8_t meta)
{
    if (meta & metaActive)
    {
        return glm::vec3(0.0f, 0.2f, 0.5f);
    }

    return glm::vec3(0.0f);
}

glm::ivec3 CombustionGenerator::getLightOffset(uint8_t meta)
{
    Side outputSide(meta & 3);
    Side frontSide = outputSide.opposite();

    return glm::ivec3(4) + 4 * frontSide.out();
}

Sound& CombustionGenerator::getBackgroundSound(uint8_t meta)
{
    if (meta & metaActive)
    {
        return Sound::combustion;
    }

    return Sound::NONE;
}

bool CombustionGenerator::canPlace(LocalWorld &world, IWorldActor &actor, glm::ivec3 pos, Side side, uint8_t &meta)
{
    meta = actor.getSideFacing2D().opposite().asNumber();
    return true;
}