#include "screenlockui.h"
#include <QImageReader>

ScreenlockUi::ScreenlockUi()
{
    setAttribute(Qt::WA_DeleteOnClose);
    uiLayout         = new QVBoxLayout(this);
    screenlockLayout = new QVBoxLayout(this);
    relateLayout     = new QVBoxLayout(this);
    uiLayout->addLayout(screenlockLayout);
    uiLayout->addLayout(relateLayout);
    uiLayout->addStretch();
    uiLayout->setSpacing(40);
    uiLayout->setMargin(0);
    initUi();
}

ScreenlockUi::~ScreenlockUi()
{

}

void ScreenlockUi::initUi()
{
    initScreenLock();
    initRelateSet();
    initSignals();
}

void ScreenlockUi::initScreenLock()
{
    wallpaperTitleLabel = new TitleLabel(this);
    previewFrame        = new UkccFrame(this);
    previewLayout       = new QHBoxLayout(previewFrame);
    previewLabel        = new QLabel(this);
    centerGroup         = new SettingGroup(this);
    //~ contents_path /Screenlock/Show picture of screenlock on screenlogin
    showPicWidget       = new SwitchWidget(tr("Show picture of screenlock on screenlogin"), centerGroup);
    picBtnFrame         = new UkccFrame(centerGroup);
    picBtnLayout        = new QVBoxLayout(picBtnFrame);
    pictureFrame        = new UkccFrame(picBtnFrame);
    pictureLayout       = new FlowLayout(pictureFrame, 16, -1, -1);
    bottomFrame         = new UkccFrame(picBtnFrame);
    bottomLayout        = new QHBoxLayout(bottomFrame);
    localBtn            = new QPushButton(bottomFrame);
    onlineBtn           = new QPushButton(bottomFrame);
    resetBtn            = new QPushButton(bottomFrame);

    screenlockLayout->setMargin(0);
    screenlockLayout->setSpacing(8);
    screenlockLayout->addWidget(wallpaperTitleLabel);
    screenlockLayout->addWidget(previewFrame);
    screenlockLayout->addWidget(centerGroup);

    //~ contents_path /Screenlock/Screenlock
    wallpaperTitleLabel->setText(tr("Screenlock"));
    previewFrame->setFixedHeight(212);
    previewLabel->setFixedSize(300, 180);
    previewLayout->addWidget(previewLabel);
    previewLayout->addStretch();

    centerGroup->addWidget(showPicWidget);
    centerGroup->addWidget(picBtnFrame);

    picBtnFrame->setLineWidth(0);
    pictureFrame->setLineWidth(0);
    picBtnLayout->setMargin(0);
    picBtnLayout->setSpacing(0);
    picBtnLayout->addWidget(pictureFrame);
    picBtnLayout->addWidget(bottomFrame);

    bottomLayout->setSpacing(32);
    bottomLayout->setContentsMargins(16, 0, 16, 16);
    bottomLayout->addWidget(localBtn);
    bottomLayout->addWidget(onlineBtn);
    bottomLayout->addStretch();
    bottomLayout->addWidget(resetBtn);

    //~ contents_path /Screenlock/Local Pictures
    localBtn->setText(tr("Local Pictures"));
    //~ contents_path /Screenlock/Online Pictures
    onlineBtn->setText(tr("Online Pictures"));
    //~ contents_path /Screenlock/Reset To Default
    resetBtn->setText(tr("Reset To Default"));

    onlineBtn->setFocusPolicy(Qt::NoFocus);
    onlineBtn->setContentsMargins(0, 0, 0, 0);
    onlineBtn->setCursor(QCursor(Qt::PointingHandCursor));
    onlineBtn->setStyleSheet("QPushButton{background: transparent;border-radius: 4px;text-decoration: underline;}");
}

