#include <screen/screen.h>

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

static void die(const char* message)
{
    perror(message);
    exit(EXIT_FAILURE);
}

static void draw_color_bars(uint8_t* pixels, int width, int height, int stride)
{
    static const uint8_t colors[8][4] = {
        {   0,   0,   0, 255 }, /* black */
        {   0,   0, 255, 255 }, /* blue */
        {   0, 255,   0, 255 }, /* green */
        {   0, 255, 255, 255 }, /* cyan */
        { 255,   0,   0, 255 }, /* red */
        { 255,   0, 255, 255 }, /* magenta */
        { 255, 255,   0, 255 }, /* yellow */
        { 255, 255, 255, 255 }, /* white */
    };

    for (int y = 0; y < height; ++y) {
        uint8_t* row = pixels + y * stride;

        for (int x = 0; x < width; ++x) {
            int index = x * 8 / width;
            uint8_t* p = row + x * 4;

            p[0] = colors[index][0];
            p[1] = colors[index][1];
            p[2] = colors[index][2];
            p[3] = colors[index][3];
        }
    }
}

int main(void)
{
    screen_context_t context;
    screen_window_t window;
    screen_buffer_t buffer;
    screen_buffer_t buffers[1];

    int size[2] = { 1280, 720 };
    int usage = SCREEN_USAGE_WRITE;
    int format = SCREEN_FORMAT_BGRA8888;

    void* pointer;
    int stride;

    if (screen_create_context(&context, SCREEN_APPLICATION_CONTEXT) != 0) {
        die("screen_create_context");
    }

    if (screen_create_window(&window, context) != 0) {
        die("screen_create_window");
    }

    if (screen_set_window_property_iv(window, SCREEN_PROPERTY_USAGE, &usage) != 0) {
        die("SCREEN_PROPERTY_USAGE");
    }

    if (screen_set_window_property_iv(window, SCREEN_PROPERTY_FORMAT, &format) != 0) {
        die("SCREEN_PROPERTY_FORMAT");
    }

    if (screen_set_window_property_iv(window, SCREEN_PROPERTY_SIZE, size) != 0) {
        die("SCREEN_PROPERTY_SIZE");
    }

    if (screen_set_window_property_iv(window, SCREEN_PROPERTY_BUFFER_SIZE, size) != 0) {
        die("SCREEN_PROPERTY_BUFFER_SIZE");
    }

    if (screen_create_window_buffers(window, 1) != 0) {
        die("screen_create_window_buffers");
    }

    if (screen_get_window_property_pv(window, SCREEN_PROPERTY_RENDER_BUFFERS, (void**)buffers) != 0) {
        die("SCREEN_PROPERTY_RENDER_BUFFERS");
    }

    buffer = buffers[0];

    if (screen_get_buffer_property_pv(buffer, SCREEN_PROPERTY_POINTER, &pointer) != 0) {
        die("SCREEN_PROPERTY_POINTER");
    }

    if (screen_get_buffer_property_iv(buffer, SCREEN_PROPERTY_STRIDE, &stride) != 0) {
        die("SCREEN_PROPERTY_STRIDE");
    }

    draw_color_bars(pointer, size[0], size[1], stride);

    int dirty[4] = { 0, 0, size[0], size[1] };

    if (screen_post_window(window, buffer, 1, dirty, 0) != 0) {
        die("screen_post_window");
    }

    puts("color bars displayed");
    sleep(30);

    screen_destroy_window(window);
    screen_destroy_context(context);

    return EXIT_SUCCESS;
}
