#include "../H/chunk.h"
#include <climits>

Chunk::Chunk(): m_blocks(Array3d<BlockType>(CHUNK_SIZE_X, CHUNK_SIZE_Y, CHUNK_SIZE_Z)), m_isDirty(true){}

Chunk::Chunk(float x, float z) : m_blocks(Array3d<BlockType>(CHUNK_SIZE_X +1, CHUNK_SIZE_Y, CHUNK_SIZE_Z +1)), m_isDirty(true), m_posx(x), m_posz(z)
{
    Perlin perlin(16, 6, 1, 95);
    m_blocks.Reset(BTYPE_AIR);
    for(int x = 0; x < CHUNK_SIZE_X; ++x)
    {
        for(int z = 0; z < CHUNK_SIZE_Z; ++z)
        {
            float val = perlin.Get((float)(m_posx + x) / 2000.f, (float)(m_posz + z) / 2000.f);
            
            if (val < 0)
                val *= -1;
            val *= CHUNK_SIZE_Y;
            val *= 0.4;

            for (int i = 0; i < val; i++)
            {
                SetBlock(x , i, z, (rand()%(BTYPE_LAST - 1)+1));
            }
        }
    }
}

Chunk::~Chunk()
{
}

void Chunk::RemoveBlock(int x, int y, int z)
{
    m_blocks.Set(x, y, z, BTYPE_AIR);
    m_isDirty = true;
}

void Chunk::SetBlock(int x, int y, int z, BlockType type)
{
    m_blocks.Set(x, y, z, type);
    m_isDirty = true;
}

BlockType Chunk::GetBlock(int x, int y, int z)
{
    return m_blocks.Get(x, y, z);
}

void Chunk::Update(BlockInfo bi[BTYPE_LAST]){
    bool front;
    bool back;
    bool top;
    bool bottom;
    bool left;
    bool right;
    // Update mesh
    if (m_isDirty)
    {
        int maxVertexCount = (CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z) * (6 * 4);
        VertexBuffer::VertexData * vd = new VertexBuffer::VertexData[maxVertexCount];
        int count = 0;
        for (int x = 0; x < CHUNK_SIZE_X; ++x)
        {
            for (int z = 0; z < CHUNK_SIZE_Z; ++z)
            {
                for (int y = 0; y < CHUNK_SIZE_Y; ++y)
                {
                    if (count > USHRT_MAX)
                        break;
                    BlockType bt = GetBlock(x ,y ,z);
                    
                    if ( bt != BTYPE_AIR )
                    {
                        AddBlockToMesh(vd, count, bt, x, y, z, bi);
                    }
                }
            }
        }
        if (count > USHRT_MAX)
        {
            count = USHRT_MAX;
            std::cout << "[ Chunk :: Update ] Chunk data truncaned, too much vertices to have a 16 bit index" << std::endl;
        }
        m_vertexBuffer.SetMeshData(vd, count);
        delete[] vd;
    }
    m_isDirty = false ;
}

