#include "etTestResult.h"
#include "ui_etTestResult.h"

#include "CAppInfo.h"

#include <QScrollBar>
#include <QHBoxLayout>
#include <QLabel>

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

    setStyleSheet(g_appInfoPtr->m_sQssStr);

	ui->tablewt_envTest->setEditTriggers(QAbstractItemView::NoEditTriggers);
	//整行选中的方式
	ui->tablewt_envTest->setSelectionBehavior(QAbstractItemView::SelectRows);
	//设置为只能选中一行
	ui->tablewt_envTest->setSelectionMode(QAbstractItemView::SingleSelection);
	//隐藏列表头
	ui->tablewt_envTest->verticalHeader()->setVisible(false);
	//隐藏边框
	ui->tablewt_envTest->setShowGrid(false);
	//表头不高亮显示
	ui->tablewt_envTest->horizontalHeader()->setVisible(false);// ->setHighlightSections(false);
	//设置行数
	ui->tablewt_envTest->setRowCount(6);
	//设置列数
	ui->tablewt_envTest->setColumnCount(2);

	ui->tablewt_envTest->setHorizontalScrollMode(QTableWidget::ScrollPerPixel);
	ui->tablewt_envTest->horizontalScrollBar()->setSingleStep(g_appInfoPtr->m_fRate * 5);

	QStringList sHeadStr;
	sHeadStr << QString::fromLocal8Bit("检测项") <<
		QString::fromLocal8Bit("结果");

	ui->tablewt_envTest->setHorizontalHeaderLabels(sHeadStr);
	ui->tablewt_envTest->horizontalHeader()->setDefaultAlignment(Qt::AlignJustify | Qt::AlignVCenter);
	ui->tablewt_envTest->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);

	ui->tablewt_envTest->verticalHeader()->setDefaultSectionSize(g_appInfoPtr->m_fRate * 48);
	ui->tablewt_envTest->setColumnWidth(0, g_appInfoPtr->m_fRate * 425);
//	ui->tablewt_envTest->setColumnWidth(1, g_appInfoPtr->m_fRate * 170);

	ui->tablewt_envTest->horizontalHeader()->setStretchLastSection(true);

}

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

int etTestResult::setUI(const int nLeft, const int nTop, const int nWidth)
{
    setGeometry(nLeft, nTop, nWidth, g_appInfoPtr->m_fRate*146);
    ui->tablewt_envTest->setGeometry(g_appInfoPtr->m_fRate*30, 0, width() - g_appInfoPtr->m_fRate*30*2, g_appInfoPtr->m_fRate*293);
     
	QString sTextStyle = QString(R"(QLabel
								{
									font-size:%1px;
									font-family:"Microsoft YaHei";
									font-weight:400;
									color:rgba(153,153,153,1);
								})").arg((int)(g_appInfoPtr->m_fRate * 12));
	
	
	{
		int i = 0;
		QWidget *widget1 = new QWidget;
		QHBoxLayout *layout1 = new QHBoxLayout;
		widget1->setLayout(layout1);
		
		QLabel *icon1 = new QLabel;
		icon1->setStyleSheet(sTextStyle);
		icon1->setText(QString::fromLocal8Bit("检查项"));
		layout1->addWidget(icon1);
		layout1->addStretch();

		ui->tablewt_envTest->setCellWidget(i, 0, widget1);
		
		QWidget *widget = new QWidget;
		QHBoxLayout *layout = new QHBoxLayout;
		widget->setLayout(layout);
		QLabel *icon = new QLabel;
		icon->setStyleSheet(sTextStyle);
		icon->setText(QString::fromLocal8Bit("结果"));
		layout->addStretch();
		layout->addWidget(icon);
		

		ui->tablewt_envTest->setCellWidget(i, 1, widget);
	}

    ui->lw_ettr_unpass->setGeometry(ui->tablewt_envTest->x(), ui->tablewt_envTest->y() + ui->tablewt_envTest->height() + g_appInfoPtr->m_fRate*20,
                                    ui->tablewt_envTest->width(), ui->lw_ettr_unpass->count()*g_appInfoPtr->m_fRate*26);
	setGeometry(nLeft, nTop, nWidth, ui->lw_ettr_unpass->y() + ui->lw_ettr_unpass->height());
    return height();
}

int etTestResult::widgetHeight()
{
    return ui->lw_ettr_unpass->y() + ui->lw_ettr_unpass->height();
}


