#include "test_ui.h"
#include <QApplication>
testUi::testUi()
{
  QFont font;
  font.setPointSize(12);
  QWidget *centralWidget = new QWidget();
  layoutVMain = new QVBoxLayout(centralWidget);
  layoutVMain->setObjectName(QString::fromUtf8("mainVerticalLayout"));
  layoutVMain->setContentsMargins(0, 0, 0, 0);

  gridButtons = new QGridLayout();
  gridButtons->setObjectName(QString::fromUtf8("buttonsGrid"));
  gridButtons->setContentsMargins(0, 0, 0, 0);
  layoutVMain->addLayout(gridButtons);

  gridScicam = new QGridLayout();
  gridScicam->setObjectName(QString::fromUtf8("scicamGrid"));
  gridScicam->setContentsMargins(0, 0, 0, 0);
  layoutVMain->addLayout(gridScicam);



  labInit = new QLabel;
  labInit->setObjectName(QString::fromUtf8("labInit"));
  labInit->setFont(font);
  labInit->setText(QString::fromUtf8("Send Init"));
  gridButtons->addWidget(labInit, 1, 1, 1, 1);

  btnInit = new QPushButton;
  btnInit->setObjectName(QString::fromUtf8("btnInit"));
  btnInit->setFont(font);
  btnInit->setText(QString::fromUtf8("Init"));
  gridButtons->addWidget(btnInit, 1, 2, 1, 1);

  labSetup = new QLabel;
  labSetup->setObjectName(QString::fromUtf8("labSetup"));
  labSetup->setFont(font);
  labSetup->setText(QString::fromUtf8("Send setup"));
  gridButtons->addWidget(labSetup, 2, 1, 1, 1);

  btnSetup = new QPushButton;
  btnSetup->setObjectName(QString::fromUtf8("btnSetup"));
  btnSetup->setFont(font);
  btnSetup->setText(QString::fromUtf8("Setup"));
  gridButtons->addWidget(btnSetup, 2, 2, 1, 1);

  labStop = new QLabel;
  labStop->setObjectName(QString::fromUtf8("labStop"));
  labStop->setFont(font);
  labStop->setText(QString::fromUtf8("Send stop"));
  gridButtons->addWidget(labStop, 3, 1, 1, 1);

  btnStop = new QPushButton;
  btnStop->setObjectName(QString::fromUtf8("btnStop"));
  btnStop->setFont(font);
  btnStop->setText(QString::fromUtf8("Stop"));
  gridButtons->addWidget(btnStop, 3, 2, 1, 1);

  labAbort = new QLabel;
  labAbort->setObjectName(QString::fromUtf8("labAbort"));
  labAbort->setFont(font);
  labAbort->setText(QString::fromUtf8("Send abort"));
  gridButtons->addWidget(labAbort, 4, 1, 1, 1);

  btnAbort = new QPushButton;
  btnAbort->setObjectName(QString::fromUtf8("btnAbort"));
  QFont fontAlert;
  fontAlert.setPointSize(12);
  fontAlert.setBold(true);
  btnAbort->setFont(fontAlert);
  btnAbort->setStyleSheet(QString::fromUtf8("QPushButton { color: red }"));
  btnAbort->setText(QString::fromUtf8("ABORT"));
  gridButtons->addWidget(btnAbort, 4, 2, 1, 1);

  labMotion = new QLabel;
  labMotion->setObjectName(QString::fromUtf8("labMotion"));
  labMotion->setFont(font);
  labMotion->setText(QString::fromUtf8("Call Motion Ctrl GUI"));
  gridButtons->addWidget(labMotion, 5, 1, 1, 1);

  btnMotion = new QPushButton;
  btnMotion->setObjectName(QString::fromUtf8("btnMotion"));
  btnMotion->setFont(font);
  btnMotion->setText(QString::fromUtf8("MotionCtrl GUI"));
  gridButtons->addWidget(btnMotion, 5, 2, 1, 1);


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

  labExpStatus = new QLabel;
  labExpStatus->setObjectName(QString::fromUtf8("labExpStatus"));
  labExpStatus->setFont(font);
  labExpStatus->setText(QString::fromUtf8("Exposure Status"));
  gridScicam->addWidget(labExpStatus, 1, 1, 1, 1);

  labExpStatusValue = new QLabel;
  labExpStatusValue->setObjectName(QString::fromUtf8("labExpStatusValue"));
  labExpStatusValue->setFont(fontAlert);
  labExpStatusValue->setStyleSheet(QString::fromUtf8("QLabel { color: red }"));
  labExpStatusValue->setText(QString::fromUtf8("Undefined"));
  labExpStatusValue->setFrameShape(QFrame::StyledPanel);
  labExpStatusValue->setFrameShadow(QFrame::Sunken);
  gridScicam->addWidget(labExpStatusValue, 1, 2, 1, 2);

  labScicamTest = new QLabel;
  labScicamTest->setObjectName(QString::fromUtf8("labScicamTest"));
  labScicamTest->setFont(font);
  labScicamTest->setText(QString::fromUtf8("SCICAM"));
  gridScicam->addWidget(labScicamTest, 2, 1, 1, 1);

  btnScicamTest = new QPushButton;
  btnScicamTest->setObjectName(QString::fromUtf8("btnScicamTest"));
  btnScicamTest->setFont(font);
  btnScicamTest->setText(QString::fromUtf8("ScicamTest"));
  gridScicam->addWidget(btnScicamTest, 2, 2, 1, 1);


  setCentralWidget(centralWidget);
  connect(btnInit,  SIGNAL(clicked()), this, SLOT(on_init_button_clicked()));
  connect(btnSetup, SIGNAL(clicked()), this, SLOT(on_setup_button_clicked()));
  connect(btnStop, SIGNAL(clicked()), this, SLOT(on_stop_button_clicked()));
  connect(btnAbort, SIGNAL(clicked()), this, SLOT(on_abort_button_clicked()));
  connect(btnScicamTest, SIGNAL(clicked()), this, SLOT(on_scicam_button_clicked()));
  connect(btnMotion, SIGNAL(clicked()), this, SLOT(on_motion_button_clicked()));
  createActions();
  createMenus();

}

