#include "konapreviewimageview.h"
#include "ui_konapreviewimageview.h"

#include <QDebug>

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

    ui->imgLabel->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    // ui->imgLabel->setPixmap(QPixmap ("/home/markus/Bilder/temp.jpg"));

    imgReloadCounter = 0;
}

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

void KonaPreviewImageView::OpenImage()
{
    emit imageClicked(ki);
}

void KonaPreviewImageView::RMB() {
    qDebug() << "RMB";
}

void KonaPreviewImageView::downloadImage()
{
    emit buttonClicked(ki);
}

void KonaPreviewImageView::previewDownloaded(KonaImage *Ki, QString localPath) {
    ki = Ki;

    QPixmap pix (localPath);
    if (pix.width() > 0) {
        pix = pix.scaled(200,150, Qt::KeepAspectRatio, Qt::SmoothTransformation);
        ui->imgLabel->setPixmap(pix);
    }
    else {
        imgReloadCounter++;
        if (imgReloadCounter < 3) {
            qDebug() << "ERROR: KonaPreviewImageView: Corrupt image. Reload #" << imgReloadCounter;
            ki->DownloadPreview(true);
        }
    }
}

void KonaPreviewImageView::setCaption(QString text) {
    ui->btnResolution->setText(text);
}
