#include <QGuiApplication>
#include <QScreen>
#include <QWidget>
#include <QMenuBar>
#include <QFileDialog>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>

#include <QDebug>

#include "Experiment.hpp"


Experiment::Experiment ():QMainWindow()
{
  createActions ();
  createMenus ();

  setWindowTitle (tr ("DSL - Draw Strength Line"));

  QScreen *screen = QGuiApplication::primaryScreen ();
  QRect screenGeometry = screen->geometry ();
  screenWidth = screenGeometry.width () - 200;
  screenHeight = screenGeometry.height () - 200;

  QWidget *central= new QWidget(this);
  setCentralWidget (central);
  setContentsMargins(0, 0, 0, 0);

  QVBoxLayout *layoutV = new QVBoxLayout(central);
  layoutV->setSpacing(0);
  layoutV->setContentsMargins(0, 0, 0, 0);
  central->setLayout(layoutV);

  // First line of the interface
  QHBoxLayout *layoutH1 = new QHBoxLayout(central);
  layoutH1->setSpacing(0);
  layoutH1->setContentsMargins(0, 0, 0, 0);
  layoutV->addLayout(layoutH1);
  
  waLeft = new WorkArea (((screenWidth-spaceInBetween)/2), screenHeight, central);
  waLeft->setContentsMargins(0, 0, 0, 0);
  layoutH1->addWidget(waLeft);

  QWidget *w1SpaceInBetween = new QWidget(central);
  w1SpaceInBetween->setContentsMargins(0, 0, 0, 0);
  w1SpaceInBetween->setFixedSize(spaceInBetween,0);
  layoutH1->addWidget(w1SpaceInBetween);
  
  waRight = new WorkArea (((screenWidth-spaceInBetween)/2), screenHeight, central);
  waRight->setContentsMargins(0, 0, 0, 0);
  layoutH1->addWidget(waRight);

  // second line of interface
  QWidget *hSpaceInBetween = new QWidget(central);
  hSpaceInBetween->setContentsMargins(0, 0, 0, 0);
  hSpaceInBetween->setFixedSize(0, spaceInBetween);
  layoutV->addWidget(hSpaceInBetween);

  // third line of interface
  QHBoxLayout *layoutH2 = new QHBoxLayout(central);
  layoutH2->setSpacing(0);
  layoutH2->setContentsMargins(0, 0, 0, 0);
  layoutV->addLayout(layoutH2);

  leftButton = new QPushButton("Image de gauche", central);
  leftButton->setFixedHeight(30);
  connect(leftButton, SIGNAL(clicked(bool)), this, SLOT(leftImage()));
  layoutH2->addWidget(leftButton);

  QWidget *w2SpaceInBetween = new QWidget();
  w2SpaceInBetween->setContentsMargins(0, 0, 0, 0);
  w2SpaceInBetween->setFixedSize(spaceInBetween,0);
  layoutH2->addWidget(w2SpaceInBetween);

  rightButton = new QPushButton("Image de droite", central);
  rightButton->setFixedHeight(30);
  connect(rightButton, SIGNAL(clicked(bool)), this, SLOT(rightImage()));
  layoutH2->addWidget(rightButton);

  // Fourth line of the interface
  progressBar = new QProgressBar(central);
  progressBar->setFixedHeight(20);
  progressBar->setRange(0, maxProgressBar);
  progressBar->setValue(0);
  connect(progressBar, SIGNAL(valueChanged(int)), this, SLOT(progressBarValueChanged(int)));
  layoutV->addWidget(progressBar);

  timer = new QTimer(central);
  timer->setSingleShot(false);
  timer->setInterval(time);
  connect(timer, SIGNAL(timeout()), this, SLOT(ticTimer()));
  
  setFixedSize (screenWidth, screenHeight);

  /*POUR TEST*/
  
  QString img_a_ouvrir("../img_test/img_test.jpg");
  QString sl1_a_ouvrir("../img_test/img_test.json");
  QString sl2_a_ouvrir("../img_test/img_test_2.json");

  openC( img_a_ouvrir, sl1_a_ouvrir, sl2_a_ouvrir);

  timer->start();
}

void
Experiment::open ()
{
  QString imageFilename = QFileDialog::getOpenFileName (this,
						   QObject::
						   tr ("Open image file"),
						   QDir::currentPath (),
						   QObject::
						   tr
						   ("Images files (*.jpg *.png);;All files (*.*)"));
  QString SLLeftFilename = QFileDialog::getOpenFileName (this,
						   QObject::
						   tr ("Open strength lines file"),
						   QDir::currentPath (),
						   QObject::
						   tr
						   ("Text files (*.txt);;All files (*.*)"));
  QString SLRightFilename = QFileDialog::getOpenFileName (this,
						   QObject::
						   tr ("Open strength lines file"),
						   QDir::currentPath (),
						   QObject::
						   tr
						   ("Text files (*.txt);;All files (*.*)"));

  openC(imageFilename, SLLeftFilename, SLRightFilename);
}

void Experiment::leftImage(){
  qDebug() << "Clic sur image gauche" << Qt::endl;
  progressBar->setValue(0);
}

void Experiment::rightImage(){
  qDebug() << "Clic sur image droite" << Qt::endl;
  progressBar->setValue(0);
}

void Experiment::progressBarValueChanged(int value){
  if(value == maxProgressBar ){
    QApplication::quit();
  }
}

void Experiment::ticTimer(){
  progressBar->setValue(progressBar->value()+time);
}

void
Experiment::openC (QString image, QString leftSL, QString rightSL){
  waLeft->loadImage (image.toStdString ());
  waRight->loadImage (image.toStdString ());
  waLeft->loadSL(leftSL.toStdString());
  waRight->loadSL(rightSL.toStdString());
  waLeft->setReadOnly(true);
  waRight->setReadOnly(true);
  waLeft->repaint();
  waRight->repaint();
  setFixedSize (waLeft->geometry().width()*2+spaceInBetween, waLeft->geometry().height()+waLeft->geometry().y()+spaceInBetween+leftButton->geometry().height()+22); // +22 ??!!
}

void
Experiment::createActions ()
{
  openAct = new QAction (tr ("&Open image"), this);
  openAct->setShortcuts (QKeySequence::Open);
  connect (openAct, &QAction::triggered, this, &Experiment::open);

  exitAct = new QAction (tr ("E&xit"), this);
  exitAct->setShortcuts (QKeySequence::Quit);
  connect (exitAct, &QAction::triggered, this, &QWidget::close);
}

void
Experiment::createMenus ()
{
  fileMenu = menuBar ()->addMenu (tr ("&File"));
  fileMenu->addAction (openAct);

  fileMenu->addSeparator ();

  fileMenu->addAction (exitAct);
}
