#include "thumbnailwidget.h"
#include <ukui-log4qt.h>
#include <gsettings.hpp>
#include <kysdk/applications/gsettingmonitor.h>
#include <CImg.h>
#include "include/theme.h"

int ThumbnailWidget::scanPictureCount = 0;
bool ThumbnailWidget::scanPictureIsEmpty = true;
ThumbnailWidget::ThumbnailWidget(QWidget *parent) : QListView(parent)
{
    setupGui();
    initConnect();
    initSettings();
    themeChange();

}
void ThumbnailWidget::setupGui()
{
    setAutoFillBackground(true);
    setBackgroundRole(QPalette::Base);

    bool isPCMode = GlobalUserSignal::getInstance()->getCurrentMode();
    if(isPCMode){
        this->setFixedWidth(ThumbnailWidgetMinimumWidth);
    }else{
        this->setFixedWidth(ThumbnailWidgetTabletMinimumWidth);
    }

    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    this->verticalScrollBar()->setProperty("drawScrollBarGroove",false);


    setDragEnabled(false);
    setSpacing(8);
    setEditTriggers(QAbstractItemView::NoEditTriggers);
    setIconSize(ThumbnailIconSize);
    setViewMode(QListView::IconMode);
    setMovement(QListWidget::Static);

    setAcceptDrops(false);
    setFocusPolicy(Qt::NoFocus);
    setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
    this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
}

void ThumbnailWidget::initConnect()
{

    connect(g_user_signal, &GlobalUserSignal::scanThreadFinishedImageLoadSignal, this, &ThumbnailWidget::showNormalImagesAfterScan);
    connect(this, &ThumbnailWidget::clicked, this, &ThumbnailWidget::clickedItemSlot);
    connect(kdk::GsettingMonitor::getInstance(), &kdk::GsettingMonitor::systemThemeChange, this, &ThumbnailWidget::themeChange);

}

void ThumbnailWidget::themeChange()
{

}

bool ThumbnailWidget::isDarkTheme()
{
    QString systemTheme = kdk::GsettingMonitor::getSystemTheme().toString();

    if (systemTheme == STYLE_NAME_KEY_DARK || systemTheme == STYLE_NAME_KEY_BLACK) {
        return true;
    } else {
        return false;
    }
}

void ThumbnailWidget::resizeEvent(QResizeEvent *event)
{
    bool isPCMode = GlobalUserSignal::getInstance()->getCurrentMode();
    if(isPCMode){
        this->setFixedWidth(ThumbnailWidgetMinimumWidth);
    }else{
        this->setFixedWidth(ThumbnailWidgetTabletMinimumWidth);
    }
    QListView::resizeEvent(event);
}

void ThumbnailWidget::initSettings()
{
    showThumbnailIcon();
}


void ThumbnailWidget::showThumbnailIcon()
{
    m_thumbnailDelegate = new ThumbnailDelegate();
    this->setItemDelegate(m_thumbnailDelegate);

    m_thumbnailItemModel = new QStandardItemModel();

}

void ThumbnailWidget::showNormalImageAfterScan(QString loadPath, QString savePath)
{
    Q_UNUSED(savePath)

    QString scanFullName = loadPath;
    if (g_config_signal->m_kylinScannerImageDebug) {
        scanFullName = g_config_signal->m_openFileName;
    }
    QFileInfo scanFullNameFileInfo(scanFullName);

    QString tooltipName = scanFullNameFileInfo.baseName();

    int rowcount = m_thumbnailItemModel->rowCount();
    KyInfo() << "rowcount = " << rowcount
             << "scanFullName = " << scanFullName;
    QString newScanFullName = QFileInfo(scanFullName).absolutePath() + "/" +  QFileInfo(scanFullName).baseName() + ".jpg";

    cimg_library::CImg<unsigned char> img(scanFullName.toLocal8Bit().data());
    img.save_jpeg(newScanFullName.toLocal8Bit().data());
    QPixmap iconPix(newScanFullName);

    QPixmap fitpixmap = iconPix.scaled(QSize(40, 40), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);

    qDebug() << "image mesg:" << fitpixmap;
    ScanStandardItem *item = new ScanStandardItem();
    item->setIcon(QIcon(fitpixmap));
    item->setToolTip(tooltipName);
    item->setPath(scanFullName);
    item->setRowCountLocation(rowcount);
    m_thumbnailItemModel->insertRow(rowcount, item);

    // Default choose index
    QModelIndex index = m_thumbnailItemModel->index(rowcount, 0);
    this->setCurrentIndex(index);

    scanPictureCount = rowcount;

    this->setModel(m_thumbnailItemModel);

}

void ThumbnailWidget::showNormalImagesAfterScan(QStringList loadPathes, QStringList savepathes)
{
    for(int i = 0; i < loadPathes.length(); i++){
        showNormalImageAfterScan(loadPathes[i], savepathes[i]);
    }
}

void ThumbnailWidget::clickedItemSlot(const QModelIndex &index)
{
    if(g_sane_object->cropFlag == 1){
        g_user_signal->cancleCorpSignal();
    }

     ScanStandardItem *item = dynamic_cast<ScanStandardItem *>(m_thumbnailItemModel->itemFromIndex(index));
     if (item) {
         int scanIconNumber = item->getRowCountLocation();
         QString loadPath = item->getPath();

         KyInfo() << "scanIconPath = " << scanIconNumber << loadPath;

        g_user_signal->showImageAfterClickedThumbnail(loadPath);
        g_user_signal->resetNavigatorsignal();
     }
     g_user_signal->exitWindowWithSaveFlag = false;
}


void ScanStandardItem::setPath(const QString &path)
{
    m_path = path;
}

QString ScanStandardItem::getPath()
{
    return m_path;
}

int ScanStandardItem::getRowCountLocation() const
{
    return rowCountLocation;
}

void ScanStandardItem::setRowCountLocation(int value)
{
    rowCountLocation = value;
}