void ScreenlockUi::initRelateSet()
{
    relateTitleLabel  = new TitleLabel(this);
    relatedGroup      = new SettingGroup(this);
    monitorWidget     = new PushButtonWidget(tr("Monitor Off"), relatedGroup);
    screensaverWidget = new PushButtonWidget(tr("Screensaver"), relatedGroup);

    relateTitleLabel->setText(tr("Related Settings"));
    relateLayout->setSpacing(8);
    relateLayout->addWidget(relateTitleLabel);
    relateLayout->addWidget(relatedGroup);
    relatedGroup->addWidget(monitorWidget);
    relatedGroup->addWidget(screensaverWidget);
    monitorWidget->setButtonText(tr("Set"));
    screensaverWidget->setButtonText(tr("Set"));
}

void ScreenlockUi::initSignals()
{
    connect(showPicWidget->switchButton(), &KSwitchButton::clicked, this, [=](){
        Q_EMIT showOnLoginChanged(showPicWidget->switchButton()->isChecked());
    });
    connect(monitorWidget, &PushButtonWidget::clicked, this, [=](){
        Q_EMIT toSetMonitor();
    });
    connect(screensaverWidget, &PushButtonWidget::clicked, this, [=](){
        Q_EMIT toSetScreensaver();
    });
    connect(localBtn, &QPushButton::clicked, this, [=](){
        Q_EMIT localButtonClicked();
    });
    connect(onlineBtn, &QPushButton::clicked, this, [=](){
        Q_EMIT onlineButtonClicked();
    });
    connect(resetBtn, &QPushButton::clicked, this, [=](){
        Q_EMIT resetButtonClicked();
    });
}

void ScreenlockUi::setShowOnLogin(bool b)
{
    showPicWidget->switchButton()->blockSignals(true);
    showPicWidget->switchButton()->setChecked(b);
    showPicWidget->switchButton()->blockSignals(false);
}

void ScreenlockUi::setPicture(const QString &data)
{
    currentFile = data;
    QImageReader reader(data);
    reader.setDecideFormatFromContent(true);
    previewLabel->setPixmap(QPixmap::fromImage(reader.read()).scaled(previewLabel->size()));
    resetClickedPic();
}

void ScreenlockUi::setPictures(const QStringList &picturePathList, const QStringList &sourcePathList)
{
    if (picturePathList.size() != sourcePathList.size()) {
        qWarning()<<"picturePathList.size():"<<picturePathList.size()<<" != sourcePathList.size():"<<sourcePathList.size();
        return;
    }

    for (int i = 0; i < picturePathList.size(); ++i) {
        QPixmap pixmap;
        pixmap.load(picturePathList.at(i));
        bool clicked = false;
        if (sourcePathList.at(i) == currentFile) {
            clicked = true;
        }
        createPictureUnit(pixmap, sourcePathList.at(i), clicked);
    }
}

void ScreenlockUi::createPictureUnit(const QPixmap &pixmap, const QString &filename, const bool &clicked)
{
    PictureUnit *picUnit = new PictureUnit(this);
    picUnit->setPixmap(pixmap);
    picUnit->setFilenameText(filename);
    if (clicked) {
        prePictureUnit = picUnit;
        picUnit->changeClickedFlag(true);
        setPicture(filename);
    }
    connect(picUnit, &PictureUnit::clicked, [=](QString file){
        if (prePictureUnit) {
            prePictureUnit->changeClickedFlag(false);
        }
        prePictureUnit = picUnit;
        setPicture(file);
        Q_EMIT pictureChanged(file);
    });
    pictureLayout->addWidget(picUnit);
}

void ScreenlockUi::resetClickedPic() {
    if (prePictureUnit != nullptr) {
        prePictureUnit->changeClickedFlag(false);
        prePictureUnit = nullptr;
    }
    for (int i = pictureLayout->count() - 1; i >= 0; --i) {
        QLayoutItem *it      = pictureLayout->itemAt(i);
        PictureUnit *picUnit = static_cast<PictureUnit*>(it->widget());
        if (currentFile == picUnit->filenameText()) {
            picUnit->changeClickedFlag(true);
            prePictureUnit = picUnit;
        }
    }
}