void Chunk::AddBlockToMesh(VertexBuffer::VertexData* vd, int& count, BlockType bt, int x, int y, int z, BlockInfo bi[BTYPE_LAST])
{
    BlockInfo biCourant = bi[bt];
    float u = biCourant.GetU();
    float v = biCourant.GetV();
    float w = biCourant.GetW();
    float h = biCourant.GetH();

    //front
    if(GetBlock(x,y,z+1) == BTYPE_AIR){
        vd[count++] = VertexBuffer::VertexData(m_posx + x - .5f, y - .5f, m_posz + z + .5f, 1.f, 1.f, 1.f, u, v);
        vd[count++] = VertexBuffer::VertexData(m_posx + x + .5f, y - .5f, m_posz + z + .5f, 1.f, 1.f, 1.f, u + w, v);
        vd[count++] = VertexBuffer::VertexData(m_posx + x + .5f, y + .5f, m_posz + z + .5f, 1.f, 1.f, 1.f, u + w, v + h);
        vd[count++] = VertexBuffer::VertexData(m_posx + x - .5f, y + .5f, m_posz + z + .5f, 1.f, 1.f, 1.f, u, v + h);
    }

    //back
    if(GetBlock(x,y,z-1) == BTYPE_AIR){
        vd[count++] = VertexBuffer::VertexData(m_posx + x - .5f, y - .5f, m_posz + z - .5f, 1.f, 1.f, 1.f, u, v);
        vd[count++] = VertexBuffer::VertexData(m_posx + x - .5f, y + .5f, m_posz + z - .5f, 1.f, 1.f, 1.f, u + w, v);
        vd[count++] = VertexBuffer::VertexData(m_posx + x + .5f, y + .5f, m_posz + z - .5f, 1.f, 1.f, 1.f, u + w, v + h);
        vd[count++] = VertexBuffer::VertexData(m_posx + x + .5f, y - .5f, m_posz + z - .5f, 1.f, 1.f, 1.f, u, v + h);
    }

    //top
    if(GetBlock(x,y+1,z) == BTYPE_AIR){
        vd[count++] = VertexBuffer::VertexData(m_posx + x - .5f, y + .5f, m_posz + z - .5f, 0.8f, 0.8f, 0.8f, u, v);
        vd[count++] = VertexBuffer::VertexData(m_posx + x - .5f, y + .5f, m_posz + z + .5f, 0.8f, 0.8f, 0.8f, u + w, v);
        vd[count++] = VertexBuffer::VertexData(m_posx + x + .5f, y + .5f, m_posz + z + .5f, 0.8f, 0.8f, 0.8f, u + w, v + h);
        vd[count++] = VertexBuffer::VertexData(m_posx + x + .5f, y + .5f, m_posz + z - .5f, 0.8f, 0.8f, 0.8f, u, v + h);
    }

    //bottom 
    if(GetBlock(x,y-1,z) == BTYPE_AIR){
        vd[count++] = VertexBuffer::VertexData(m_posx + x - .5f, y - .5f, m_posz + z - .5f, 0.8f, 0.8f, 0.8f, u, v);
        vd[count++] = VertexBuffer::VertexData(m_posx + x + .5f, y - .5f, m_posz + z - .5f, 0.8f, 0.8f, 0.8f, u + w, v);
        vd[count++] = VertexBuffer::VertexData(m_posx + x + .5f, y - .5f, m_posz + z + .5f, 0.8f, 0.8f, 0.8f, u + w, v + h);
        vd[count++] = VertexBuffer::VertexData(m_posx + x - .5f, y - .5f, m_posz + z + .5f, 0.8f, 0.8f, 0.8f, u, v + h);
    }
    
    //left
    if(GetBlock(x-1,y,z) == BTYPE_AIR){
        vd[count++] = VertexBuffer::VertexData(m_posx + x - .5f, y - .5f, m_posz + z + .5f, 0.9f, 0.9f, 0.9f, u, v);
        vd[count++] = VertexBuffer::VertexData(m_posx + x - .5f, y + .5f, m_posz + z + .5f, 0.9f, 0.9f, 0.9f, u + w, v);
        vd[count++] = VertexBuffer::VertexData(m_posx + x - .5f, y + .5f, m_posz + z - .5f, 0.9f, 0.9f, 0.9f, u + w, v + h);
        vd[count++] = VertexBuffer::VertexData(m_posx + x - .5f, y - .5f, m_posz + z - .5f, 0.9f, 0.9f, 0.9f, u, v + h);
    }

    //Right
    if(GetBlock(x+1,y,z) == BTYPE_AIR){
        vd[count++] = VertexBuffer::VertexData(m_posx + x + .5f, y + .5f, m_posz + z - .5f, 0.9f, 0.9f, 0.9f, u, v);
        vd[count++] = VertexBuffer::VertexData(m_posx + x + .5f, y + .5f, m_posz + z + .5f, 0.9f, 0.9f, 0.9f, u + w, v);
        vd[count++] = VertexBuffer::VertexData(m_posx + x + .5f, y - .5f, m_posz + z + .5f, 0.9f, 0.9f, 0.9f, u + w, v + h);
        vd[count++] = VertexBuffer::VertexData(m_posx + x + .5f, y - .5f, m_posz + z - .5f, 0.9f, 0.9f, 0.9f, u, v + h);
    }
}

void Chunk::Render() const{
    m_vertexBuffer.Render();
}

bool Chunk::IsDirty() const{
    return m_isDirty;
}


