#include "gamecube.h"

gamecube::gamecube(const Vec3 &pos, Color col)
    : position(pos), color(col) {}

void gamecube::Update(float flipSpeed)
{
    //Tom: Added vars for clarity; replaced old 180.0f, 0.0f
    const float MaxRotationAngle = 180.0f;
    const float NoRotationAngle = 0.0f;

    if (flippingForward)
    {
        rotation += flipSpeed;
        if (rotation >= MaxRotationAngle)
        {
            rotation = MaxRotationAngle;
            flippingForward = false;
            flipped = true;
        }
    }
    else if (flippingBackward)
    {
        rotation -= flipSpeed;
        if (rotation <= NoRotationAngle)
        {
            rotation = NoRotationAngle;
            flippingBackward = false;
            flipped = false;
        }
    }
}

void gamecube::FlipForward()   { flippingForward = true; }
void gamecube::FlipBackward()  { flippingBackward = true; }

bool gamecube::IsFlipped() const { return flipped; }
bool gamecube::IsMatched() const { return matched; }
void gamecube::SetMatched(bool m) { matched = m; }

void gamecube::Draw() const
{
    rlPushMatrix();

    // Matrizen für Rotation und Translation erzeugen
    auto matrix_a = gameMatrix::translate({ position.x, position.y, position.z});
    auto matrix_b = gameMatrix::rot3D(rotation, 'y');

    // Matrizen multiplizieren (Translation * Rotation)
    auto model = gameMatrix::matmul(matrix_a, matrix_b);

    // transform for raylib matrix
    float f[16];
    for (int i = 0; i < 4; i++)
        for (int j = 0; j < 4; j++)
            f[j * 4 + i] = model[i][j];
    rlMultMatrixf(f);

    if (rotation < 90.0f)
        DrawCube({0,0,0}, 1,1,1, GRAY);
    else
        DrawCube({0,0,0}, 1,1,1, color);

    DrawCubeWires({0,0,0}, 1,1,1, BLACK);

    rlPopMatrix();
}

Vec3 gamecube::GetPosition() const     { return position; }
float gamecube::GetRotationY() const   { return rotation; }