#include "background.h"

Cloud::Cloud(float speed, glm::vec2 pos, glm::vec2 size, int cloudIndex)
    : speed(speed), pos(pos), size(size), spriteUVs(spriteUVs)
{
    int cloudIndexes[5][3] =
        {{20, 2, 1},
         {22, 2, 1},
         {24, 1, 1},
         {25, 3, 1},
         {42, 6, 2}};

    glm::vec4 spriteUVs = spriteSheet.getUVsExtended(cloudIndexes[cloudIndex][0], cloudIndexes[cloudIndex][1], cloudIndexes[cloudIndex][2]);
    GLfloat vertices[] = {
        // Pos      // Tex
        0.0f, 1.0f, spriteUVs.x, spriteUVs.y,
        1.0f, 0.0f, spriteUVs.z, spriteUVs.w,
        0.0f, 0.0f, spriteUVs.x, spriteUVs.w,
        1.0f, 1.0f, spriteUVs.z, spriteUVs.y};

    // Configure VAO/VBO
    unsigned int indices[] = {
        0, 1, 2,
        0, 1, 3};
    glGenVertexArrays(1, &this->quadVAO);
    glGenBuffers(1, &this->VBO);
    glGenBuffers(1, &this->EBO);

    glBindBuffer(GL_ARRAY_BUFFER, this->VBO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->EBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

    glBindVertexArray(this->quadVAO);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (GLvoid *)0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);
    glActiveTexture(GL_TEXTURE0);
    glBindBuffer(GL_ARRAY_BUFFER, this->VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
}

void Cloud::drawCloud(SpriteRenderer &renderer, float dt)
{
    //renderer.DrawSprite(this->Sprite, this->Position, this->spriteUVs, this->vertices, this->Size, this->Rotation);
    pos.x += speed * dt;
    ResourceManager::GetShader("shader").Use();
    glm::mat4 model = glm::mat4(1.0);
    model = glm::translate(model, glm::vec3(pos, 0.0f)); // First translate
    model = glm::scale(model, glm::vec3(size, 0.0f));    // Last scale

    ResourceManager::GetShader("shader").SetMatrix4("model", model);

    this->Sprite.bind();
    glBindVertexArray(this->quadVAO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->EBO);
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
};

Background::Background()
{
    srand(time(NULL));
    for (int i = 0; i < 50; i++)
    {
        int cloudIndexes[5][3] =
            {{20, 2, 1},
             {22, 2, 1},
             {24, 1, 1},
             {25, 3, 1},
             {42, 6, 3}};
        float xScalar = (float)(rand() % 4000 + 1);
        float yScalar = (float)(rand() % 1000 + 1);
        float sizeScalar = 5.0f / (float)(rand() % 6 + 3);
        float speedScalar = (float)(rand() % 3);
        int cloudIndex = rand() % 4;
        // std::cout << cloudIndex << "\n";
        clouds.push_back(new Cloud(speedScalar * sizeScalar,
                                   glm::vec2(xScalar - 500.0f, yScalar),
                                   glm::vec2(128.0f * (float)cloudIndexes[cloudIndex][1], 128.0f * (float)cloudIndexes[cloudIndex][2]) * sizeScalar, cloudIndex));
    }
    clouds.push_back(new Cloud(1.0f,
                               glm::vec2(1000.0f, 300.0f),
                               glm::vec2(128.0f * 6.0f, 128.0f * 3.0f), 4));
}
void Background::drawBackground(SpriteRenderer &renderer, float dt)
{
    for (int i = 0; i < clouds.size(); i++)
    {
        clouds[i]->drawCloud(renderer, dt);
    }
}