// main.cpp

#include "mbed.h"

#define TARGET_TX_PIN USBTX
#define TARGET_RX_PIN USBRX
// #define BAUD_RATE 1843200
// #define BAUD_RATE 921600
#define BAUD_RATE 460800

static BufferedSerial serial_port(TARGET_TX_PIN, TARGET_RX_PIN, BAUD_RATE);

FileHandle *mbed::mbed_override_console(int fd)
{
    return &serial_port;
}

DigitalOut led(LED1);

#define BUFFER_SIZE 64
static char rx_buffer[BUFFER_SIZE];

// Draw a moving “sky + ground” background with optional message
void draw_mask(uint32_t mask, int shift, const char *text = nullptr)
{
    const int width = 80;   // terminal columns
    const int height = 24;  // terminal rows
    const int horizon = 16; // sky ends here, ground below

    printf("\033[2J\033[H"); // clear + home

    // draw sky layer (moving clouds)
    for (int y = 0; y < horizon; y++) {
        for (int x = 0; x < width; x++) {
            // Parallax: slower movement at top
            int move = (shift / 2 + y) % width;
            char c = ((x + move) % 11 == 0) ? '☁' : ' ';
            printf("%c", c);
        }
        printf("\r\n");
    }

    // draw horizon line
    for (int i = 0; i < width; i++) printf("-");
    printf("\r\n");

    // draw ground (grass / terrain)
    for (int y = horizon + 1; y < height; y++) {
        for (int x = 0; x < width; x++) {
            int move = (shift * 2 + y) % width;
            // simple wave pattern for terrain / hill
            char c;
            if (((x + move) % 7) == 0)
                c = '^';
            else if (((x + move) % 5) == 0)
                c = '`';
            else
                c = ' ';
            printf("%c", c);
        }
        printf("\r\n");
    }

    // optional text overlay at bottom
    if (text && text[0] != '\0') {
        printf("\r\n[RX] %s\r\n", text);
    }

    fflush(stdout);
}

int main(void)
{
    serial_port.set_format(8, BufferedSerial::None, 1);
    printf("Baud: %d, Format: 8-N-1\r\n", BAUD_RATE);

    uint32_t mask = 0xF0F0A55A;
    int shift = 0;
    char message[BUFFER_SIZE] = {0};
    bool message_active = false;
    Timer msg_timer;
    Timer anim_timer;
    msg_timer.start();
    anim_timer.start();

    while (true) {
        if (serial_port.readable()) {
            memset(rx_buffer, 0, sizeof(rx_buffer));
            ssize_t num = serial_port.read(rx_buffer, sizeof(rx_buffer) - 1);
            if (num > 0) {
                led = !led;

                strncpy(message, rx_buffer, sizeof(message) - 1);
                message_active = true;
                msg_timer.reset();
            }
        }

        if (message_active && msg_timer.elapsed_time() > 1s) {
            message_active = false;
            memset(message, 0, sizeof(message));
        }

        // if (message_active && msg_timer.elapsed_time() > 100ms) {
	//	shift = (shift + 1) % 8;

        // }
        if (anim_timer.elapsed_time() >= 200ms) {
            shift = (shift + 1) % 8;
            anim_timer.reset();

	    draw_mask(mask, shift, message_active ? message : nullptr);
        }
        ThisThread::sleep_for(20ms);
    }
}
