//----------------------------------------------------------------------------
///
/// \file   biwak.cpp
///
/// \brief  stuff for x86
///
/// \date   20151026
/// \author Maximilian Seesslen <mes@seesslen.net>
///
//----------------------------------------------------------------------------


#if 1

#include <biwak/biwak.h>
#include "arbiter.h"
#include <time.h>               // clock()
#include <QApplication>
#include <QMainWindow>

#include <biwak/log_private.h>
#include <biwak/heartbeat.hpp>
#include <biwak/linux/boardgui.h>

CArbiter *pArbiter;


int getSystemTicker()
{
   return clock();
}


int param_argc=1;
//class MainWindow *bw=NULL;
//class qtplatform* bw=NULL;
/*
 * The UI belongs to biwak, not platform. The platform hosts any elements but
 * the elements dot know anything about the platform.
 */
char *param_argv[]=
{
    (char *)"/usr/bin/bash", (char *)"help", NULL
};


// Some bringup tests use the end of the heap to store data surviving an reset.
// Just let the code be happy.
uint32_t __heap_end[10];

//qtplatform *pQtplatform=NULL;
//QApplication a(param_argc, param_argv);

//QApplication* pApplication=nullptr;
//CBoardGui* pBoardGui=nullptr;

/*
void MainWindow::eventLoop()
{
   //a.processEvents();
   a.exec();
}
*/
/*
class CBiwak
{
      QApplication a;
      //QWidget *w=NULL;
      QMainWindow *w;

public:
      CBiwak()
         :a(param_argc, param_argv)
      {
         w=new QMainWindow(NULL); //QWidget(NULL);
         w->show();

         pPlatformUi=new Ui_CBoardUi;
         pPlatformUi->setupUi(w);
      }
      void eventLoop()
      {
         a.processEvents();
      }
};
*/


void biwakInit()
{
   // The GUI needs an QApplication, QCoreApplication not sufficient.
   // UI/QApplication can not be instantiated when neither DISPLAY is present
   // and platform is not "offscreen".
   // Either set qt platform to "offscreen" or just do not instantiate GUI when
   // DISPLAY is not present.
   QString s=qgetenv("DISPLAY");
   if( s.isEmpty() )
   {
      // even CBoardGui is not instanciated, some applications may create a
      // QMainWindow object which will crash without DISPLAY. QMainWindow also
      // needs a full QCoreApplication.
      // Give QCoreApplication a new try when CANDis::main_qt is liquidated.
      qputenv("QT_QPA_PLATFORM", "offscreen");
      new QApplication(param_argc, param_argv);
   }
   else
   {
      new QApplication(param_argc, param_argv);
      new CBoardGui;
      CBoardGui::instance()->show();
   }
   
   return;
}


void biwakInitNoClock()
{
   biwakInit();
}


void biwakEventLoop()
{
   QCoreApplication::processEvents();

   logEventLoop();
   
   #if IS_ENABLED( CONFIG_BIWAK_LOG_HEARTBEAT )
      // Keep heartbeats eventloop happy
      logHeartBeat( ELogCategory::Loop | 0 );
   #endif

   return;
}

void biwakPrintClockInfo()
{
}

void biwakPrintMemoryBaseInfo()
{
}

void biwakPrintMemoryInfo()
{
}

/*
void checkBootLoader()
{
   return;
}
*/

void setStdio(CIOStream *io)
{
   // Does not make much sense in linux? The printf should end up in a Qt widget
};


void systemReset()
{
   // Its a linux system
   exit(0);
}


int resetCounter()
{
   return(0);
}


void clearResetCounter()
{
   
}


#endif


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