#include "ColorCube.h"
static unsigned int s_nbColorCubes=0;

// Constructeur et Destructeur
ColorCube::ColorCube()
{
    // Incr�mentation du nombre de cubes
    s_nbColorCubes++;

    // Vertices
    float verticesTmp[42] = {0.0f,0.0f,1.0f,   1.0f,1.0f,1.0f,   1.0f,0.0f,1.0f,  1.0f,0.0f,0.0f,  0.0f,0.0f,1.0f,  0.0f,0.0f,0.0f,        0.0f,1.0f,0.0f,
                        1.0f,0.0f,0.0f,    1.0f,1.0f,0.0f,    1.0f,1.0f,1.0f,    0.0f,1.0f,0.0f,    0.0f,1.0f,1.0f,    0.0f,0.0f,1.0f,      1.0f,1.0f,1.0f};
    for(int i(0); i < 42; i++)
        m_vertices[i] = verticesTmp[i];

    // Couleurs
    float couleursTmp[42] = {1.0,1.0,0.0,     1.0,0.0,0.0,   1.0,0.0,0.0,   0.0,0.0,1.0,    1.0,1.0,0.0,     0.0,1.0,0.0,           0.0,1.0,0.0,
                            0.0,0.0,1.0,      0.0,0.0,1.0,      1.0,0.0,0.0,      0.0,1.0,0.0,      1.0,1.0,0.0,      1.0,1.0,0.0,     1.0,0.0,0.0};
    for (int i(0); i<42; i++)
        m_couleurs[i] = couleursTmp[i];
}


ColorCube::ColorCube(float taille, float red, float green, float blue)
{
    // Incr�mentation du nombre de cubes
    s_nbColorCubes++;

    // Vertices
    float verticesTmp[42] = {0.f,0.0f,taille,   taille,taille,taille,   taille,0.0f,taille,  taille,0.0f,0.0f,  0.0f,0.0f,taille,  0.0f,0.0f,0.0f,        0.0f,taille,0.0f,
                        taille,0.0f,0.0f,    taille,taille,0.0f,    taille,taille,taille,    0.0f,taille,0.0f,    0.0f,taille,taille,    0.0f,0.0f,taille,      taille,taille,taille};
    for(int i(0); i < 42; i++)
        m_vertices[i] = verticesTmp[i];

    // Couleurs
    setColor(red,green,blue);
}


ColorCube::~ColorCube()
{
    s_nbColorCubes--;
}


// M�thodes
void ColorCube::setColor(float red, float green, float blue)
{
    // Attribution des couleurs
    for(int i(0); i < 42; i++)
    {
        switch (i%3)
        {
            case 0:
                m_couleurs[i] = red;
                break;
            case 1:
                m_couleurs[i] = green;
                break;
            case 2:
                m_couleurs[i] = blue;
                break;
        }
    }
}

void ColorCube::afficher()
{
    // Envoi des vertices
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, m_vertices);
    glEnableVertexAttribArray(0);

    // Envoi de la couleur
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, m_couleurs);
    glEnableVertexAttribArray(1);

    // Rendu
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 14);

    // D�sactivation des tableaux
    glDisableVertexAttribArray(1);
    glDisableVertexAttribArray(0);
}

void ColorCube::move(glm::vec3 deplacement)
{
    for(int i(0); i < 42; i++)
    {
        switch (i%3)
        {
            case 0:
                m_vertices[i] += deplacement.x;
                break;
            case 1:
                m_vertices[i] += deplacement.y;
                break;
            case 2:
                m_vertices[i] += deplacement.z;
                break;
        }
    }
}

unsigned int getNbColorCubes()
{
    return s_nbColorCubes;
}