#include "CustomDX11API.h"

#include <Shader.h>
#include <DXBuffer.h>
#include <d3d11.h>

#include "CustomUIDX11PixelShader.h"
#include "CustomUIDX11VertexShader.h"

using namespace Framework;

CustomDX11API::CustomDX11API()
    : DirectX11(),
      vertexShaderLightBufferIndex(-1)
{}

Framework::DX11VertexShader* CustomDX11API::initializeVertexShader(
    unsigned char* byteCode, int size)
{
    D3D11_RASTERIZER_DESC rasterDesc;
    ZeroMemory(&rasterDesc, sizeof(rasterDesc));
    rasterDesc.AntialiasedLineEnable = false;
    rasterDesc.CullMode = D3D11_CULL_NONE;
    rasterDesc.DepthBiasClamp = 0.0f;
    rasterDesc.DepthClipEnable = true;
    rasterDesc.FillMode = D3D11_FILL_SOLID;
    rasterDesc.FrontCounterClockwise = false;
    rasterDesc.MultisampleEnable = false;
    rasterDesc.ScissorEnable = false;
    rasterDesc.SlopeScaledDepthBias = 0.0f;
    ID3D11Device* device;
    zContext()->GetDevice(&device);
    device->CreateRasterizerState(&rasterDesc, &noCull);

    DX11VertexShader* shader = DirectX11::initializeVertexShader(
        (unsigned char*)CustomUIDX11VertexShader,
        sizeof(CustomUIDX11VertexShader));
    vertexShaderLightBufferIndex = shader->getFirstUninitializedBufferIndex();
    shader->erstelleConstBuffer(20, vertexShaderLightBufferIndex);
    char buffer[8];
    *(int*)buffer = 0;
    *(float*)(buffer + 4) = 1.f;
    shader->füllConstBuffer(buffer, vertexShaderLightBufferIndex, 8);
    return shader;
}

Framework::DX11PixelShader* CustomDX11API::initializePixelShader(
    unsigned char* byteCode, int size)
{
    DX11PixelShader* shader = DirectX11::initializePixelShader(
        (unsigned char*)CustomUIDX11PixelShader,
        sizeof(CustomUIDX11PixelShader));
    return shader;
}

void CustomDX11API::setVertexLightBuffer(Framework::DXBuffer* zBuffer)
{
    DX11StructuredBuffer* buffer = dynamic_cast<DX11StructuredBuffer*>(zBuffer);
    if (buffer)
    {
        ID3D11ShaderResourceView* v[1];
        v[0] = *buffer;
        zContext()->VSSetShaderResources(0, 1, v);
    }
}

int CustomDX11API::getVertexShaderLightBufferIndex() const
{
    return vertexShaderLightBufferIndex;
}

void CustomDX11API::setCullBack(bool cull)
{
    zContext()->RSSetState(cull ? texturRS : noCull);
}