#include <SDL3/SDL.h>
#include <stdio.h>

#include "../src/interface/sdl_if.h"

static int failure = 0;

static void check_int(const char *test, int value, int expected)
{
    if (value == expected) {
        printf("[PASS] %s Result=%d\n", test, value);
    }
    else {
        ++failure;
        printf("[FAIL] %s Expected=%d Result=%d\n", test, expected, value);
    }
}

static void check_geometry(const char *name, SDL_Rect bounds,
        int exp_w, int exp_h, int exp_x, int exp_y)
{
    int w = -1;
    int h = -1;
    int x = -1;
    int y = -1;
    int rc = pc_calculate_first_use_window_geometry(&bounds, &w, &h, &x, &y);

    check_int(name, rc, 0);
    check_int("width", w, exp_w);
    check_int("height", h, exp_h);
    check_int("x", x, exp_x);
    check_int("y", y, exp_y);
}

int main(void)
{
    int w = 111;
    int h = 222;
    int x = 333;
    int y = 444;
    SDL_Rect bounds;

    bounds.x = 0;
    bounds.y = 0;
    bounds.w = 1920;
    bounds.h = 1080;
    check_geometry("standard", bounds, 1440, 810, 240, 135);

    bounds.x = -1920;
    bounds.y = 0;
    bounds.w = 1920;
    bounds.h = 1080;
    check_geometry("offset", bounds, 1440, 810, -1680, 135);

    bounds.x = 0;
    bounds.y = 0;
    bounds.w = 900;
    bounds.h = 500;
    check_geometry("wide-short", bounds, 675, 480, 112, 10);

    bounds.x = 10;
    bounds.y = 20;
    bounds.w = 500;
    bounds.h = 900;
    check_geometry("narrow-tall", bounds, 500, 675, 10, 132);

    bounds.x = 5;
    bounds.y = 6;
    bounds.w = 320;
    bounds.h = 240;
    check_geometry("smaller-than-min", bounds, 320, 240, 5, 6);

    bounds.x = 0;
    bounds.y = 0;
    bounds.w = 0;
    bounds.h = 1080;
    check_int("invalid", pc_calculate_first_use_window_geometry(
                &bounds, &w, &h, &x, &y), -1);
    check_int("invalid-width-unchanged", w, 111);
    check_int("invalid-height-unchanged", h, 222);
    check_int("invalid-x-unchanged", x, 333);
    check_int("invalid-y-unchanged", y, 444);

    return failure;
}