void testUi::on_init_button_clicked()
{
  QProcess procPyInit;
  this->labInit->setText(QString::fromUtf8("Clicked!"));
  procPyInit.start("python ../homeSHINS.py");
  procPyInit.waitForFinished(-1);
  this->labInit->setText(QString::fromUtf8("Done!"));
}

void testUi::on_setup_button_clicked()
{
  QProcess procPySetup;
  this->labSetup->setText(QString::fromUtf8("Clicked!"));
  procPySetup.startDetached("python ../setupSHINS.py");
  // procPySetup.waitForFinished(-1);
  this->labSetup->setText(QString::fromUtf8("Done!"));
}

void testUi::on_stop_button_clicked()
{
  QProcess procPyStop;
  this->labStop->setText(QString::fromUtf8("Clicked!"));
  procPyStop.startDetached("python ../stopSHINS.py");
  // procPyStop.waitForFinished(-1);
  this->labStop->setText(QString::fromUtf8("Done!"));
}

void testUi::on_abort_button_clicked()
{
  QProcess procPyAbort;
  std::cout << "abort clicked" << std::endl;
  this->labSetup->setText(QString::fromUtf8("Clicked!"));
  procPyAbort.start("python ../abortSHINS.py");
  procPyAbort.waitForFinished(-1);
  QString p_stdout = procPyAbort.readAllStandardOutput();

  // this->labAbort->setText(p_stdout);
  this->labAbort->setText(QString::fromUtf8("Done!!"));
}

void testUi::on_motion_button_clicked()
{
  QProcess procMotionGUI;
  this->labMotion->setText(QString::fromUtf8("Clicked!"));
  QString program = "niceQUI";
  QStringList arguments;
  arguments << "--MOTION_CTRL.CONFIG:Endpoint=[NAME=motion_ctrl-svr-DUMMY,TCP=localhost:60200]";
  // procMotionGUI.startDetached("niceQUI --MOTION_CTRL.CONFIG:Endpoint=[NAME=motion_ctrl-svr-DUMMY,TCP=localhost:60200]");
  procMotionGUI.start(program, arguments);
  procMotionGUI.waitForStarted();
  // procMotionGUI.waitForFinished(-1);
  this->labMotion->setText(QString::fromUtf8("Done!"));
}

void testUi::on_scicam_button_clicked()
{
  int timeout = 30000; // msec
  QProcess procPyScicamTest;
  this->labScicamTest->setText(QString::fromUtf8("Clicked!"));
  procPyScicamTest.start("python ../scicamTestSHINS.py");
  procPyScicamTest.waitForFinished(timeout);
  this->labScicamTest->setText(QString::fromUtf8("Done!"));
}

void testUi::createActions()
{
  actionClose = new QAction(QString::fromUtf8("Close"), this);
  actionClose->setShortcuts(QKeySequence::Quit);
  actionClose->setStatusTip(tr("Exit the application"));
  connect(actionClose, SIGNAL(triggered()), this, SLOT(close()));


  actionOpenEngPanel = new QAction(QString::fromUtf8("Engineering Panel"), this);
  actionSetInstMode = new QAction(QString::fromUtf8("Instrument Mode"), this);
}

void testUi::createMenus()
{
  menuFile = menuBar()->addMenu("&File");
  menuFile->addAction(actionClose);

  menuEng = menuBar()->addMenu(QString::fromUtf8("Engineering"));
  menuEng->addAction(actionOpenEngPanel);

  menuInstrument = menuBar()->addMenu(QString::fromUtf8("Instrument"));
  menuInstrument->addAction(actionSetInstMode);
}

int main(int argc, char **args)
{
  //Q_INIT_RESOURCE(application);
  //Q_INIT_RESOURCE(resources);

  QApplication app(argc, args);
  app.setOrganizationName("inaf");
  app.setApplicationName("SHINS");

  testUi mainUi;

  mainUi.show();

  return app.exec();
}
