#include "AardRemoteMessage.h"
#include "Commands.h"
#include <stdio.h>

AardRemoteMessage::AardRemoteMessage(unsigned short header) {
   message.push_back(header);
   hdr = header;
}

AardRemoteMessage::~AardRemoteMessage() {
}

unsigned char
AardRemoteMessage::MessageKind() const {
   return MSG_KIND(b[0]);
}

unsigned int
AardRemoteMessage::MessageLength() const {
   return MSG_LEN(b[0]);
}

unsigned int
AardRemoteMessage::FunctionCode() const {
   return b[1];
}

const std::vector<unsigned short> &
AardRemoteMessage::MessageBody() const {
   return message;
}

void
AardRemoteMessage::Append(unsigned short newdata) {
   message.push_back(newdata);
}

void
AardRemoteMessage::DebugDump() {
   switch(MSG_KIND(b[0])) {
   case MK_INVALID:
      printf("I");
      break;
   case MK_COMMAND:
      printf("C");
      break;
   case MK_REPLY:
      printf("R");
      break;
   case MK_ASYNC:
      printf("A");
      break;
   }
   printf("(%d)", MSG_LEN(b[0]));
   switch(MSG_KIND(b[0])) {
   case MK_INVALID:
      printf("?_0x%02x",b[1]);
      break;
   case MK_COMMAND:
   case MK_REPLY:
      switch(b[1]) {
      case MKR_NACK:
         printf("MKR_NACK");
         break;
      case MKCR_QVERSION:
         printf("MKCR_QVERSION");
         break;
      case MKCR_RECEIVER:
         printf("MKCR_RECEIVER");
         break;
      case MKCR_QRECEIVER:
         printf("MKCR_QRECEIVER");
         break;
      case MKCR_FREQUENCY:
         printf("MKCR_FREQUENCY");
         break;
      case MKCR_QFREQUENCY:
         printf("MKCR_QFREQUENCY");
         break;
      case MKCR_QMICROS:
         printf("MKCR_QMICROS");
         break;
      case MKCR_SENDRAW:
         printf("MKCR_SENDRAW");
         break;
      case MKCR_WAITAVAIL:
         printf("MKCR_WAITAVAIL");
         break;
      case MKCR_WAITSEND:
         printf("MKCR_WAITSEND");
         break;
      case MKCR_QUEUESIZE:
         printf("MKCR_QUEUESIZE");
         break;
      default:
         printf("?_0x%02x",b[1]);
         break;
      }
      break;
   case MK_ASYNC:
      switch(b[1]) {
      case MKAS_RESET:
         printf("MKAS_RESET");
         break;
      case MKAS_BILGE:
         printf("MKAS_BILGE");
         break;
      case MKAS_GLOBERR:
         printf("MKAS_GLOBERR");
         break;
      case MKAS_RAWIR:
         printf("MKAS_RAWIR");
         break;
      default:
         printf("?_0x%02x",b[1]);
         break;
      }
      break;
   }
   printf(":");
   for (int i = 0; i < message.size(); ++i) {
      printf(" %04x", message[i]);
   }
   printf("\n");
}
