#include "gobotrpc.hpp"
#include <stdio.h>

const char * TEST_MSG_RPC_RESULT_GOOD = "Good";
const char * TEST_MSG_RPC_RESULT_PACKAGE_READY = "Package Ready";
const char * TEST_MSG_RPC_RESULT_ERROR_INVALID_FRAMESUBMITIONRESULT = "Invalid FrameSubmitionResult";
const char * TEST_MSG_RPC_RESULT_ERROR_INVALID_RPC_TYPE = "Invalid RPC Type (0b10)";
const char * TEST_MSG_RPC_RESULT_ERROR_INVALID_RPC_NUMBER = "Invalid RPC Number";
const char * TEST_MSG_RPC_RESULT_ERROR_NO_EMPTY_OR_FITTING_SLOT = "No empty or fitting slot for submitted frame";

char * mapFrameSubmitionResult2String(FrameSubmitionResult res) {

    switch (res) {
        case PENDING: return (char *)(TEST_MSG_RPC_RESULT_GOOD);
        case COMPLEATE: return (char *) TEST_MSG_RPC_RESULT_PACKAGE_READY;

        case ERROR_INVALID_RPC_TYPE: return (char *) TEST_MSG_RPC_RESULT_ERROR_INVALID_RPC_TYPE;
        case ERROR_INVALID_RPC_NUMBER: return (char *) TEST_MSG_RPC_RESULT_ERROR_INVALID_RPC_NUMBER;
        case ERROR_NO_EMPTY_OR_FITTING_SLOT: return (char *) TEST_MSG_RPC_RESULT_ERROR_INVALID_FRAMESUBMITIONRESULT;
    }
    return (char *) TEST_MSG_RPC_RESULT_ERROR_INVALID_FRAMESUBMITIONRESULT;
}

void GobotRPCParser::print_out_slots() {
    for(int i = 0; i < NUM_SLOTS; i++) {
        printf("[%d] %d%d (%x-%d) %d T%08x S=%02d ", i, 
            buffer[i].is_complete, buffer[i].is_in_use, buffer[i].sender_address,
            buffer[i].rpcNum, buffer[i].type, buffer[i].timestamp, buffer[i].length
        );

        for(int j = 0; j < MAX_PAGE_SIZES; j++) {
            printf("%02x", buffer[i].buffer[j]);
        }

        printf(" ");

        for(int j = 0; j < 32; j++) {
            printf("%d", (buffer[i].used_bit_masked >> (31-j)) & 1);
        }

        printf("\n");
    }
}
