#include "agreementwindow.h"
#include <QSettings>
#include <QVBoxLayout>
#include <QTextCodec>
#include <QDebug>
#include <QScrollBar>

#define CONFIG_FILE "/usr/share/ukui-greeter/ukui-greeter.conf"

#define CENTER_WIDTH   960
#define CENTER_HEIGHT  722
#define ENSURE_BUTTON_HEIGHT 48
#define BORDER_WIDTH 40
AgreementWindow::AgreementWindow(bool hideTitle,QString title,QString text,QWidget *parent)
    : m_hideTitle(hideTitle)
    , m_title(title)
    , m_text(text)
    ,QWidget(parent)
{
    initUI();
}

void AgreementWindow::initUI()
{
    centerWidget = new QWidget(this);
    centerWidget->setObjectName("centerWidget");
   // centerWidget->setFixedSize(960,744);
    centerWidget->setStyleSheet("#centerWidget{background-color: rgba(0,0,0,15%);border-radius: 12px;}");

    QVBoxLayout *layout = new QVBoxLayout(centerWidget);


    ensureBtn = new QPushButton(this);
    ensureBtn->setText(tr("I know"));
    ensureBtn->setObjectName("ensureBtn");
    ensureBtn->setFixedSize(240,48);
    ensureBtn->setDefault(true);
    ensureBtn->setFocusPolicy(Qt::StrongFocus);
    ensureBtn->setStyleSheet("#ensureBtn{background-color:rgba(55, 144, 250, 1);border-radius: 6px;}");
    ensureBtn->setStyleSheet("QPushButton{text-align:center;background-color:#3790FA;border-radius: 6px;color:white} \
			      QPushButton::hover{background-color:#3489EE;} \
			      QPushButton::pressed {background-color:#2C73C8;}");
    connect(ensureBtn, &QPushButton::clicked, this, &AgreementWindow::switchPage);

    titleLbl = new QLabel(centerWidget);
    titleLbl->setText(m_title);
    titleLbl->setObjectName("titleLbl");
    titleLbl->setStyleSheet("#titleLbl{color: white;font-size: 24px;}");

    browser = new QTextBrowser(centerWidget);
    browser->setObjectName("browser");
    browser->setAttribute(Qt::WA_TranslucentBackground);
    browser->setStyleSheet("#browser{background-color:transparent;border-radius: none;color: white;font-size: 16px;}");
    browser->verticalScrollBar()->setProperty("drawScrollBarGroove", false);
    browser->setContextMenuPolicy(Qt::NoContextMenu);
    browser->verticalScrollBar()->setContextMenuPolicy(Qt::NoContextMenu);

    setFocusProxy(ensureBtn);

    browser->setText(m_text);

    layout->addSpacing(24);
    layout->addWidget(titleLbl);
    layout->addWidget(browser);
    layout->setAlignment(ensureBtn,Qt::AlignCenter);
    layout->setAlignment(titleLbl,Qt::AlignCenter);

    if(m_hideTitle)
        titleLbl->hide();
}

void AgreementWindow::switchPage()
{
    emit switchToGreeter();
}

void AgreementWindow::resizeEvent(QResizeEvent *event)
{
    int m_width = CENTER_WIDTH,m_height = CENTER_HEIGHT-ENSURE_BUTTON_HEIGHT-BORDER_WIDTH;
    if(this->width() < (CENTER_WIDTH + 2*BORDER_WIDTH))
        m_width = (width() - BORDER_WIDTH)/2;
    if(this->height() < (CENTER_HEIGHT+2*BORDER_WIDTH))
        m_height = (height() - BORDER_WIDTH)/2;

    if (centerWidget) {
        centerWidget->setGeometry((width()-m_width)/2,(height()-m_height)/2,m_width,m_height);

        if (ensureBtn)
            ensureBtn->move((width()-ensureBtn->width())/2,centerWidget->geometry().bottom() + 40);
    }
}