void etTestResult::setResult(int nNetWorkStatus, int nTimekStatus, int nCameraStatus, int nVoiceStatus, int nWXStatus)
{
	QString sTextStyle = QString(R"(QLabel
								{
									font-size:%1px;
									font-family:"Microsoft YaHei";
									font-weight:400;
									color:rgba(254,119,100,1);
								})").arg((int)(g_appInfoPtr->m_fRate*12));
    ui->lw_ettr_unpass->clear();
    ui->lw_ettr_unpass->setIconSize(QSize(g_appInfoPtr->m_fRate*16, g_appInfoPtr->m_fRate*16));
    int i = 1;
    {
        ui->tablewt_envTest->setItem(i, 0, new QTableWidgetItem(QString::fromLocal8Bit("网速")));
        QWidget *widget = new QWidget;
        QHBoxLayout *layout = new QHBoxLayout;
        widget->setLayout(layout);
        layout->addStretch();
        QLabel *icon = new QLabel;
        if(nNetWorkStatus == 1)
        {
            icon->setFixedSize(QSize(g_appInfoPtr->m_fRate*12, g_appInfoPtr->m_fRate*9));
            icon->setPixmap(QPixmap(":/images/icon-et-item-pass.png").scaled(g_appInfoPtr->m_fRate*12, g_appInfoPtr->m_fRate*9));
        }
        else
        {
            icon->setFixedSize(QSize(g_appInfoPtr->m_fRate*8, g_appInfoPtr->m_fRate*8));
            icon->setPixmap(QPixmap(":/images/icon-et-iten-unpass.png").scaled(g_appInfoPtr->m_fRate*8, g_appInfoPtr->m_fRate*8));
            QListWidgetItem *item = new QListWidgetItem(QIcon(QPixmap(":/images/icon-et-tips.png")), QString::fromLocal8Bit("检查网络是否连接，路由器是否正常工作。"));
            ui->lw_ettr_unpass->addItem(item);
			item->setSizeHint(QSize(item->sizeHint().width(), g_appInfoPtr->m_fRate*26));
        }
        layout->addWidget(icon);

        ui->tablewt_envTest->setCellWidget(i, 1, widget);
    }

    {
        i = 2;
        ui->tablewt_envTest->setItem(i, 0, new QTableWidgetItem(QString::fromLocal8Bit("时钟")));
        QWidget *widget = new QWidget;
        QHBoxLayout *layout = new QHBoxLayout;
        widget->setLayout(layout);
        layout->addStretch();
        QLabel *icon = new QLabel;
        if(nTimekStatus == 1)
        {
            icon->setFixedSize(QSize(g_appInfoPtr->m_fRate*12, g_appInfoPtr->m_fRate*9));
            icon->setPixmap(QPixmap(":/images/icon-et-item-pass.png").scaled(g_appInfoPtr->m_fRate*12, g_appInfoPtr->m_fRate*9));
        }
        else
        {
            icon->setFixedSize(QSize(g_appInfoPtr->m_fRate*8, g_appInfoPtr->m_fRate*8));
            icon->setPixmap(QPixmap(":/images/icon-et-iten-unpass.png").scaled(g_appInfoPtr->m_fRate*8, g_appInfoPtr->m_fRate*8));
            QListWidgetItem *item = new QListWidgetItem(QIcon(QPixmap(":/images/icon-et-tips.png")), QString::fromLocal8Bit("检查电脑时区及时钟是否正常。"));
            ui->lw_ettr_unpass->addItem(item);
			item->setSizeHint(QSize(item->sizeHint().width(), g_appInfoPtr->m_fRate * 26));
        }
        layout->addWidget(icon);

        ui->tablewt_envTest->setCellWidget(i, 1, widget);
    }

    {
        i = 3;
        ui->tablewt_envTest->setItem(i, 0, new QTableWidgetItem(QString::fromLocal8Bit("摄像头")));
        QWidget *widget = new QWidget;
        QHBoxLayout *layout = new QHBoxLayout;
        widget->setLayout(layout);
        layout->addStretch();
        QLabel *icon = new QLabel;
        if(nCameraStatus == 1)
        {
            icon->setFixedSize(QSize(g_appInfoPtr->m_fRate*12, g_appInfoPtr->m_fRate*9));
            icon->setPixmap(QPixmap(":/images/icon-et-item-pass.png").scaled(g_appInfoPtr->m_fRate*12, g_appInfoPtr->m_fRate*9));
        }
        else if(nCameraStatus == 0)
        {
            icon->setFixedSize(QSize(g_appInfoPtr->m_fRate*8, g_appInfoPtr->m_fRate*8));
            icon->setPixmap(QPixmap(":/images/icon-et-iten-unpass.png").scaled(g_appInfoPtr->m_fRate*8, g_appInfoPtr->m_fRate*8));
            QListWidgetItem *item = new QListWidgetItem(QIcon(QPixmap(":/images/icon-et-tips.png")), QString::fromLocal8Bit("请确认摄像头连接线正常，能正常工作，关闭杀毒软件；请确认您的电脑是否为双摄像头，启用摄像头是否正确。"));
            ui->lw_ettr_unpass->addItem(item);
			item->setSizeHint(QSize(item->sizeHint().width(), g_appInfoPtr->m_fRate * 26));
        }
        else if(nCameraStatus == 2)
        {
			icon->setStyleSheet(sTextStyle);
            icon->setText(QString::fromLocal8Bit("请在“摄像头”步骤进行人工确认！"));
            icon->adjustSize();			
        }
        layout->addWidget(icon);

        ui->tablewt_envTest->setCellWidget(i, 1, widget);
    }

    {
        i = 4;
        ui->tablewt_envTest->setItem(i, 0, new QTableWidgetItem(QString::fromLocal8Bit("声音")));
        QWidget *widget = new QWidget;
        QHBoxLayout *layout = new QHBoxLayout;
        widget->setLayout(layout);
        layout->addStretch();
        QLabel *icon = new QLabel;
        if(nVoiceStatus == 1)
        {
            icon->setFixedSize(QSize(g_appInfoPtr->m_fRate*12, g_appInfoPtr->m_fRate*9));
            icon->setPixmap(QPixmap(":/images/icon-et-item-pass.png").scaled(g_appInfoPtr->m_fRate*12, g_appInfoPtr->m_fRate*9));
        }
        else if(nVoiceStatus == 0)
        {
            icon->setFixedSize(QSize(g_appInfoPtr->m_fRate*8, g_appInfoPtr->m_fRate*8));
            icon->setPixmap(QPixmap(":/images/icon-et-iten-unpass.png").scaled(g_appInfoPtr->m_fRate*8, g_appInfoPtr->m_fRate*8));
            QListWidgetItem *item = new QListWidgetItem(QIcon(QPixmap(":/images/icon-et-tips.png")), QString::fromLocal8Bit("请确认音箱连接正常，调节音量开关及大小。"));
            ui->lw_ettr_unpass->addItem(item);
			item->setSizeHint(QSize(item->sizeHint().width(), g_appInfoPtr->m_fRate * 26));
        }
        else if(nVoiceStatus == 2)
        {
			icon->setStyleSheet(sTextStyle);
            icon->setText(QString::fromLocal8Bit("请在“声音”步骤进行人工确认！"));
            icon->adjustSize();			
        }
        layout->addWidget(icon);

        ui->tablewt_envTest->setCellWidget(i, 1, widget);
    }
    {
        i = 5;
        ui->tablewt_envTest->setItem(i, 0, new QTableWidgetItem(QString::fromLocal8Bit("微信小程序")));
        QWidget *widget = new QWidget;
        QHBoxLayout *layout = new QHBoxLayout;
        widget->setLayout(layout);
        layout->addStretch();
        QLabel *icon = new QLabel;
        if(nWXStatus == 1)
        {
            icon->setFixedSize(QSize(g_appInfoPtr->m_fRate*12, g_appInfoPtr->m_fRate*9));
            icon->setPixmap(QPixmap(":/images/icon-et-item-pass.png").scaled(g_appInfoPtr->m_fRate*12, g_appInfoPtr->m_fRate*9));
        }
        else if(nWXStatus == 0)
        {
            icon->setFixedSize(QSize(g_appInfoPtr->m_fRate*8, g_appInfoPtr->m_fRate*8));
            icon->setPixmap(QPixmap(":/images/icon-et-iten-unpass.png").scaled(g_appInfoPtr->m_fRate*8, g_appInfoPtr->m_fRate*8));
            QListWidgetItem *item = new QListWidgetItem(QIcon(QPixmap(":/images/icon-et-tips.png")), QString::fromLocal8Bit("请确认微信已登录并连接网络。"));
            ui->lw_ettr_unpass->addItem(item);
			item->setSizeHint(QSize(item->sizeHint().width(), g_appInfoPtr->m_fRate * 26));
        }
        else if(nWXStatus == 2)
        {
			icon->setStyleSheet(sTextStyle);
            icon->setText(QString::fromLocal8Bit("请在“微信小程序”步骤进行人工确认！"));
            icon->adjustSize();			
        }
        layout->addWidget(icon);

        ui->tablewt_envTest->setCellWidget(i, 1, widget);
    }

    if(ui->lw_ettr_unpass->count() > 0)
    {
        QListWidgetItem *item = new QListWidgetItem(QIcon(QPixmap(":/images/icon-et-tips.png")), QString::fromLocal8Bit("请按提示检查并调试，调试后可再次进行环境检测。"));
        ui->lw_ettr_unpass->addItem(item);
    }
}
