#include "imageviewer.h"
#include "ui_imageviewer.h"
#include <QWheelEvent>
#include <QDebug>

ImageViewer::ImageViewer(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::ImageViewer)
{
    ui->setupUi(this);

    EventFilterizer* interceptor = new EventFilterizer;
    ui->centralwidget->installEventFilter(interceptor);

    connect(interceptor, SIGNAL(onMove(int,int)),
            this, SLOT(move(int,int)));

    connect(interceptor, SIGNAL(onZoom(int)),
            this, SLOT(zoom(int)));
}

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

void ImageViewer::setImage(KonaImage *Ki) { // TODO: (no todo but a notice: This function requires the image to be available locally when called!)
    //ki = Ki; //TODO: <--

    disconnect(Ki, SIGNAL(imageDownloaded(KonaImage*,QString)),
                this, SLOT(setImage(KonaImage*)));

    currentID = Ki->getId();

    pix.load(Ki->getLocalImagePath());


    // We're doing this a bit complicated. (It's to remove any frame from the label)
    QPixmap temp = pix.scaled(this->width(), this->height(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
    ui->imgLabel->setGeometry(0,0,this->width(), this->height());

    ui->imgLabel->setPixmap(temp);

    ui->imgLabel->setGeometry((ui->imgLabel->width()-temp.width()) / 2,
                              (ui->imgLabel->height()-temp.height()) / 2,
                              temp.width(), temp.height());

    setWindowTitle(tr("KonaClient - Image viewer: %1 (%2x%3)").arg(QString::number(Ki->getId()),QString::number(Ki->getWidth()),QString::number(Ki->getHeight())));
}

void ImageViewer::showLoading() {
    ui->imgLabel->setText("Loading image...");
    setWindowTitle(tr("KonaClient - Image viewer"));
    ui->imgLabel->setGeometry(0,0,this->width(),this->height());
}

void ImageViewer::move(int dx, int dy) {
    ui->imgLabel->setGeometry(ui->imgLabel->x() + dx, ui->imgLabel->y() + dy, ui->imgLabel->width(),ui->imgLabel->height());
    checkPos();
}

void ImageViewer::zoom(int mode) {
    if (mode == ZOOM_OUT) {
        ui->imgLabel->setGeometry(ui->imgLabel->x() + (pix.width()/20)/2,
                                  ui->imgLabel->y() + (pix.height()/20)/2,
                                  ui->imgLabel->width() - pix.width()/20,
                                  ui->imgLabel->height() - pix.height()/20);
    }
    else if (mode == ZOOM_IN) {
        ui->imgLabel->setGeometry(ui->imgLabel->x() - (pix.width()/20)/2,
                                  ui->imgLabel->y() - (pix.height()/20)/2,
                                  ui->imgLabel->width() + pix.width()/20,
                                  ui->imgLabel->height() + pix.height()/20);
    }

    checkPos();
    ui->imgLabel->setPixmap(pix.scaled(ui->imgLabel->width(), ui->imgLabel->height(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
}

void ImageViewer::checkPos() {
    if (ui->imgLabel->width() < this->width()) {
        if (ui->imgLabel->x() < 0) {
            ui->imgLabel->setGeometry(0,
                                      ui->imgLabel->y(),
                                      ui->imgLabel->width(),
                                      ui->imgLabel->height());
        }
        else if (ui->imgLabel->x() + ui->imgLabel->width() > this->width()) {
            ui->imgLabel->setGeometry(this->width() - ui->imgLabel->width(),
                                      ui->imgLabel->y(),
                                      ui->imgLabel->width(),
                                      ui->imgLabel->height());
        }
    }
    else {
       // if (ui->imgLabel->x() )
    }

    if (ui->imgLabel->height() < this->height()) {
        if (ui->imgLabel->y() < 0) {
            ui->imgLabel->setGeometry(ui->imgLabel->x(),
                                      0,
                                      ui->imgLabel->width(),
                                      ui->imgLabel->height());

        }
        else if (ui->imgLabel->y() + ui->imgLabel->height() > this->height()) {
            ui->imgLabel->setGeometry(ui->imgLabel->x(),
                                      this->height() - ui->imgLabel->height(),
                                      ui->imgLabel->width(),
                                      ui->imgLabel->height());
        }
    }
    else {
        if (ui->imgLabel->y() + ui->imgLabel->height() < this->height()) {
            ui->imgLabel->setGeometry(ui->imgLabel->x(),
                                      this->height() - ui->imgLabel->height(),
                                      ui->imgLabel->width(),
                                      ui->imgLabel->height());
        }
        else if (ui->imgLabel->y() > 0) {
            ui->imgLabel->setGeometry(ui->imgLabel->x(),
                                      0,
                                      ui->imgLabel->width(),
                                      ui->imgLabel->height());
        }

        if (ui->imgLabel->x() + ui->imgLabel->width() < this->width()) {
            ui->imgLabel->setGeometry(this->width() - ui->imgLabel->width(),
                                      ui->imgLabel->y(),
                                      ui->imgLabel->width(),
                                      ui->imgLabel->height());
        }
        else if (ui->imgLabel->x() > 0) {
            ui->imgLabel->setGeometry(0,
                                      ui->imgLabel->y(),
                                      ui->imgLabel->width(),
                                      ui->imgLabel->height());
        }
    }
}

void ImageViewer::keyPressEvent(QKeyEvent *e) {
    if (e->key() == Qt::Key_Left || e->key() == Qt::Key_Backspace || e->key() == Qt::Key_Shift) {
        emit loadPrev();
    }
    else if (e->key() == Qt::Key_Right || e->key() == Qt::Key_Return || e->key() == Qt::Key_Control) {
        emit loadNext();
    }
}






// MOUSE EVENT
EventFilterizer::EventFilterizer(QObject *parent) :
    QObject(parent)
{
}

bool EventFilterizer::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type() == QEvent::Wheel) {
        QWheelEvent *wheelEvent = static_cast< QWheelEvent * >( event );
        if (wheelEvent->delta() > 0) {
            emit onZoom(ZOOM_IN);
        }
        else if (wheelEvent->delta() < 0) {
            emit onZoom(ZOOM_OUT);
        }

        return true;
    }
    else if (event->type() == QEvent::MouseButtonPress) {
        QMouseEvent *mouseEvent = static_cast< QMouseEvent * >( event );

        lastX = mouseEvent->x();
        lastY = mouseEvent->y();
        return true;
    }
    else if (event->type() == QEvent::MouseMove) { // ONLY CALLED WHEN PRESSED
        //qDebug() << "MOVE";
        QMouseEvent *mouseEvent = static_cast< QMouseEvent * >( event );

        emit onMove(mouseEvent->x() - lastX,
                  mouseEvent->y() - lastY);


        lastX = mouseEvent->x();
        lastY = mouseEvent->y();
        return true;
    }
    else
    {
        // standard event processing
        return QObject::eventFilter(obj, event);
    }
}
