#include "environmentalTest.h"
#include "ui_environmentalTest.h"

#include <QDesktopWidget>

#include "CAppInfo.h"


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

    setWindowFlags(Qt::FramelessWindowHint);

    setStyleSheet(g_appInfoPtr->m_sQssStr);

    m_curentStep = ENV_TEST_STEP::ett_network;

    initUI();
}

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

void environmentalTest::initUI()
{
    QDesktopWidget *dekwiget = QApplication::desktop();
    setGeometry(0, 0, dekwiget->width(), dekwiget->height());
    ui->widget_mask->setGeometry(0, 0, dekwiget->width(), dekwiget->height());
    ui->widget_et_BG->setGeometry((width() - g_appInfoPtr->m_fRate*800)/2, (height() - g_appInfoPtr->m_fRate*377)/2,
                                   g_appInfoPtr->m_fRate*800, g_appInfoPtr->m_fRate*377);
    ui->label_et_title->adjustSize();
    ui->label_et_title->setGeometry(g_appInfoPtr->m_fRate*20, g_appInfoPtr->m_fRate*16,
                                    ui->label_et_title->width(), ui->label_et_title->height());
    ui->widget_et_steps->setGeometry(0, ui->label_et_title->y() + ui->label_et_title->height() + g_appInfoPtr->m_fRate*16,
                                     ui->widget_et_BG->width(), g_appInfoPtr->m_fRate*86);
    ui->label_et_networkIcon->setGeometry(g_appInfoPtr->m_fRate*55, g_appInfoPtr->m_fRate*20, g_appInfoPtr->m_fRate*24, g_appInfoPtr->m_fRate*24);
    ui->label_et_network->adjustSize();
    ui->label_et_network->setGeometry(ui->label_et_networkIcon->x() + (ui->label_et_networkIcon->width() - ui->label_et_network->width())/2,
                                      ui->label_et_networkIcon->y() + ui->label_et_networkIcon->height() + g_appInfoPtr->m_fRate*6,
                                      ui->label_et_network->width(), ui->label_et_network->height());
    ui->label_et_HLine1->setGeometry(ui->label_et_networkIcon->x() + ui->label_et_networkIcon->width()/2, ui->label_et_networkIcon->y() + (ui->label_et_networkIcon->height() - g_appInfoPtr->m_fRate*1)/2,
                                     g_appInfoPtr->m_fRate*109 + ui->label_et_networkIcon->width(), g_appInfoPtr->m_fRate*1 < 1 ? 1 : g_appInfoPtr->m_fRate*1);
    ui->label_et_timeIcon->setGeometry(ui->label_et_networkIcon->x() + ui->label_et_HLine1->width(), ui->label_et_networkIcon->y(),
                                       ui->label_et_networkIcon->width(), ui->label_et_networkIcon->height());
    ui->label_et_time->adjustSize();
    ui->label_et_time->setGeometry(ui->label_et_timeIcon->x() + (ui->label_et_timeIcon->width() - ui->label_et_time->width())/2,
                                   ui->label_et_network->y(), ui->label_et_time->width(), ui->label_et_time->height());
    ui->label_et_HLine2->setGeometry(ui->label_et_HLine1->x() + ui->label_et_HLine1->width(), ui->label_et_HLine1->y(),
                                     ui->label_et_HLine1->width(), ui->label_et_HLine1->height());
    ui->label_et_cameraIcon->setGeometry(ui->label_et_timeIcon->x() + ui->label_et_HLine1->width(), ui->label_et_networkIcon->y(),
                                       ui->label_et_networkIcon->width(), ui->label_et_networkIcon->height());
    ui->label_et_camera->adjustSize();
    ui->label_et_camera->setGeometry(ui->label_et_cameraIcon->x() + (ui->label_et_cameraIcon->width() - ui->label_et_camera->width())/2,
                                   ui->label_et_network->y(), ui->label_et_camera->width(), ui->label_et_camera->height());
    ui->label_et_HLine3->setGeometry(ui->label_et_HLine2->x() + ui->label_et_HLine1->width(), ui->label_et_HLine1->y(),
                                     ui->label_et_HLine1->width(), ui->label_et_HLine1->height());
    ui->label_et_voiceIcon->setGeometry(ui->label_et_cameraIcon->x() + ui->label_et_HLine1->width(), ui->label_et_networkIcon->y(),
                                       ui->label_et_networkIcon->width(), ui->label_et_networkIcon->height());
    ui->label_et_voice->adjustSize();
    ui->label_et_voice->setGeometry(ui->label_et_voiceIcon->x() + (ui->label_et_voiceIcon->width() - ui->label_et_voice->width())/2,
                                   ui->label_et_network->y(), ui->label_et_voice->width(), ui->label_et_voice->height());
    ui->label_et_HLine4->setGeometry(ui->label_et_HLine3->x() + ui->label_et_HLine1->width(), ui->label_et_HLine1->y(),
                                     ui->label_et_HLine1->width(), ui->label_et_HLine1->height());
    ui->label_et_mobileIcon->setGeometry(ui->label_et_voiceIcon->x() + ui->label_et_HLine1->width(), ui->label_et_networkIcon->y(),
                                       ui->label_et_networkIcon->width(), ui->label_et_networkIcon->height());
    ui->label_et_mobile->adjustSize();
    ui->label_et_mobile->setGeometry(ui->label_et_mobileIcon->x() + (ui->label_et_mobileIcon->width() - ui->label_et_mobile->width())/2,
                                   ui->label_et_network->y(), ui->label_et_mobile->width(), ui->label_et_mobile->height());
    ui->label_et_HLine5->setGeometry(ui->label_et_HLine4->x() + ui->label_et_HLine1->width(), ui->label_et_HLine1->y(),
                                     ui->label_et_HLine1->width(), ui->label_et_HLine1->height());
    ui->label_et_resultIcon->setGeometry(ui->label_et_mobileIcon->x() + ui->label_et_HLine1->width(), ui->label_et_networkIcon->y(),
                                       ui->label_et_networkIcon->width(), ui->label_et_networkIcon->height());
    ui->label_et_result->adjustSize();
    ui->label_et_result->setGeometry(ui->label_et_resultIcon->x() + (ui->label_et_resultIcon->width() - ui->label_et_result->width())/2,
                                   ui->label_et_network->y(), ui->label_et_result->width(), ui->label_et_result->height());
    ui->btn_nextStep->setGeometry(ui->widget_et_BG->width() - g_appInfoPtr->m_fRate*(30 + 80), ui->widget_et_BG->height() - g_appInfoPtr->m_fRate*(30 + 30),
                                  g_appInfoPtr->m_fRate*80, g_appInfoPtr->m_fRate*30);
    ui->btn_previousStep->setGeometry(ui->btn_nextStep->x() - g_appInfoPtr->m_fRate*10 - ui->btn_nextStep->width(), ui->btn_nextStep->y(),
                                      ui->btn_nextStep->width(), ui->btn_nextStep->height());
    changeStepUI(m_curentStep);
}

