#include "mainviewerwindow.h"
#include "ui_mainviewerwindow.h"
#include <QDir>
#include <QLabel>
#include <QPropertyAnimation>
#include <QDesktopWidget>
#include <QMouseEvent>

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

MainViewerWindow::MainViewerWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainViewerWindow)
{
    // define available screen area for this main window
    QRect availableGeometry = qApp->desktop()->availableGeometry();
    ui->setupUi(this);
    this->resize(availableGeometry.size());

    // connects for navigation between views
    connect(ui->listWidget, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(showImageView(QListWidgetItem*)));
    connect(ui->backButton, SIGNAL(clicked()), this, SLOT(showFileView()));

    // take ownership of toolbar created with Qt Designer
    // and set it up
    m_toolbar = ui->toolbar;
    m_toolbar->setParent(ui->imageLabel);
    m_toolbar->show();
    m_toolbarOnScreen = true;
    updateToolbarPosition();

    // define toolbar y movement positions for animation
    TOOLBAR_Y_SHOWN = 0;
    TOOLBAR_Y_HIDDEN = -m_toolbar->height();

    // start app with file view on screen
    updateFileList();
    ui->stackedWidget->setCurrentIndex(Fileview);

    // create animation class for transition
    m_transition = new QPropertyAnimation(ui->imageLabel, "pos", this);

    // create animation class for toolbar
    m_ani = new QPropertyAnimation(m_toolbar, "pos", this);
}

void MainViewerWindow::mousePressEvent(QMouseEvent* /*event*/)
{
    m_ani->setDuration(500);

    if(m_toolbarOnScreen)
    {
        m_ani->setEndValue(QPoint(m_toolbar->pos().x(), TOOLBAR_Y_HIDDEN));
        m_ani->setEasingCurve(QEasingCurve::InBack);
        m_toolbarOnScreen = false;
    }
    else
    {
        m_ani->setEndValue(QPoint(m_toolbar->pos().x(), TOOLBAR_Y_SHOWN));
        m_ani->setEasingCurve(QEasingCurve::OutBack);
        m_toolbarOnScreen = true;
    }
    m_ani->start();
}

void MainViewerWindow::showFileView()
{
    ui->stackedWidget->setCurrentIndex(Fileview);
}

void MainViewerWindow::updateFileList()
{
    QDir dir;
    dir.cd(":/img");

    QFileInfoList list = dir.entryInfoList();
    for (int i = 0; i < list.size(); ++i) {
        QFileInfo fileInfo = list.at(i);
        ui->listWidget->addItem(fileInfo.fileName());
    }
}

void MainViewerWindow::showImageView(QListWidgetItem *item)
{
    // set selected image for image label
    QPixmap pix(":/img/"+item->text());
    ui->imageLabel->setPixmap(pix);

    // change to image view
    ui->stackedWidget->setCurrentIndex(Imageview);

    // create animation to slide the image to view
    QRect pixRect = QRect(QPoint(0, 0), ui->imageLabel->size());
    pixRect.moveCenter(QPoint( width()/2, 0 ));

    m_transition->setDuration(200);
    m_transition->setStartValue(QPoint(-(ui->imageLabel->width()), ui->imageLabel->y()));
    m_transition->setEndValue( QPoint(pixRect.x(), ui->imageLabel->y()) );
    m_transition->setEasingCurve(QEasingCurve::OutQuad);
    m_transition->start();
}

void MainViewerWindow::resizeEvent(QResizeEvent */*event*/)
{
    updateToolbarPosition();
}

void MainViewerWindow::updateToolbarPosition()
{
    // center the toolbar
    QRect toolbarRect;
    toolbarRect.setSize(m_toolbar->size());
    toolbarRect.moveCenter(QPoint(this->width()/2, TOOLBAR_Y_SHOWN+m_toolbar->height()/2));
    m_toolbar->setGeometry(toolbarRect);
}
