#include "slidershow.h"
#include "QDir"
#include <QTimer>
#include <QFile>
#include <QDebug>
#include <QTextStream>
#include "../PluginService/ui_unit/xrandrobject.h"
#include "../PluginService/ksystemsetting_unit.h"
#include "../PluginService/ui_unit/xrandrobject.h"
#include <QPainter>
#define RATIO_1 0.6

#define IMG_PATH  ":/data/png/slidershow/openkylin-slideshow"

namespace KInstaller {
using namespace KServer;
SlideShow::SlideShow(QWidget *parent) : QWidget(parent),
    m_currentIndex(0)
{
    initUI();
    timer = new QTimer(this);
    timer->setInterval(5000);

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

QImage SlideShow::addtransterstr(QImage &image, QRect rct, QString str,QFont font,QColor color)
{

    QPainter painter(&image);
    painter.setCompositionMode(QPainter::CompositionMode_SourceOut);

    QPen pen = painter.pen();
    pen.setColor(color);
    painter.setPen(pen);
    painter.setFont(font);
    painter.drawText(rct, Qt::AlignHCenter|Qt::TextWordWrap, str);
    return image;
}

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


    QFont ft1,ft2;
    ft1.setFamily(QStringLiteral("bold"));
    ft1.setBold(true);
    ft1.setPointSize(24);

    ft2.setFamily(QStringLiteral("bold"));
    ft2.setBold(true);
    ft2.setPointSize(12);

    //载入背景图
    QDir *dir = new QDir(IMG_PATH);
    QStringList filter;
    filter << "*.png";
    dir->setNameFilters(filter);

    QFileInfoList fileInfoList = dir->entryInfoList(filter);
    int num =fileInfoList.count();

    for (int i = 1; i <= num; i++) {
        Slide *slide = new Slide(this);
         slide->setObjectName("slide_"+QString::number(i));
//      slide->hide();
         QString titlestr="";
         switch (i) {
            case 1:titlestr=tr("Mainstream hardware platforms @ Support Intel, AMD, RISC-V and other hardware platforms");break;//主流硬件平台@支持Intel，AMD，RISC-V，Raspberry Pi等硬件平台
            case 2:titlestr=tr("Innovation and stability @ Based on linux 6.1+5.15 dual kernel, including cutting-edge technologies");break;//创新与稳定@基于Linux 6.1+5.15双内核，矢志创新，兼顾稳定
            case 3:titlestr=tr("Independent selection and upgrading of core components @ Complete independent selection and upgrading of more than 20 core components");break;//核心组件自主选型升级@完成20+核心组件自主选型升级，摆脱桎梏，“源”梦铸“根”
            case 4:titlestr=tr("UKUI Desktop environment optimization @ Smoother PC/tablet PC interaction experience, fashionable and concise interface");break;//UKUI桌面环境优化@更顺畅的PC/平板交互体验，界面时尚简洁
            case 5:titlestr=tr("Integrated intelligent voice assistant @ Enable intelligent interaction, voice input can evoke system applications");break;//集成智能语音助手@开启智能交互，语音输入即可唤起系统应用
            case 6:titlestr=tr("Expanding the Win application ecosystem @ Launching dozens of Win applications in the software store to meet users daily usage needs");break;//拓展Win应用生态@软件商店上架数十款Win应用，满足用户日常使用需求
         default:
             break;
         }

    QImage image(QString(IMG_PATH).append("/%1.png").arg(i));

    //统一标准，将图片缩放至固定大小便于写字
    image = image.scaledToHeight(500, Qt::SmoothTransformation);
    QRect rect1,rect2;
    rect1=QRect(0,10,image.width(),50);
    rect2=QRect(0,60,image.width(),30);

    image = addtransterstr(image, rect1,titlestr.section('@',0,0),ft1,QColor(220,220,220));
    image = addtransterstr(image, rect2,titlestr.section('@',1,1),ft2,QColor(220,220,220));

    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();
}

}
