//
// Created by bruno on 3/30/25.
//

#include "messages.h"


// Convert a 32-bit number to a string (10 digits, zero-padded)
void u32_to_str(uint32_t num, char *str) {
    for (int i = 9; i >= 0; i--) {
        str[i] = '0' + (num % 10);
        num /= 10;
    }
    str[10] = '\0';
}

// Convert an 8-bit number to a 2-digit string (zero-padded)
void u8_to_str(uint8_t num, char *str) {
    str[0] = '0' + (num / 100);      // Hundreds place
    str[1] = '0' + ((num / 10) % 10); // Tens place
    str[2] = '0' + (num % 10);       // Ones place
    str[3] = '\0';
}


void UI_DisplayMessages(void) {
    char String[19];
    UI_DisplayClear();
    if (gEnteringSMS != SMS_NOT_ENTERING) {
        if (gEnteringSMS == SMS_ENTERING_DEST) {
            UI_PrintString("SMS dest", 0, 0, 0 /*, 8 */);
            u32_to_str(dataPacket.dest, String);
            UI_PrintStringSmallNormal(String, 0, 0, 2);
        } else if (gEnteringSMS == SMS_ENTERING_MESSAGE) {
            UI_PrintString("SMS data", 0, 0, 0 /*, 8 */);
            UI_PrintStringSmallNormal((const char *) dataPacket.data, 1, 0, 1);
            memset(gFrameBuffer[1] + 2 + (7 * strlen((const char *) dataPacket.data)), gKeyTimeout == 0 ? 0xFF : 0x80,
                   6);
        }
    } else {
        uint8_t Data[8];
        EEPROM_ReadBuffer(SEQParameterEEPROM, Data, 8);
        uint8_t msgIndex = Data[1]; // index of latest written message

        // Determine oldest message index (next to be overwritten)
        uint8_t startIndex = (msgIndex + 1) % MESSAGES_COUNT;

        // Each bank shows 3 messages
        uint8_t messagesPerPage = 3;

        // Compute offset for scrolling (0 = newest, higher = older)
        uint8_t pageOffset = gActiveMessageBank * messagesPerPage;

        for (uint8_t ixa = 0; ixa < messagesPerPage; ixa++) {
            // Logical order from oldest to newest
            uint8_t logicalIndex = (startIndex + pageOffset + ixa) % MESSAGES_COUNT;

            // If out of stored range, stop
            if (pageOffset + ixa >= MESSAGES_COUNT)
                break;

            gActiveMessage = logicalIndex;
            MESSAGES_GET();

            // Build the display string
            u32_to_str(gActiveMessage == MESSAGES_COUNT ? dataPacket.dest : loadedPacket.src, &String[0]);
            String[10] = '-';
            u8_to_str(loadedPacket.seq, &String[11]);
            String[14] = '-';

            // Display a visual index (0,1,2,3... not EEPROM slot)
            uint8_t visualIndex = ixa + (gActiveMessageBank * 3);
            String[15] = (visualIndex < 10) ? ('0' + visualIndex) : ('A' + visualIndex - 10);
            String[16] = '-';
            String[17] = (gActiveMessage < 10) ? ('0' + gActiveMessage) : ('A' + gActiveMessage - 10);
            String[18] = 0;

            // Print both metadata and message
            UI_PrintString(String, 2, 0, ixa * 2);
            UI_PrintString((const char *) loadedPacket.data, 2, 0, ixa * 2 + 1);
        }
    }
    ST7565_BlitFullScreen();
}