#pragma once

#include "Array.h"
#include "Critical.h"

struct ID3D10Blob;
struct ID3D11PixelShader;
struct ID3D11VertexShader;
struct ID3D11Device;
struct ID3D11DeviceContext;
struct D3D11_INPUT_ELEMENT_DESC;
struct ID3D11Buffer;
struct ID3D11InputLayout;

namespace Framework
{
    class Text;
    class DXBuffer;
    class DX12CopyCommandQueue;
    class DX12DirectCommandQueue;

    enum ShaderType
    {
        UNBEKANNT,
        VERTEX,
        PIXEL
    };

    //! A Shader class that manages all constant buffers of a shader
    class Shader : public virtual ReferenceCounter
    {
    protected:
        ShaderType type;
        RCArray<DXBuffer>* constBuffers;

    public:
        //! Constructor
        DLLEXPORT Shader();
        //! Destructor
        DLLEXPORT virtual ~Shader();
        //! Sets the compiled shader
        //!  zD3d11Device: The device with which the shader should be created
        //! \param bytes The bytes of the compiled code
        //! \param length The length of the byte array
        //! \return true if bytes is valid, false otherwise
        virtual bool setCompiledByteArray(unsigned char* bytes, int length) = 0;
        //! After calling this function, this shader is used as a pixel shader
        //!  zD3d11Context: The context object with which the shader should
        //!  be used
        virtual void useShader() = 0;
        //! Creates a constant buffer that passes constant data to the shader.
        //! A maximum of 14 buffers can be created.
        //!  zD3d11Device: The device with which the buffer should be created
        //! \param size The size of the buffer in bytes
        //! \param index The position of the buffer in the buffer array. An
        //! existing buffer will be replaced. Buffer 1 cannot be created
        //! if buffer 0 has not been created yet, etc.
        virtual bool createConstBuffer(int size, int index) = 0;
        //! Deletes a constant buffer
        //! \param index The index of the buffer to delete. Buffer 0 cannot
        //! be deleted while buffer 1 still exists, etc.
        DLLEXPORT bool removeConstBuffer(int index);
        //! Copies data into a constant buffer
        //!  zD3d11Context: The context object to use for copying
        //! \param data A pointer to a byte array of the buffer's size
        //! \param index The index of the buffer
        //! \param len The length of the data in bytes (-1 for the maximum
        //! buffer size)
        DLLEXPORT bool fillConstBuffer(char* data, int index, int len = -1);
        //! Returns the length of a constant buffer
        //! \param index The index of the buffer
        DLLEXPORT int getConstBufferLaenge(int index) const;
        //! Returns the shader type
        DLLEXPORT ShaderType getType() const;
        //! Returns the index of the first uninitialized buffer
        DLLEXPORT int getFirstUninitializedBufferIndex() const;
    };

    class DX11Shader : public Shader
    {
    protected:
        ID3D11Device* device;
        ID3D11DeviceContext* context;
        Critical& deviceLock;

    public:
        DLLEXPORT DX11Shader(ID3D11Device* device,
            ID3D11DeviceContext* context,
            Critical& deviceLock);
        DLLEXPORT virtual ~DX11Shader();
        //! Creates a constant buffer that passes constant data to the shader.
        //! A maximum of 14 buffers can be created.
        //!  zD3d11Device: The device with which the buffer should be created
        //! \param size The size of the buffer in bytes
        //! \param index The position of the buffer in the buffer array. An
        //! existing buffer will be replaced. Buffer 1 cannot be created
        //! if buffer 0 has not been created yet, etc.
        DLLEXPORT virtual bool createConstBuffer(int size, int index) override;
    };

    //! Manages a pixel shader
    class DX11PixelShader : public DX11Shader
    {
    private:
        ID3D11PixelShader* pixelShader;

    public:
        //! Constructor
        DLLEXPORT DX11PixelShader(ID3D11Device* device,
            ID3D11DeviceContext* context,
            Critical& deviceLock);
        //! Destructor
        DLLEXPORT ~DX11PixelShader();
        //! Sets the compiled shader
        //!  zD3d11Device: The device with which the shader should be created
        //! \param bytes The bytes of the compiled code
        //! \param length The length of the byte array
        //! \return true if bytes is valid, false otherwise
        DLLEXPORT bool setCompiledByteArray(
            unsigned char* bytes, int length) override;
        //! After calling this function, this shader is used as a pixel shader
        //!  zD3d11Context: The context object with which the shader should
        //!  be used
        DLLEXPORT void useShader() override;
    };

    //! Manages a vertex shader
    class DX11VertexShader : public DX11Shader
    {
    private:
        ID3D11VertexShader* vertexShader;
        ID3D11InputLayout* inputLayout;
        unsigned char* shaderByteBuffer;
        int byteBufferSize;

    public:
        //! Constructor
        DLLEXPORT DX11VertexShader(ID3D11Device* device,
            ID3D11DeviceContext* context,
            Critical& deviceLock);
        //! Destructor
        DLLEXPORT ~DX11VertexShader();
        //! Sets the compiled shader
        //!  zD3d11Device: The device with which the shader should be created
        //! \param bytes The bytes of the compiled code
        //! \param length The length of the byte array
        //! \return true if bytes is valid, false otherwise
        DLLEXPORT bool setCompiledByteArray(
            unsigned char* bytes, int length) override;
        //! Creates an input layout for the shader.
        //! May only be called after compile.
        //!  zD3d11Device: The device with which the layout should be created
        //! \param descArray An array with initialization data
        //! \param anz The number of elements in the array
        DLLEXPORT bool createInputLayout(
            D3D11_INPUT_ELEMENT_DESC* descArray, int anz);
        //! After calling this function, this shader is used as a vertex shader
        //!  zD3d11Context: The context object with which the shader should
        //!  be used
        DLLEXPORT void useShader() override;
    };
} // namespace Framework