#include <QGuiApplication>
#include <QScreen>
#include <QWidget>
#include <QMenuBar>
#include <QFileDialog>
#include <QUrl>
#include <QFileInfo>

#include <QDebug>

#include "MainWindow.hpp"


MainWindow::MainWindow ()
{
  createActions ();
  createMenus ();

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

  QScreen *screen = QGuiApplication::primaryScreen ();
  QRect screenGeometry = screen->geometry ();
  int screenWidth = screenGeometry.width () - 200;
  int screenHeight = screenGeometry.height () - 200;
  
  wa = new WorkArea (screenWidth, screenHeight, this);
  
  setCentralWidget (wa);

  setFixedSize (screenWidth, screenHeight);
}

void
MainWindow::openFile ()
{
  filename = QFileDialog::getOpenFileName (this,
						   QObject::
						   tr ("Open image file"),
						   QDir::currentPath (),
						   QObject::tr("Images files (*.jpg *.png);;All files (*.*)"));
  QDir::setCurrent(QString(filename).remove(QUrl(filename).fileName()));
  wa->loadImage (filename.toStdString ());
  wa->repaint();
  setFixedSize (wa->geometry().width(), wa->geometry().height()+wa->geometry().y());
}

void
MainWindow::save ()
{
  QString fileToSave = QFileDialog::getSaveFileName (this,
						   QObject::tr ("Save file"),
						   QDir::currentPath ().append("/").append(QFileInfo(filename).baseName()).append(".json"),
						   QObject::tr("JSON file (*.json);;All files (*.*)"));
  if(!fileToSave.endsWith(".json"))
    fileToSave.append(".json");
  wa->saveStrengthLine (fileToSave.toStdString ());
}

void
MainWindow::firstImage ()
{
  qDebug() << "First image" << Qt::endl;
}

void
MainWindow::previousImage ()
{
  qDebug() << "Previous image" << Qt::endl;
}

void
MainWindow::nextImage ()
{
  qDebug() << "Next image" << Qt::endl;
}

void
MainWindow::lastImage ()
{
  qDebug() << "Last image" << Qt::endl;
}


void
MainWindow::createActions ()
{
  openFileAct = new QAction (tr ("&Open image"), this);
  openFileAct->setShortcuts (QKeySequence::Open);
  connect (openFileAct, &QAction::triggered, this, &MainWindow::openFile);

  saveAct = new QAction (tr ("&Save"), this);
  saveAct->setShortcuts (QKeySequence::Save);
  connect (saveAct, &QAction::triggered, this, &MainWindow::save);

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

  firstImageAct = new QAction (tr ("First"), this);
  firstImageAct->setShortcuts (QKeySequence::MoveToPreviousLine);
  connect (firstImageAct, &QAction::triggered, this, &MainWindow::firstImage);

  previousImageAct = new QAction (tr ("Previous"), this);
  previousImageAct->setShortcuts (QKeySequence::MoveToPreviousChar);
  connect (previousImageAct, &QAction::triggered, this, &MainWindow::previousImage);
  
  nextImageAct = new QAction (tr ("Next"), this);
  nextImageAct->setShortcuts (QKeySequence::MoveToNextChar);
  connect (nextImageAct, &QAction::triggered, this, &MainWindow::nextImage);

  lastImageAct = new QAction (tr ("Last"), this);
  lastImageAct->setShortcuts (QKeySequence::MoveToNextLine);
  connect (lastImageAct, &QAction::triggered, this, &MainWindow::lastImage);
}

void
MainWindow::createMenus ()
{
  fileMenu = menuBar ()->addMenu (tr ("&File"));
  fileMenu->addAction (openFileAct);
  fileMenu->addAction (saveAct);

  fileMenu->addSeparator ();

  fileMenu->addAction (exitAct);

  imageMenu = menuBar ()->addMenu (tr ("&Image"));
  imageMenu->addAction (firstImageAct);
  imageMenu->addAction (previousImageAct);
  imageMenu->addAction (nextImageAct);
  imageMenu->addAction (lastImageAct);
}
