#include "FactoryCraftModel.h"

#include <Shader.h>

#include "CustomDX11API.h"
#include "Globals.h"
#include "World.h"

FactoryCraftModel::FactoryCraftModel()
    : Model3D()
{
    vertexLightBuffer = 0;
    lightBuffer = 0;
    vertexLightBufferCount = 0;
    visible = 1;
    destroyedState = 0;
    breakTextur
        = uiFactory.initParam.bildschirm->zGraphicsApi()->createOrGetTextur(
            "blocks.ltdb/crack.png", 0);
    effectAlpha = 0;
}

FactoryCraftModel::~FactoryCraftModel()
{
    breakTextur->release();
    if (vertexLightBuffer) vertexLightBuffer->release();
    delete[] lightBuffer;
}

struct TexturEffect
{
    bool enabled;
    float percentage;
};


void FactoryCraftModel::beforeRender(Framework::GraphicsApi* api,
    Framework::Shader* zVertexShader,
    Framework::Shader* zPixelShader)
{
    CustomDX11API* cApi = dynamic_cast<CustomDX11API*>(api);
    if (cApi)
    {
        char buffer[8];
        *(int*)buffer
            = vertexLightBufferCount == 0 ? 1 : vertexLightBufferCount;
        *(float*)(buffer + 4) = World::INSTANCE->getDayLightFactor();
        zVertexShader->füllConstBuffer(
            buffer, cApi->getVertexShaderLightBufferIndex(), 8);
        if (vertexLightBuffer)
        {
            cApi->setVertexLightBuffer(vertexLightBuffer);
        }
        else
        {
            cApi->setVertexLightBuffer(
                World::INSTANCE->zFallbackVertexLightBuffer());
        }
        if (destroyedState > 0 && effectAlpha)
        {
            TexturEffect e = {1, 1.f + destroyedState};
            zPixelShader->füllConstBuffer(
                (char*)&e, 3, sizeof(TexturEffect));
        }
    }
}

void FactoryCraftModel::afterRender(Framework::GraphicsApi* api,
    Framework::Shader* zVertexShader,
    Framework::Shader* zPixelShader)
{
    CustomDX11API* cApi = dynamic_cast<CustomDX11API*>(api);
    if (cApi)
    {
        char buffer[8];
        *(int*)buffer = 0;
        *(float*)(buffer + 4) = World::INSTANCE->getDayLightFactor();
        zVertexShader->füllConstBuffer(
            buffer, cApi->getVertexShaderLightBufferIndex(), 8);
    }
}

void FactoryCraftModel::setVertexLightBuffer(__int64* data, int vertexCount)
{
    if (!vertexLightBuffer)
    {
        vertexLightBuffer = uiFactory.initParam.bildschirm->zGraphicsApi()
                                ->createStructuredBuffer(16);
    }
    if (lightBuffer)
    {
        delete[] lightBuffer;
    }
    lightBuffer = data;
    vertexLightBufferCount = vertexCount;
    vertexLightBuffer->setData(data);
    vertexLightBuffer->setLength(vertexCount * 8);
    vertexLightBuffer->copieren();
}

__int64* FactoryCraftModel::zLightBuffer()
{
    if (vertexLightBuffer)
    {
        vertexLightBuffer->setChanged();
    }
    return lightBuffer;
}

void FactoryCraftModel::copyLightToGPU()
{
    if (vertexLightBuffer)
    {
        vertexLightBuffer->copieren();
    }
}

void FactoryCraftModel::setVisible(bool visible)
{
    this->visible = visible;
}

void FactoryCraftModel::setDestroyedState(float percentage)
{
    destroyedState = percentage;
}

void FactoryCraftModel::setUseEffectAlpha(bool useAlpha)
{
    effectAlpha = useAlpha;
}

bool FactoryCraftModel::needRenderPolygon(int index)
{
    return visible;
}

Textur* FactoryCraftModel::zEffectTextur()
{
    return destroyedState > 0 ? breakTextur : 0;
}

float FactoryCraftModel::getEffectPercentage()
{
    return destroyedState;
}