#include <examples_common.c>

FONT *f;

int letter(int c)
{
    return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_';
}

int digit(int c)
{
    return c >= '0' && c <= '9';
}

void draw_text(char *text, int scroll_x, int scroll_y)
{
    char const *pos = text;
    char *line = NULL;
    int y = scroll_y;
    clear_to_color(screen, makecol(255, 255, 255));
    while (1)
    {
        char *line_end = strstr(pos, "\n");
        int colors[][4] =
        {
            {0, 0, 0},
            {128, 0, 128},
            {128, 128, 255},
            {255, 0, 0},
            {0, 192, 128}
        };
        if (line_end)
        {
            int l = line_end - pos;
            line = realloc(line, l + 1);
            strncpy(line, pos, l);
            line[l] = '\0';
        }
        else
        {
            int l = pos + strlen(pos) - pos;
            line = realloc(line, l + 1);
            strncpy(line, pos, l);
            line[l] = '\0';
        }
        int x = scroll_x, i;
        int *c = colors[0];
        for (i = 0; i < (int)strlen(line); i++)
        {
            char str[16] = "";
            uinsert(str, 0, line[i]);

            if (c == colors[2] && !digit(str[0])) c = colors[0];

            if (c == colors[0])
            {
                if (str[0] == '#' && i == 0) c = colors[1];
                if (digit(str[0])) c = colors[2];
                if (!ustrncmp(line + i, "install_fudgefont", 17)) c = colors[3];
                if (!ustrncmp(line + i, "fudgefont", 8)) c = colors[3];
                if (!ustrncmp(line + i, "gl", 2)) c = colors[4];
                if (!ustrncmp(line + i, "allegro_gl", 10)) c = colors[4];
            }

            if ((c == colors[3] || c == colors[4]) && !letter(str[0])) c = colors[0];

            my_color(c[0], c[1], c[2]);
            my_print(f, x, y, 0, "%s", str);

            x += text_length(f, str);
        }
        y += text_height(f);
        if (!line_end)
            break;
        pos = line_end + 1;
    }
}

int main(void)
{
    srand(time(NULL));

    allegro_init();

    my_setup_screen();

    install_keyboard();
    install_timer();
    install_mouse();
    show_os_cursor(MOUSE_CURSOR_ARROW);

    install_fudgefont();

    f = my_load_font("ttf/DejaVuSans.ttf", 12);

    PACKFILE *file = pack_fopen("src/example2.c", "r");
    int s = file_size("src/example2.c");
    char *str = malloc(s + 1);
    pack_fread(str, s, file);
    str[s] = '\0';
    int scroll_x = 0, scroll_y = 0;
    int omx = mouse_x, omy = mouse_y;
    draw_text(str, scroll_x, scroll_y);
    while (!key[KEY_ESC])
    {
        int redraw = 0;
        int mx = mouse_x;
        int my = mouse_y;
        if (mouse_b & 1)
        {
            scroll_x += mx - omx;
            scroll_y += my - omy;
            redraw = 1;
        }
        omx = mx;
        omy = my;

        int k = 0;
        while (keypressed())
        {
            k = readkey() >> 8;
            if (k == KEY_LEFT)
            {
                redraw = 1;
                scroll_x += 10;
            }
            if (k == KEY_RIGHT)
            {
                redraw = 1;
                scroll_x -= 10;
            }
            if (k == KEY_UP)
            {
                redraw = 1;
                scroll_y += 10;
            }
            if (k == KEY_DOWN)
            {
                redraw = 1;
                scroll_y -= 10;
            }
            if (k == KEY_PGUP)
            {
                redraw = 1;
                scroll_y += 100;
            }
            if (k == KEY_PGDN)
            {
                redraw = 1;
                scroll_y -= 100;
            }
        }

        if (redraw)
            draw_text(str, scroll_x, scroll_y);

        redraw = 0;

        my_flip();

        rest(10);
    }

    return 0;
}
