#include <stdio.h>
#include <conio.h>
#include <unistd.h>     /* sleep */
#include <sprinter_exit.h>

/*
 * Exercises every conio function:
 *   clrscr  — clear the screen
 *   gotoxy  — move cursor to (x, y), 0-based
 *   putch   — single character
 *   cputs   — string without auto-newline
 *   kbhit   — non-blocking key probe
 *   getch   — blocking, no echo
 *   getche  — blocking, with echo
 *
 * Sprinter native text mode is 80×32, so we anchor corners at
 * (0,0), (79,0), (0,31), (79,31).
 */

extern two_bytes pc_place;

int main(void)
{
    /* 1. clrscr + diagnostic probes + a centered banner.
     *
     * Row-0 probe: figure out why [NW]/[NE] don't paint.
     *   '0','1','2','3' — single putch at row 0, cols 0/5/40/79
     *   "R0"            — cputs at row 0, col 10
     *   'a','b','c','R1'— same pattern on row 1 (control)
     */
    clrscr();

    gotoxy(1, 0);
    textattr(0x07);
    putch('0');
    textattr(0x70);
    putch('1');
    // gotoxy(10, 20);
    printf("X = %u, Y = %u\n", pc_place.byte.low, pc_place.byte.high);
    uint8_t x = wherex();
    uint8_t y = wherey();
    printf("X = %u, Y = %u\n", x, y);

    set_putch_raw_mode(1);

    textattr(0x0F);
    gotoxy(4, 3);
    for (int i = 0; i < 16; i++) {
        putch(i < 10 ? '0' + i : 'A' - 10 + i);
        putch(' ');
    }

    for (int j = 0; j < 16; j++) {
        gotoxy(2, 4 + j);
        textattr(0x0F);
        putch(j < 10 ? '0' + j : 'A' - 10 + j);
        putch(' ');
        for (int i = 0; i < 16; i++) {
            textattr((j << 4) + i);
            putch((j << 4) + i); putch(' ');
        }
    }

    set_text_attr(0x011D);
    set_putch_raw_mode(0);
    gotoxy(0,22);
    cputs("Test message line 1\nTest message line2\rline3\n\r1234\t5678\b90\n\r");

    set_putch_raw_mode(1);
    gotoxy(0,25);
    cputs("Test message line 1\nTest message line2\rline3\n\r1234\t5678\b90\n\r");

    set_text_attr(0x001D);
    set_putch_raw_mode(0);
    gotoxy(0,28);
    cputs("Test message line 1\nTest message line2\rline3\n\r1234\t5678\b90\n\r");

    set_putch_raw_mode(1);
    gotoxy(0,31);
    cputs("Test message line 1\nTest message line2\rline3\n\r1234\t5678\b90\n\r");

    (void)getch();

    return 0;
}
