#include "clMobileLogin.h"
#include "ui_clMobileLogin.h"

#include "CAppInfo.h"
#include "CQREncode.h"

#include <QGraphicsDropShadowEffect>
#include <QByteArray>
#include "logproc.h"

clMobileLogin::clMobileLogin(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::clMobileLogin)
{
    ui->setupUi(this);

    setStyleSheet(g_appInfoPtr->m_sQssStr);
}

clMobileLogin::~clMobileLogin()
{
    delete ui;
}

void clMobileLogin::setUI(const int nLeft, const int nTop, const int nWidth, const int nHeight)
{
    setGeometry(nLeft, nTop, nWidth, nHeight);
    ui->widget_npw_BG->setGeometry(g_appInfoPtr->m_fRate*5, g_appInfoPtr->m_fRate*5, width()-g_appInfoPtr->m_fRate*10, height()-g_appInfoPtr->m_fRate*10);
    ui->btn_downloadApp->setGeometry(g_appInfoPtr->m_fRate*20, g_appInfoPtr->m_fRate*20,
                                     g_appInfoPtr->m_fRate*100, g_appInfoPtr->m_fRate*32);
    ui->btn_bindUser->setGeometry(ui->btn_downloadApp->x() + ui->btn_downloadApp->width(),
                                  ui->btn_downloadApp->y(), ui->btn_downloadApp->width(), ui->btn_downloadApp->height());
    ui->label_qrcode->setGeometry((ui->widget_npw_BG->width() - g_appInfoPtr->m_fRate*164)/2, ui->btn_downloadApp->y() + ui->btn_downloadApp->height() + g_appInfoPtr->m_fRate*20,
                                  g_appInfoPtr->m_fRate*164, g_appInfoPtr->m_fRate*164);
    ui->label_useAppScan->adjustSize();
    ui->label_useAppScan->setGeometry((ui->widget_npw_BG->width() - ui->label_useAppScan->width())/2,
                                      ui->label_qrcode->y() + ui->label_qrcode->height() + g_appInfoPtr->m_fRate*10,
                                      ui->label_useAppScan->width(), ui->label_useAppScan->height());

    QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect;
    effect->setOffset(g_appInfoPtr->m_fRate*4,g_appInfoPtr->m_fRate*4);
    effect->setColor(QColor(0,0,0,30));
    effect->setBlurRadius(g_appInfoPtr->m_fRate*16);
    ui->widget_npw_BG->setGraphicsEffect(effect);

    on_btn_downloadApp_clicked();
}

void clMobileLogin::setBtnDown(ML_BTN_TYPE type)
{
	QString sDownLoadAppStyle = QString(R"(QPushButton
                                {
                                    outline:none;
                                    background:rgba(%1,1);
                                    font-size:%2px;
                                    font-family:"Microsoft YaHei";
                                    font-weight:400;
                                    color:rgba(%3,1);
                                    border-top-left-radius:%4px;
                                    border-bottom-left-radius:%5px;
                                })");
	QString sDownLoadAppNormalStyle = sDownLoadAppStyle.arg("244,243,247").arg((int)(g_appInfoPtr->m_fRate*12))
                                .arg("102, 102, 102")
								.arg((int)(g_appInfoPtr->m_fRate*6))
                                .arg((int)(g_appInfoPtr->m_fRate*6));
	QString sDownLoadAppCheckStyle = sDownLoadAppStyle.arg("19,187,138").arg((int)(g_appInfoPtr->m_fRate * 12))
		.arg("255,255,255")
		.arg((int)(g_appInfoPtr->m_fRate * 6))
		.arg((int)(g_appInfoPtr->m_fRate * 6));

	QString sBindUserStyle = QString(R"(QPushButton
                                {
                                    outline:none;
                                    background:rgba(%1,1);
                                    font-size:%2px;
                                    font-family:"Microsoft YaHei";
                                    font-weight:400;
                                    color:rgba(%3,1);
                                    border-top-right-radius:%4px;
                                    border-bottom-right-radius:%5px;
                                })");
	QString sBindUserNormalStyle = sBindUserStyle.arg("244,243,247").arg((int)(g_appInfoPtr->m_fRate * 12))
									.arg("102, 102, 102").arg((int)(g_appInfoPtr->m_fRate*6))
                                    .arg((int)(g_appInfoPtr->m_fRate*6));
	QString sBindUserCheckStyle = sBindUserStyle.arg("19,187,138").arg((int)(g_appInfoPtr->m_fRate * 12))
		.arg("255,255,255").arg((int)(g_appInfoPtr->m_fRate * 6))
		.arg((int)(g_appInfoPtr->m_fRate * 6));
    if(type == ML_BTN_TYPE::mbt_downloadApp)
    {
        ui->btn_downloadApp->setStyleSheet(sDownLoadAppCheckStyle);
        ui->btn_bindUser->setStyleSheet(sBindUserNormalStyle);
    }
    else
    {
        ui->btn_downloadApp->setStyleSheet(sDownLoadAppNormalStyle);
        ui->btn_bindUser->setStyleSheet(sBindUserCheckStyle);
    }

}

void clMobileLogin::on_btn_downloadApp_clicked()
{
    ui->label_qrcode->setPixmap(CQREncode::GenerateQRcode(g_appInfoPtr->m_sAppDownLoadUrl, ui->label_qrcode->width()));
    ui->label_useAppScan->setVisible(false);
    setBtnDown(ML_BTN_TYPE::mbt_downloadApp);
}

void clMobileLogin::on_btn_bindUser_clicked()
{    
    QString sUserInfo = QString::fromLocal8Bit("%1@%2").arg(g_appInfoPtr->m_sRootOrgId).arg(g_appInfoPtr->m_sRootOrgName);
    sUserInfo = QByteArray(sUserInfo.toUtf8()).toBase64();

    ui->label_qrcode->setPixmap(CQREncode::GenerateQRcode(sUserInfo, ui->label_qrcode->width()));
    ui->label_useAppScan->setVisible(true);
    setBtnDown(ML_BTN_TYPE::mbt_bindUser);
}
