#include "slidershow.h"
#include "QDir"
#include <QTimer>
#include <QFile>
#include <QDebug>
#include <QTextStream>
#include "../PluginService/ksystemsetting_unit.h"
#include "../PluginService/ui_unit/xrandrobject.h"
#define RATIO_1 0.6
namespace KInstaller {
using namespace KServer;
SlideShow::SlideShow(QWidget *parent) : QWidget(parent),
    m_currentIndex(0)
{
    fileInfo();
    initUI();

    timer = new QTimer(this);
    timer->setInterval(5000);

    initAllConnect();
    addStyleSheet();
    start();
    this->setFocus();
}

int SlideShow::fileInfo()
{
    QDir *dir = new QDir(":/data/png/slidershow/");
    QStringList filter;
    filter << "*.png";
    dir->setNameFilters(filter);

    QFileInfoList fileInfoList = dir->entryInfoList(filter);
    num =fileInfoList.count() / 2;
    return num ;

}

void SlideShow::loadSlides()
{
    sliderWidget = new QStackedWidget();
    QRect rect = KServer::GetScreenRect();

    for (int i = 1; i <= num; i++) {
        Slide *slide = new Slide(this);
         slide->setObjectName("slide_"+QString::number(i));
//      slide->hide();
        if(KServer::ReadSettingIni("config", "language") == "en_US") {
            QImage image(QString(":/data/png/slidershow/%1e.png").arg(i));
            image = image.scaledToHeight(rect.height() * RATIO_1, Qt::SmoothTransformation);
            slide->setPixmap(QPixmap::fromImage(image));
            slide->setScaledContents(true);
        } else {
            QImage image(QString(":/data/png/slidershow/%1.png").arg(i));
            image = image.scaledToHeight(rect.height() * RATIO_1, Qt::SmoothTransformation);
            slide->setPixmap(QPixmap::fromImage(image));
            slide->setScaledContents(true);
        }
         sliderWidget->addWidget(slide);
    }
    sliderWidget->setCurrentIndex(m_currentIndex);

}

void SlideShow::initUI()
{
    QHBoxLayout* hlayout = new QHBoxLayout(this);
    this->setLayout(hlayout);

    m_prev = new QPushButton(this);
    m_prev->setObjectName("prev");
    m_prev->installEventFilter(this);

    m_prev->setIcon(QIcon(":/data/png/slidershow/prev.svg"));
//    m_prev->setIconSize(QSize(16, 16));
    m_prev->setStyleSheet(QLatin1String("QPushButton{border: none;outline: none;color: white;padding:16px 16px 16px 16px;background:transparent;border:none;}"
                                        "QPushButton:hover{"
                                        "width: 48px;height: 48px;background: rgba(255, 255, 255, 0.35);border-radius: 6px;}"
                                        " QPushButton:checked{ "
                                        " height: 48px;background: rgba(255, 255, 255, 0.25); border-radius: 6px;}"
                                        "QPushButton:pressed{height: 48px;background: rgba(255, 255, 255, 0.25); border-radius: 6px;}"
                                        "QPushButton:foucs{height: 48px;background: rgba(255, 255, 255, 0.25); border-radius: 6px;}"));
    hlayout->addWidget(m_prev, Qt::AlignCenter);
    loadSlides();

    hlayout->addWidget(sliderWidget);
    m_next = new QPushButton(this);
    m_next->setObjectName("next");
    m_next->installEventFilter(this);
    m_next->setIcon(QIcon(":/data/png/slidershow/next.svg"));
//    m_next->setIconSize(QSize(16, 16));
    m_next->setStyleSheet(QLatin1String("QPushButton{border: none;outline: none;color: white;padding:16px 16px 16px 16px;background:transparent;border:none;}"
                                        "QPushButton:hover{"
                                        "width: 48px;height: 48px;background: rgba(255, 255, 255, 0.35);border-radius: 6px;}"
                                        " QPushButton:checked{ "
                                        " height: 48px;background: rgba(255, 255, 255, 0.25); border-radius: 6px;}"
                                        "QPushButton:pressed{height: 48px;background: rgba(255, 255, 255, 0.25); border-radius: 6px;}"
                                        "QPushButton:foucs{height: 48px;background: rgba(255, 255, 255, 0.25); border-radius: 6px;}"));
    hlayout->addWidget(m_next, Qt::AlignCenter);


}

void SlideShow::initAllConnect()
{
    connect(timer, &QTimer::timeout, this, &SlideShow::next);
    connect(m_prev, &QPushButton::clicked, this, &SlideShow::prev);
    connect(m_next, &QPushButton::clicked, this, &SlideShow::next);
}

void SlideShow::addStyleSheet()
{
    QFile file(":/data/qss/InstallingFrame.css");
    file.open(QFile::ReadOnly);
    QTextStream filetext(&file);
    QString stylesheet = filetext.readAll();
    file.close();

    this->setStyleSheet(stylesheet);
}

void SlideShow::changeSlide(int index)
{
    if (index > 0) {
        m_currentIndex++;
        if (m_currentIndex > sliderWidget->count() -1)
            m_currentIndex = 0;
        sliderWidget->setCurrentIndex(m_currentIndex);

    } else if (index < 0 ) {
        m_currentIndex--;
        if (m_currentIndex < 0) {
            m_currentIndex = sliderWidget->count() - 1;
        }
        sliderWidget->setCurrentIndex(m_currentIndex) ;
    }

}

void SlideShow::keyPressEvent(QKeyEvent *event)
{
    qDebug() << Q_FUNC_INFO << "keyPressEvent";
    if(event->key() == Qt::Key_Left) {
        prev();
    } else if(event->key() == Qt::Key_Right) {
        next();
    } else if(event->key() == Qt::Key_Escape || event->key() == Qt::Key_Control) {
        this->setFocus();
    } else
        QWidget::keyPressEvent(event);
}

bool SlideShow::eventFilter(QObject *watched, QEvent *event)
{
    if(event->type() == QEvent::KeyPress) {
        QKeyEvent *ev = (QKeyEvent*) event;
        switch (ev->key()) {
        case Qt::Key_Left:
            prev();
            break;
        case Qt::Key_Right:
            next();
            break;
        }
        return true;
    }
    return false;
}

void SlideShow::next()
{
    timer->stop();
    changeSlide(1);
    timer->start();
}

void SlideShow::prev()
{
    timer->stop();
    changeSlide(-1);
    timer->start();
}

void SlideShow::start()
{
    changeSlide(0);
    timer->start();
}

}