void environmentalTest::changeStepUI(ENV_TEST_STEP step)
{
    QString sHLineNormalStyle = QString(R"(QLabel
                                        {
                                            background:rgba(218,217,229,1);
                                        })");
    QString sHLineHoverStyle = QString(R"(QLabel
                                        {
                                            background:rgba(19,187,138,1);
                                        })");
    QString sNumNormalStyle = QString(R"(QLabel
                                      {
                                          border:1px solid rgba(105,105,105,1);
                                          border-radius:%1px;
                                          background:rgba(244,243,247,1);
                                          font-size:%2px;
                                          font-family:"Microsoft YaHei";
                                          font-weight:600;
                                          color:rgba(102, 102, 102,1);
                                      })").arg((int)(g_appInfoPtr->m_fRate*12)).arg((int)(g_appInfoPtr->m_fRate*12));
    QString sNumHoverStyle = QString(R"(QLabel
                                      {
                                          border:0px solid rgba(105,105,105,1);
                                          border-radius:%1px;
                                          background:rgba(19,187,138,1);
                                          font-size:%2px;
                                          font-family:"Microsoft YaHei";
                                          font-weight:600;
                                          color:rgba(255, 255, 255,1);
                                      })").arg((int)(g_appInfoPtr->m_fRate*12)).arg((int)(g_appInfoPtr->m_fRate*12));
	QString sTextNormalStyle = QString(R"(QLabel
                                      { 
                                          font-size:%1px;
                                          font-family:"Microsoft YaHei";
                                          font-weight:600;
                                          color:rgba(102, 102, 102,1);
                                      })").arg((int)(g_appInfoPtr->m_fRate * 12));
	QString sTextHoverStyle = QString(R"(QLabel
                                      {
                                          font-size:%1px;
                                          font-family:"Microsoft YaHei";
                                          font-weight:600;
                                          color:rgba(19, 187, 138,1);
                                      })").arg((int)(g_appInfoPtr->m_fRate * 12));
    int nWidgetHeight = 0;
    int nHeight = 0;
	ui->btn_nextStep->setText(QString::fromLocal8Bit("下一步"));
    if(step == ENV_TEST_STEP::ett_network)
    {
		if(m_pTimeTest !=nullptr)
		{ 
			m_pTimeTest->hide();
		}
		
        if(m_pNetworkTest == nullptr)
        {
            m_pNetworkTest = std::make_shared<etNetworkTest>(ui->widget_et_BG);
			nHeight = m_pNetworkTest->setUI(0, ui->widget_et_steps->y() + ui->widget_et_steps->height(), ui->widget_et_BG->width());
        }
		nHeight = m_pNetworkTest->widgetHeight();
		m_pNetworkTest->show();
		ui->label_et_HLine1->setStyleSheet(sHLineNormalStyle);
		ui->label_et_network->setStyleSheet(sTextHoverStyle);
		ui->label_et_networkIcon->setStyleSheet(sNumHoverStyle);
		ui->label_et_time->setStyleSheet(sTextNormalStyle);
		ui->label_et_timeIcon->setStyleSheet(sNumNormalStyle);
    }
    else if(step == ENV_TEST_STEP::ett_time)
    {
		if (m_pNetworkTest != nullptr)
		{
			m_pNetworkTest->hide();
		}
		if (m_pCameraTest != nullptr)
		{
			m_pCameraTest->hide();
		}
        if(m_pTimeTest == nullptr)
        {
            m_pTimeTest = std::make_shared<etTimeTest>(ui->widget_et_BG);
			nHeight = m_pTimeTest->setUI(0, ui->widget_et_steps->y() + ui->widget_et_steps->height(), ui->widget_et_BG->width());
        }
		nHeight = m_pTimeTest->widgetHeight();
		m_pTimeTest->show();
		ui->label_et_HLine1->setStyleSheet(sHLineHoverStyle);
        ui->label_et_HLine2->setStyleSheet(sHLineNormalStyle);
		ui->label_et_network->setStyleSheet(sTextNormalStyle);
		ui->label_et_networkIcon->setStyleSheet(sNumNormalStyle);
		ui->label_et_camera->setStyleSheet(sTextNormalStyle);
		ui->label_et_cameraIcon->setStyleSheet(sNumNormalStyle);
		ui->label_et_time->setStyleSheet(sTextHoverStyle);
		ui->label_et_timeIcon->setStyleSheet(sNumHoverStyle);
    }
    else if(step == ENV_TEST_STEP::ett_camera)
    {
		if (m_pTimeTest != nullptr)
		{
			m_pTimeTest->hide();
		}
		if (m_pVoiceTest != nullptr)
		{
			m_pVoiceTest->hide();
		}
        if(m_pCameraTest == nullptr)
        {
            m_pCameraTest = std::make_shared<etCameraTest>(ui->widget_et_BG);
			nHeight = m_pCameraTest->setUI(0, ui->widget_et_steps->y() + ui->widget_et_steps->height(), ui->widget_et_BG->width());
        }
		nHeight = m_pCameraTest->widgetHeight();
		m_pCameraTest->show();
		ui->label_et_HLine2->setStyleSheet(sHLineHoverStyle);
        ui->label_et_HLine3->setStyleSheet(sHLineNormalStyle);
		ui->label_et_camera->setStyleSheet(sTextHoverStyle);
		ui->label_et_cameraIcon->setStyleSheet(sNumHoverStyle);
		ui->label_et_time->setStyleSheet(sTextNormalStyle);
		ui->label_et_timeIcon->setStyleSheet(sNumNormalStyle);
		ui->label_et_voice->setStyleSheet(sTextNormalStyle);
		ui->label_et_voiceIcon->setStyleSheet(sNumNormalStyle);
    }
    else if(step == ENV_TEST_STEP::ett_voice)
    {
		if (m_pCameraTest != nullptr)
		{
			m_pCameraTest->hide();
		}
		if (m_pMobileTest != nullptr)
		{
			m_pMobileTest->hide();
		}

        if(m_pVoiceTest == nullptr)
        {
            m_pVoiceTest = std::make_shared<etVoiceTest>(ui->widget_et_BG);
			nHeight = m_pVoiceTest->setUI(0, ui->widget_et_steps->y() + ui->widget_et_steps->height(), ui->widget_et_BG->width());
        }
		nHeight = m_pVoiceTest->widgetHeight();
		m_pVoiceTest->show();
		ui->label_et_HLine3->setStyleSheet(sHLineHoverStyle);
        ui->label_et_HLine4->setStyleSheet(sHLineNormalStyle);
		ui->label_et_camera->setStyleSheet(sTextNormalStyle);
		ui->label_et_cameraIcon->setStyleSheet(sNumNormalStyle);
		ui->label_et_voice->setStyleSheet(sTextHoverStyle);
		ui->label_et_voiceIcon->setStyleSheet(sNumHoverStyle);
		ui->label_et_mobile->setStyleSheet(sTextNormalStyle);
		ui->label_et_mobileIcon->setStyleSheet(sNumNormalStyle);
    }
    else if(step == ENV_TEST_STEP::ett_mobile)
    {
		if (m_pVoiceTest != nullptr)
		{
			m_pVoiceTest->hide();
		}
		if (m_pTestResult != nullptr)
		{
			m_pTestResult->hide();
		}
        if(m_pMobileTest == nullptr)
        {
            m_pMobileTest = std::make_shared<etMobileTest>(ui->widget_et_BG);
			nHeight = m_pMobileTest->setUI(0, ui->widget_et_steps->y() + ui->widget_et_steps->height(), ui->widget_et_BG->width());
        }
		nHeight = m_pMobileTest->widgetHeight();
		m_pMobileTest->show();
		ui->label_et_HLine4->setStyleSheet(sHLineHoverStyle);
        ui->label_et_HLine5->setStyleSheet(sHLineNormalStyle);
		ui->label_et_voice->setStyleSheet(sTextNormalStyle);
		ui->label_et_voiceIcon->setStyleSheet(sNumNormalStyle);
		ui->label_et_mobile->setStyleSheet(sTextHoverStyle);
		ui->label_et_mobileIcon->setStyleSheet(sNumHoverStyle);
		ui->label_et_result->setStyleSheet(sTextNormalStyle);
		ui->label_et_resultIcon->setStyleSheet(sNumNormalStyle);
    }
    else if(step == ENV_TEST_STEP::ett_result)
    {
		if (m_pMobileTest != nullptr)
		{
			m_pMobileTest->hide();
		}
        if(m_pTestResult == nullptr)
        {
            m_pTestResult = std::make_shared<etTestResult>(ui->widget_et_BG);
			//nHeight = m_pTestResult->setUI(0, ui->widget_et_steps->y() + ui->widget_et_steps->height(), ui->widget_et_BG->width());
        }
		//nHeight = m_pTestResult->widgetHeight();
		int nNetworkStatus = m_pNetworkTest->getCheckStatus();
		int nTimekStatus = m_pTimeTest->getCheckStatus();
		int nCameraStatus = m_pCameraTest->getCheckStatus();
		int nVoiceStatus = m_pVoiceTest->getCheckStatus();
		int nWXStatus = m_pMobileTest->getCheckStatus();
		m_pTestResult->setResult(nNetworkStatus, nTimekStatus, nCameraStatus,
			nVoiceStatus, nWXStatus);
		nHeight = m_pTestResult->setUI(0, ui->widget_et_steps->y() + ui->widget_et_steps->height(), ui->widget_et_BG->width());
		m_pTestResult->show();
        ui->label_et_HLine5->setStyleSheet(sHLineHoverStyle);
		ui->label_et_mobile->setStyleSheet(sTextNormalStyle);
		ui->label_et_mobileIcon->setStyleSheet(sNumNormalStyle);
		ui->label_et_result->setStyleSheet(sTextHoverStyle);
		ui->label_et_resultIcon->setStyleSheet(sNumHoverStyle);
		ui->btn_nextStep->setText(QString::fromLocal8Bit("进入考试"));
    }
    nWidgetHeight = ui->widget_et_steps->y() + ui->widget_et_steps->height() + nHeight + g_appInfoPtr->m_fRate*90;
    ui->widget_et_BG->setGeometry((width() - g_appInfoPtr->m_fRate*800)/2, (height() - nWidgetHeight)/2,
                                   g_appInfoPtr->m_fRate*800, nWidgetHeight);
    ui->btn_nextStep->setGeometry(ui->widget_et_BG->width() - g_appInfoPtr->m_fRate*(30 + 80), ui->widget_et_BG->height() - g_appInfoPtr->m_fRate*(30 + 30),
                                  g_appInfoPtr->m_fRate*80, g_appInfoPtr->m_fRate*30);
    ui->btn_previousStep->setGeometry(ui->btn_nextStep->x() - g_appInfoPtr->m_fRate*10 - ui->btn_nextStep->width(), ui->btn_nextStep->y(),
                                      ui->btn_nextStep->width(), ui->btn_nextStep->height());
}

void environmentalTest::on_btn_previousStep_clicked()
{
    if(m_curentStep > ENV_TEST_STEP::ett_network)
    {
        m_curentStep = static_cast<ENV_TEST_STEP>(static_cast<int>(m_curentStep) - 1);
        changeStepUI(m_curentStep);
    }
}

void environmentalTest::on_btn_nextStep_clicked()
{
    if(m_curentStep < ENV_TEST_STEP::ett_result)
    {
        m_curentStep = static_cast<ENV_TEST_STEP>(static_cast<int>(m_curentStep) + 1);
        changeStepUI(m_curentStep);
    }
    else
    {
        emit enterExam();
    }
}
