#include <biwak/linux/boardgui.h>
#include "ui_boardgui.h"
#include <QCheckBox>
#include <QSpinBox>
#include <QPushButton>
#include <QLabel>
#include <QSlider>
#include <QSpinBox>
#include <lepto/log.h>

CBoardGui* pBoardGui=nullptr;


CBoardGui::CBoardGui(QWidget *parent) :
   QMainWindow(parent),
   ui(new Ui::CBoardGui)
{
   QGridLayout *layout;
   ui->setupUi(this);
   pBoardGui = this;
   
   // Check that all groupboxes have a valid layout object.
   layout=dynamic_cast<QGridLayout*>( ui->groupOutput->layout() );
   lAssert(layout);
   layout=dynamic_cast<QGridLayout*>( ui->groupInput->layout() );
   lAssert(layout);
   layout=dynamic_cast<QGridLayout*>( ui->groupPwm->layout() );
   lAssert(layout);
   layout=dynamic_cast<QGridLayout*>( ui->groupDisplay->layout() );
   lAssert(layout);

   /*
   pixmapGraphicDisplay=new QPixmap( 128, 128);
   pixmapGraphicDisplay->fill( Qt::black );
   ui->graphicDisplay->setPixmap( *pixmapGraphicDisplay );
   */
}


CBoardGui::~CBoardGui()
{
   delete ui;
}


CBoardGui *CBoardGui::instance() /* instance */
{
   return( pBoardGui );
}


Ui::CBoardGui *CBoardGui::uiInstance() /* static */
{
   if(!pBoardGui)
   {
      return nullptr;
   }
   return( pBoardGui->getUi() );
}


QAbstractButton *CBoardGui::createOutput()
{
   QGridLayout* layout=dynamic_cast<QGridLayout*>( ui->groupOutput->layout() );
   Q_ASSERT(layout);

   QAbstractButton* button = new QCheckBox(ui->groupOutput);
   button->setObjectName(QStringLiteral("inX"));
   layout->addWidget( button, layout->rowCount(), 0, 1, 1);
   return( button );
}


QAbstractButton *CBoardGui::createInput()
{
   QGridLayout* layout=dynamic_cast<QGridLayout*>( ui->groupInput->layout() );
   Q_ASSERT(layout);
   
   QAbstractButton* button = new QPushButton(ui->groupOutput);
   button->setObjectName(QStringLiteral("outX"));
   button->setCheckable(true);
   layout->addWidget( button, layout->rowCount(), 0, 1, 1);
   return( button );
}


QAbstractSlider *CBoardGui::createPwm(const char* labelText)
{
   QGridLayout* layout=dynamic_cast<QGridLayout*>( ui->groupPwm->layout() );
   Q_ASSERT(layout);
   
   QAbstractSlider* slider = new QSlider(Qt::Orientation::Vertical, ui->groupPwm);
   QLabel* label = new QLabel( ui->groupPwm );
   slider->setObjectName(QStringLiteral("pwmX"));
   int column=layout->columnCount();
   layout->addWidget( slider, 0,column, 1, 1);
   layout->addWidget( label, 1,column, 1, 1);
   label->setText(labelText);
   return( slider );
}


QSpinBox *CBoardGui::createTimer(const char* labelText)
{
   QGridLayout* layout=dynamic_cast<QGridLayout*>( ui->groupTimer->layout() );
   Q_ASSERT(layout);
   
   QSpinBox* spinBox = new QSpinBox( ui->groupTimer );
   QLabel* label = new QLabel( ui->groupTimer );
   spinBox->setObjectName(QStringLiteral("timerX"));
   int row=layout->rowCount();
   layout->addWidget( spinBox, row,0, 1, 1);
   layout->addWidget( label, row,1, 1, 1);
   label->setText(labelText);
   return( spinBox );
}


QLabel *CBoardGui::createDisplay()
{
   QGridLayout* layout=dynamic_cast<QGridLayout*>( ui->groupDisplay->layout() );
   Q_ASSERT(layout);
   
   QLabel* display = new QLabel( ui->groupDisplay );
   display->setObjectName(QStringLiteral("displayX"));
   int row=layout->rowCount();
   layout->addWidget( display, row,0, 1, 1);
   
   return( display );
}


void CBoardGui::slotExited()
{
   lInfo ( "doExit!" );
   //doExit=true;
   
   //QCoreApplication::exit does not exit when QCoreApplication::exec was not used
   exit(0);
}

Ui::CBoardGui *CBoardGui::getUi()
{
   return(ui);
}


void CBoardGui::handleInput(int extiIndex, int state)
{
   /*
   callInterrupt( (EInterrupt)((int)eInterruptExti0 + extiIndex) );
   //lHint << "Calling interrupt "  << (int)eInterruptExti0 + BIWAK_INPUT_START + input;
   CExti *that=CExti::getThat(extiIndex);
   lHint << "That: " << (address_t)that;
   lHint << "this: " << (address_t)this;
   CInput *input=dynamic_cast <CInput *>(that);
   CButton *button=dynamic_cast <CButton *>(that);

   if(!input)
      lHint << "Found the Source!";
   else
      lHint << "Did not find the Source!";

   if(!input)
      lHint << "Found the Source (Button)!";
   else
      lHint << "Did not find the Source (Button)!";
   */
   lInfo ( "Emit %d/%d", extiIndex, state);

//#if defined(emit) && emit+0 == 0
//#error EMPTY!!!
//#endif
   emit signalInputChanged( extiIndex, state );

   return;
}


/*
QLabel *CBoardGui::getGraphicDispay()
{
   return ( ui->graphicDisplay );
}


QPixmap *CBoardGui::getGraphicDispayPixmap()
{
   return ( pixmapGraphicDisplay );
}
*/

//----------------------------------------------------------------------------
