import std.stdio;
import std.datetime;

struct Texture2D {
    uint id;
    int width;
    int height;
    int mipmaps;
    int format;
}

struct Vector2 {
    float x;
    float y;
}

struct Color {
    ubyte r;
    ubyte g;
    ubyte b;
    ubyte a;
}

extern(C):
    void InitWindow(int width, int height, const char* title);
    void CloseWindow();

    void SetTargetFPS(int fps);
    void SetRandomSeed(uint seed);
    int GetRandomValue(int min, int max);

    Texture2D LoadTexture(const char* file_name);
    void UnloadTexture(Texture2D texture);

    bool WindowShouldClose();
    float GetFrameTime();

    void BeginDrawing();
    void ClearBackground(Color color);
    void DrawTextureEx(Texture2D texture, Vector2 position, float rotation, float scale, Color tint);
    void EndDrawing();

const int WINDOW_WIDTH = 800;
const int WINDOW_HEIGHT = 600;
const float LOGO_SCALE = 0.1f;
const float LOGO_SPEED = 300;

const Color BLACK = Color(0, 0, 0, 255);
const Color WHITE = Color(255, 255, 255, 255);

void main() {
    InitWindow(800, 600, "Raylib in D");
    SetTargetFPS(60);
    SetRandomSeed(cast(uint)Clock.currStdTime());

    Texture2D logo_texture = LoadTexture("./d_logo.png");

    int logo_width = cast(int)(logo_texture.width * LOGO_SCALE);
    int logo_height = cast(int)(logo_texture.height * LOGO_SCALE);

    float x = GetRandomValue(0, WINDOW_WIDTH - logo_width);
    float y = GetRandomValue(0, WINDOW_HEIGHT - logo_height);

    float dx = LOGO_SPEED * (GetRandomValue(0, 1) ? -1 : 1);
    float dy = LOGO_SPEED * (GetRandomValue(0, 1) ? -1 : 1);

    while (!WindowShouldClose()) {
        float delta_time = GetFrameTime();

        x += dx * delta_time;
        y += dy * delta_time;

        if (x < 0 || (x + logo_width) >= WINDOW_WIDTH - 1)
        {
            dx *= -1;
            x += dx * delta_time;
        }
        if (y < 0 || (y + logo_height) >= WINDOW_HEIGHT - 1)
        {
            dy *= -1;
            y += dy * delta_time;
        }

        BeginDrawing();

        ClearBackground(BLACK);
        DrawTextureEx(logo_texture, Vector2(x, y), 0, LOGO_SCALE, WHITE);

        EndDrawing();
    }

    UnloadTexture(logo_texture);
    CloseWindow();
}