#include "faces.h"
#include <stdlib.h>

#define TOP_LEFT { 0.0f, 0.0f }
#define TOP_RIGHT { 1.0f, 0.0f }
#define BOTTOM_LEFT { 0.0f, 1.0f }
#define BOTTOM_RIGHT { 1.0f, 1.0f }

void add_face(Mesh* mesh, vector3* face, vector2* tex, int block_id, vector3 pos) {
        int vert_start = mesh->vertexCount * 3;
        int tex_start = mesh->vertexCount * 2;

	mesh->vertexCount += 6;
	mesh->triangleCount += 2;

	mesh->vertices = realloc(mesh->vertices, (mesh->vertexCount * 3) * sizeof(float));
        mesh->texcoords = realloc(mesh->texcoords, (mesh->vertexCount * 2) * sizeof(float));

	for (int i = 0; i < 18; i += 3) {
                int v = i / 3;
		mesh->vertices[vert_start + i + 0] = face[v].x + pos.x;
		mesh->vertices[vert_start + i + 1] = face[v].y + pos.y;
		mesh->vertices[vert_start + i + 2] = face[v].z + pos.z; 
	}

        for (int i = 0; i < 12; i += 2) {
                int v = i / 2;

                int block_x = block_id % ATLAS_FIT;
                int block_y = block_id / ATLAS_FIT;

                mesh->texcoords[tex_start + i + 0] = ((block_x + tex[v].x) * TEXTURE_SIZE) / ATLAS_SIZE;
                mesh->texcoords[tex_start + i + 1] = ((block_y + tex[v].y) * TEXTURE_SIZE) / ATLAS_SIZE;
        }
}

vector3 bottom[] = {
        { -0.5f, -0.5f, -0.5f },
        {  0.5f, -0.5f, -0.5f },
        { -0.5f, -0.5f,  0.5f },

        {  0.5f, -0.5f, -0.5f },
        {  0.5f, -0.5f,  0.5f },
        { -0.5f, -0.5f,  0.5f }
};

vector2 bottom_tex[] = {
        BOTTOM_LEFT,
        BOTTOM_RIGHT,
        TOP_LEFT,

        BOTTOM_RIGHT,
        TOP_RIGHT,
        TOP_LEFT
};

vector3 top[] = {
        { -0.5f, 0.5f, -0.5f },
        { -0.5f, 0.5f,  0.5f },
        {  0.5f, 0.5f, -0.5f },

        {  0.5f, 0.5f, -0.5f },
        { -0.5f, 0.5f,  0.5f },
        {  0.5f, 0.5f,  0.5f }
};

vector2 top_tex[] = {
        BOTTOM_LEFT,
        TOP_LEFT,
        BOTTOM_RIGHT,

        BOTTOM_RIGHT,
        TOP_LEFT,
        TOP_RIGHT
};

vector3 front[] = {
        { -0.5f, -0.5f, 0.5f },
        {  0.5f, -0.5f, 0.5f },
        { -0.5f,  0.5f, 0.5f },

        {  0.5f, -0.5f, 0.5f },
        {  0.5f,  0.5f, 0.5f },
        { -0.5f,  0.5f, 0.5f }
};

vector2 front_tex[] = {
        BOTTOM_LEFT,
        BOTTOM_RIGHT,
        TOP_LEFT,

        BOTTOM_RIGHT,
        TOP_RIGHT,
        TOP_LEFT
};

vector3 back[] = {
        { -0.5f, -0.5f, -0.5f },
        { -0.5f,  0.5f, -0.5f },
        {  0.5f, -0.5f, -0.5f },

        {  0.5f, -0.5f, -0.5f },
        { -0.5f,  0.5f, -0.5f },
        {  0.5f,  0.5f, -0.5f }
};

vector2 back_tex[] = {
        BOTTOM_LEFT,
        TOP_LEFT,
        BOTTOM_RIGHT,

        BOTTOM_RIGHT,
        TOP_LEFT,
        TOP_RIGHT
};

vector3 right[] = {
        { 0.5f, -0.5f, -0.5f },
        { 0.5f,  0.5f,  0.5f },
        { 0.5f, -0.5f,  0.5f },

        { 0.5f, -0.5f, -0.5f },
        { 0.5f,  0.5f, -0.5f },
        { 0.5f,  0.5f,  0.5f }
};

vector2 right_tex[] = {
        BOTTOM_LEFT,
        TOP_RIGHT,
        TOP_LEFT,

        BOTTOM_LEFT,
        BOTTOM_RIGHT,
        TOP_RIGHT
};

vector3 left[] = {
        { -0.5f, -0.5f, -0.5f },
        { -0.5f, -0.5f,  0.5f },
        { -0.5f,  0.5f,  0.5f },

        { -0.5f, -0.5f, -0.5f },
        { -0.5f,  0.5f,  0.5f },
        { -0.5f,  0.5f, -0.5f }
};

vector2 left_tex[] = {
        BOTTOM_LEFT,
        TOP_LEFT,
        TOP_RIGHT,

        BOTTOM_LEFT,
        TOP_RIGHT,
        BOTTOM_RIGHT
};
