#include "failedpagewidget.h"
#include <ukui-log4qt.h>
#include <kysdk/applications/gsettingmonitor.h>

#include <QPainter>
#include <QStyleOption>
#include <kysdk/applications/gsettingmonitor.h>
#include "include/theme.h"

FailedPageWidget::FailedPageWidget(QWidget *parent) :
    QWidget(parent),
    m_failedPageIcon(new QLabel(this)),
    m_failedPageText(new QLabel(this)),
    m_failedPageButton(new QPushButton(this)),
    m_failedPageVLayout(new QVBoxLayout(this))
{
    setupGui();
    initConnect();
    initTheme();

}
void FailedPageWidget::initTheme(){
    if (isDarkTheme()) {
        themeColor = "black";
        m_failedPageIcon->setPixmap(QPixmap(":/default-connect-page/detect_fail_dark.svg").scaled(FailedPageIconSize));

    } else {
        themeColor = "white";
        m_failedPageIcon->setPixmap(QPixmap(":/default-connect-page/detect_fail_light.svg").scaled(FailedPageIconSize));
    }
}
bool FailedPageWidget::isDarkTheme()
{
    QString systemTheme = kdk::GsettingMonitor::getSystemTheme().toString();
    if (systemTheme == STYLE_NAME_KEY_DARK || systemTheme == STYLE_NAME_KEY_BLACK) {
        return true;
    } else {
        return false;
    }
}

void FailedPageWidget::setupGui()
{
    m_failedPageIcon->setFixedSize(FailedPageIconSize);

    m_failedPageText->setEnabled(false);
    m_failedPageText->setText(tr("No available scan devices"));

    m_failedPageButton->setProperty("isImportant",true);
    m_failedPageButton->setMinimumSize(FailedPageButtonSize);
    bool isPCMode = GlobalUserSignal::getInstance()->getCurrentMode();
    rotateChangedSlot(isPCMode);
    m_failedPageButton->setText(tr("Connect"));
    m_failedPageButton->adjustSize();
    m_failedPageButton->setFocusPolicy(Qt::StrongFocus);
    m_failedPageButton->setFocus();
    m_failedPageButton->setDefault(true);

    m_failedPageVLayout->setSpacing(0);
    m_failedPageVLayout->addStretch();
    m_failedPageVLayout->addWidget(m_failedPageIcon, 0,  Qt::AlignCenter);
    m_failedPageVLayout->addSpacing(13);
    m_failedPageVLayout->addWidget(m_failedPageText, 0,  Qt::AlignCenter);
    m_failedPageVLayout->addSpacing(29);
    m_failedPageVLayout->addWidget(m_failedPageButton, 0,  Qt::AlignCenter);
    m_failedPageVLayout->addStretch();
    m_failedPageVLayout->setContentsMargins(0, 0, 0, 0);

    setLayout(m_failedPageVLayout);
}

void FailedPageWidget::initConnect()
{
    connect(m_failedPageButton, &QPushButton::clicked, this, [=](){
        emit scanButtonClicked();
    });
    connect(kdk::GsettingMonitor::getInstance(), &kdk::GsettingMonitor::systemFontSizeChange,this,&FailedPageWidget::fontSizeChanged);
    connect(kdk::GsettingMonitor::getInstance(), &kdk::GsettingMonitor::systemThemeChange, this, &FailedPageWidget::initTheme);
    connect(GlobalUserSignal::getInstance(),&GlobalUserSignal::rotationChangedSig,this,&FailedPageWidget::rotateChangedSlot);
}

void FailedPageWidget::fontSizeChanged()
{
    float systemFontSize = kdk::GsettingMonitor::getSystemFontSize().toFloat();
    QFont font;
    font.setPointSize(systemFontSize);
    m_failedPageButton->setFont(font);
}

void FailedPageWidget::clickfiledPageButton()
{
    m_failedPageButton->click();
}

void FailedPageWidget::rotateChangedSlot(bool isPCMode){
    if(isPCMode){
        m_failedPageButton->setMinimumSize(FailedPageButtonSize);
    }else{
        m_failedPageButton->setMinimumSize(FailedPageButtonSizePADMODE);
    }
}
