#include "recoUiUnauthorized.h"
#include <QDebug>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QTime>
#include <QBitmap>
#include <QPainter>

recoUiUnauthorized::recoUiUnauthorized(QWidget *parent) : QWidget(parent)
{
    qDebug() << "recoUiUnauthorized()";

    //title part
    titleShow(LOGO_TIME);

    _label_Bottom = new QLabel();
    _label_Bottom->setObjectName("label_bottom");
    _label_Bottom->setStyleSheet("#label_bottom{background-color: #181f33;}");

    QFont ft;
    QPalette pa;

    m_label_msgUnahd = new QLabel();
    m_label_msgUnahd->setText(tr("软件未授权"));
    ft.setPointSize(72);
    m_label_msgUnahd->setFont(ft);
    m_label_msgUnahd->setObjectName("label_tip");
    m_label_msgUnahd->setStyleSheet("#label_tip{color:#FF9c00;}");
//    pa.setColor(QPalette::WindowText,Qt::yellow);
    m_label_msgUnahd->setPalette(pa);

    m_label_msg = new QLabel();
    m_label_msg->setText(tr("请联系平台管理员"));
    ft.setPointSize(25);
    m_label_msg->setFont(ft);
    pa.setColor(QPalette::WindowText,Qt::white);
    m_label_msg->setPalette(pa);

    m_btn_reCheck = new QPushButton();
    m_btn_reCheck->setText(tr("重新检测授权状态"));
    m_btn_reCheck->setObjectName("pushButton_reCheck");
    m_btn_reCheck->setStyleSheet("#pushButton_reCheck{color:#FFFFFF; background-color:#949eba;}");//font-size:25px;
    m_btn_reCheck->setMinimumHeight(30);
    m_btn_sysSet = new QPushButton();
    m_btn_sysSet->setText(tr("进入系统设置"));
    m_btn_sysSet->setObjectName("pushButton_sysSet");
    m_btn_sysSet->setStyleSheet("#pushButton_sysSet{color:#FFFFFF; background-color:#949eba;}");//font-size:25px;
    m_btn_sysSet->setMinimumHeight(30);

    QHBoxLayout* hbLayout_btn = new QHBoxLayout();
    hbLayout_btn->addWidget(m_btn_reCheck/*, 1,   Qt::AlignBaseline | Qt::AlignTop */);
    hbLayout_btn->addSpacing(10);
    hbLayout_btn->addWidget(m_btn_sysSet/*, 1, Qt::AlignBaseline | Qt::AlignTop   */);
    hbLayout_btn->setMargin(34);

    QVBoxLayout* vbLayout_msg = new QVBoxLayout();
    vbLayout_msg->addSpacing(72);
    vbLayout_msg->addWidget(m_label_msgUnahd, 3, /*Qt::AlignBottom |*/ Qt::AlignCenter);
    vbLayout_msg->addWidget(m_label_msg, 1, /*Qt::AlignBottom | */Qt::AlignCenter);
    vbLayout_msg->addLayout(hbLayout_btn, 3);
    vbLayout_msg->addSpacing(16);
    _label_Bottom->setLayout(vbLayout_msg);

    QHBoxLayout *pHLayoutLabel = new QHBoxLayout();
    pHLayoutLabel->addWidget(_label_Bottom);
    pHLayoutLabel->setMargin(0);
    
    QLabel *pLabelEmpty = new QLabel();

    QVBoxLayout* vbLayout = new QVBoxLayout();
    vbLayout->addLayout(_HLayoutTitle, 1);
    vbLayout->addWidget(pLabelEmpty, 6);
    vbLayout->addLayout(pHLayoutLabel, 3);
    vbLayout->setMargin(0);
//    vbLayout->addLayout(vbLayout_msg, 6);
//    vbLayout->addLayout(hbLayout_btn, 4);
//    vbLayout->setSpacing(80);
    setLayout(vbLayout);

    //floor two
    _widgetMainReco = new QWidget(this);
    _widgetMainReco->setMinimumSize(476, 800);
    m_label_iconUnahd = new QLabel();
    m_label_iconUnahd->setObjectName("label_iconUnahd");
    m_label_iconUnahd->setStyleSheet("#label_iconUnahd{min-width: 112px;min-height:112px;max-width: 112px;max-height: 112px;border-radius: 56px;\
                                  border-width: 0 0 0 0;background-color:#FF9c00;}");

    QPixmap pm(":/res/image/unauthorized.png");
    QPixmap fitpixmap_faceIcon=pm.scaled(68, 68, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    fitpixmap_faceIcon = PixmapToRound(fitpixmap_faceIcon,34);

    m_label_iconUnahd->setPixmap(fitpixmap_faceIcon);
    m_label_iconUnahd->setAlignment(Qt::AlignCenter);

    QLabel *pLabelTop = new QLabel();
    QLabel *pLabelBottom = new QLabel();

    QVBoxLayout* pVLayoutIcon = new QVBoxLayout();
    pVLayoutIcon->addWidget(pLabelTop, 5);
    pVLayoutIcon->addWidget(m_label_iconUnahd, 1, Qt::AlignHCenter);
    pVLayoutIcon->addWidget(pLabelBottom, 2);
    _widgetMainReco->setLayout(pVLayoutIcon);
}

recoUiUnauthorized::~recoUiUnauthorized()
{
    qDebug() << "~recoUiUnauthorized()";
}

void recoUiUnauthorized::titleShow(recoUiUnauthorized::TITLE_LAYOUT titleIndex)
{
    if(titleIndex > LOGO_TIME)
    {
        return;
    }

    //show logo
    if( titleIndex == LOGO_MID )
    {

    }
    else if(titleIndex == TIME_MID) //show time
    {

    }
    else
    {/*show logo and time*/
//       _WidgetTitle = new QWidget(_widgetMainReco);
       showRWLogo(":/res/image/ProjectSet_Intenet.png", NULL);
       showTime();

       _HLayoutTitle = new QHBoxLayout();
       _HLayoutTitle->addWidget(_labelRWIcon, 1,Qt::AlignCenter);
       _HLayoutTitle->addWidget(_labelRWLogo, 10, Qt::AlignLeft);
       _HLayoutTitle->addWidget(_labelTime, 1, Qt::AlignCenter);
    }
}

void recoUiUnauthorized::showRWLogo(QString icon, QString rw)
{
    //ICON
    QPixmap pm(icon);
    pm = pm.scaled(40, 40, Qt::KeepAspectRatio);

    _labelRWIcon = new QLabel();
    _labelRWIcon->setPixmap(pm);
    _labelRWIcon->setObjectName("label_icon");

    //LOGO
    _labelRWLogo = new QLabel();
    _labelRWLogo->setObjectName("label_logo");
    _labelRWLogo->setText(tr("瑞为技术\nRECONOVA"));
    _labelRWLogo->setStyleSheet("#label_logo{color:#FFFFFF;}");
}

void recoUiUnauthorized::showTime()
{
    QTime time = QTime::currentTime();
    QString timeStr = time.toString("hh:mm");
    _labelTime = new QLabel();
    _labelTime->setObjectName("label_time");
    _labelTime->setText(timeStr);
    _labelTime->setStyleSheet("#label_time{color:#FFFFFF;font-size:30px}");
}

void recoUiUnauthorized::showRecoInfo(QString name, QString id, QString part)
{
    QLabel *pLabelName = new QLabel(_widgetReco);
    QLabel *pLabelId = new QLabel(_widgetReco);
    QLabel *pLabelPart = new QLabel(_widgetReco);

    pLabelName->setObjectName("label_name");
    pLabelId->setObjectName("label_id");
    pLabelPart->setObjectName("label_part");

    pLabelName->setStyleSheet("#label_name{font-size:72px; color:#0ebcf8;}");
    pLabelId->setStyleSheet("#label_id{font-size:25px; color:#FFFFFF;}");
    pLabelPart->setStyleSheet("#label_part{font-size:25px; color:#FFFFFF;}");

    pLabelName->setText(tr(name.toUtf8()));
    pLabelId->setText(tr(id.toUtf8()));
    pLabelPart->setText(tr(part.toUtf8()));

    QVBoxLayout *pVLayoutInfo = new QVBoxLayout(_widgetReco);
    pVLayoutInfo->addWidget(pLabelName, 1, Qt::AlignCenter);
    pVLayoutInfo->addWidget(pLabelId, 1, Qt::AlignCenter);
    pVLayoutInfo->addWidget(pLabelPart, 1, Qt::AlignCenter);
    pVLayoutInfo->setMargin(nSpace);
}

QPixmap recoUiUnauthorized::PixmapToRound(QPixmap &src, int radius)
{
    if (src.isNull()) {
        return QPixmap();
    }

    QSize size(2*radius, 2*radius);
    QBitmap mask(size);
    QPainter painter(&mask);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setRenderHint(QPainter::SmoothPixmapTransform);
    painter.fillRect(0, 0, size.width(), size.height(), Qt::white);
    painter.setBrush(QColor(0, 0, 0));
    painter.drawRoundedRect(0, 0, size.width(), size.height(), 99, 99);

    QPixmap image = src.scaled(size);
    image.setMask(mask);
    return image;
}