#include <stdio.h>
// add all the shit from the other shit
#include "raylib.h"
#include "world.h"
#include "physics.h"
#include "player.h"
#include "render.h"
#include "config.h"

int main(void)
{
    // Let's make a window
    const int screenWidth = 1000;
    const int screenHeight = 600;

    InitWindow(
        screenWidth,
        screenHeight,
        "WrldBox Sandbox");

    SetTargetFPS(60);

    // Now we actually start the code
    InitWorld();

    printf("WrldBox engine started\n");
    printf("Gravity = %.2f\n", g);

    // until we close the window, expect inputs
    while (!WindowShouldClose())
    {
        float dt = GetFrameTime();

        if (IsKeyPressed(KEY_P))
        {
            // If it's simulating, stop simulating. If it isn't simulating, start simulating.
            isSimulating = !isSimulating;
        }

        if (IsKeyPressed(KEY_R))
        {
            // VERY complicated reset logic
            InitWorld();
        }

        if (IsKeyPressed(KEY_Q))
        {
            // We're gonna have to rewrite the entity logic at some point. Because an actual game doesn't just contain a bunch of blocks. But whatever for now...
            // Q key pressed? -> Poof, box, random position, random size, random color.
            SpawnEntity(
                GetRandomValue(50, 950),
                        GetRandomValue(20, 150),
                        GetRandomValue(15, 45),
                        1.0f,
                        (Color)
                        {
                            GetRandomValue(50,255),
                        GetRandomValue(50,255),
                        GetRandomValue(50,255),
                        255
                        });
        }

        if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
        {
            // Spawn cube where the mouse is when it clicks!
            Vector2 m = GetMousePosition();

            SpawnEntity(
                m.x,
                m.y,
                25,
                1.0f,
                ORANGE);
        }

        if (isSimulating)
        {
            // allow player to be controlled, fit everything within the size of the screen
            UpdatePlayerControls();
            UpdateEntities(dt, screenWidth);

            simTime += dt;
        }

        // UI shenanigans
        BeginDrawing();

        ClearBackground(RAYWHITE);

        DrawLine(
            0,
            (int)ground_y,
                 screenWidth,
                 (int)ground_y,
                 DARKGRAY);

        DrawText(
            "GROUND",
            10,
            (int)ground_y + 5,
                 20,
                 DARKGRAY);

        DrawEntities();

        DrawRectangle(
            0,
            0,
            screenWidth,
            120,
            Fade(LIGHTGRAY, 0.4f));

        DrawText(
            "WRLDBOX SANDBOX",
            10,
            10,
            28,
            BLACK);

        DrawText(
            TextFormat("Time: %.2f", simTime),
                 10,
                 45,
                 20,
                 BLACK);

        DrawText(
            TextFormat("Entities: %d",
                       CountEntities()),
                 10,
                 70,
                 20,
                 BLACK);

        if (player)
        {
            DrawText(
                TextFormat(
                    "Player Pos: %.1f %.1f",
                    player->position.x,
                    player->position.y),
                     250,
                     45,
                     20,
                     BLACK);

            DrawText(
                TextFormat(
                    "Player Vel: %.1f %.1f",
                    player->velocity.x,
                    player->velocity.y),
                     250,
                     70,
                     20,
                     BLACK);
        }

        DrawText(
            isSimulating ?
            "SIMULATING" :
            "PAUSED",
            850,
            15,
            25,
            isSimulating ?
            GREEN :
            RED);

        DrawText("A/D = Move", 700, 45, 18, BLACK);
        DrawText("SPACE = Jump", 700, 65, 18, BLACK);
        DrawText("LMB = Spawn Cube", 700, 85, 18, BLACK);
        DrawText("Q = Random Cube", 700, 105, 18, BLACK);
        DrawText("P = Pause | R = Reset", 700, 125, 18, BLACK);

        EndDrawing();
    }

    // When the window wants to close, close it. What a surprise.
    CloseWindow();
    return 0;
}
