//-----------------------------------------------------------------------------
///
/// \brief  Arbiter for signals from Qt Interface
///
/// \date   20151129
/// \author Maximilian Seesslen <mes@seesslen.net>
///
//-----------------------------------------------------------------------------


//---Includes------------------------------------------------------------------


#include "qtgateway.h"
#include <biwak/linux/boardgui.h>
//#include <lepto/leak.h>
#include "ui_boardgui.h"
#include <QListWidget>
#include <lepto/dictionary.h>
//#include <lepto/telegram_data_structures.h>


//---Implementation------------------------------------------------------------


CQtGateway::CQtGateway( )
{
   return;
};


CQtGateway::~CQtGateway( )
{
   return;
}


void CQtGateway::telegramEventLoop() /*virtual*/
{
   //lHint << "Loop";
   qpf.getUi()->lineLeak->setText( QString("%1").arg( CLeak::objectCounter() ) );
   qpf.getUi()->lineTelegrams->setText( QString("%1").arg( listTelegram.count() ) );

   while( listTelegram.count() )
   {
      CTelegram &tel=listTelegram[0];
      switch(tel.getState())
      {
         case(eStateReady):
            printTelegram(tel);
            tel.setState(eStateFinished);
            break;

         case(eStateFinished):
            tel.setState(eStateBroken);
            listTelegram.removeAt(0);
            break;

         default:
            lHint << "Unknown state: " << tel.getState();
            break;
      }
   }
}


void CQtGateway::printTelegram(const CTelegram &tel)
{
   switch(tel.getIndex())
   {
      case(OBJECT_INDEX_RTC):
      {
         telegramRtc *data=(telegramRtc *)tel.getData();
         qpf.getUi()->listTelegrams->insertItem(0, QString("Time: %1:%2:%3")
            .arg(data->hour, 2, 10, QChar('0'))
            .arg(data->min, 2, 10, QChar('0'))
            .arg(data->sec, 2, 10, QChar('0')) );
         break;
      }
      default:
         lHint << "Unknown Object index: "  << tel.getIndex();
   }
}


//---fin-----------------------------------------------------------------------
