 #include "awWaitExam.h"
#include "ui_awWaitExam.h"

#include "CAppInfo.h"
#include "awMsgBox.h"

#include <QListWidgetItem>
#include <QHBoxLayout>
#include <QLabel>

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

    setStyleSheet(g_appInfoPtr->m_sQssStr);

	qRegisterMetaType<CGetExamProperty>("CGetExamProperty");
	connect(g_httpBllPtr.get(), &CHttpBll::sgnGetExamProperty, this, &awWaitExam::onGetExamProperty);
	qRegisterMetaType<CGetExamRecordPaperStruct>("CGetExamRecordPaperStruct");
	connect(g_httpBllPtr.get(), &CHttpBll::sgnGetExamRecordPaperStruct, this, &awWaitExam::onGetExamRecordPaperStruct);
	qRegisterMetaType<CStartExam>("CStartExam");
	connect(g_httpBllPtr.get(), &CHttpBll::sgnStartExam, this, &awWaitExam::onStartExam);


	CHttpRequestPackage hrp;
	hrp.sUri = QString("/api/ecs_exam_work/exam/getExamPropertyFromCacheByStudentSession/%1/%2")
		.arg(g_appInfoPtr->m_oExamInfo.nExamId).arg("BEFORE_EXAM_REMARK");
	hrp.nRequestType = RequestType::rtGetExamProperty;
	hrp.sCommonStr = "BEFORE_EXAM_REMARK";
	g_httpBllPtr->get(hrp);

	CHttpRequestPackage hrp1;
	hrp1.sUri = "/api/ecs_oe_student/client/exam/process/startExam";
	hrp1.nRequestType = RequestType::rtStartExam;
	hrp1.sParamList.push_back(QString("examStudentId,%1").arg(g_appInfoPtr->m_oExamInfo.nExamStudentId));
	hrp1.eParamType = HttpParamType::hptUrl;
	g_httpBllPtr->post(hrp1);

	bHasStruct = false;
	nCountDownCount = 60 * 2;
	m_pTimer = std::make_shared<QTimer>();
	m_pTimer->setInterval(1000);
	m_pTimer->start();
	connect(m_pTimer.get(), &QTimer::timeout, this, [&]() {
		--nCountDownCount;
		ui->btn_we_startExam->setText(QString::fromLocal8Bit("接受以上条款，开始考试（倒计时：%1:%2:%3）")
			.arg(nCountDownCount / 3600, 2, 10, QChar('0'))
			.arg((nCountDownCount % 3600) / 60, 2, 10, QChar('0'))
			.arg(nCountDownCount % 60, 2, 10, QChar('0')));
				
		if (bHasStruct && nCountDownCount < 110)
		{
			ui->btn_we_startExam->setStyleSheet(QString(R"( QPushButton
														{
															background:rgba(19,187,138,1);
															border-radius:%1px;
															font-size:%2px;
															font-family:"Microsoft YaHei";
															font-weight:600;
															color:rgba(255,255,255,0.5);
														})").arg((int)(g_appInfoPtr->m_fRate*10))
															.arg((int)(g_appInfoPtr->m_fRate * 14)));
		}

		if (nCountDownCount == 0)
		{
			m_pTimer->stop();
			emit enterExam();
			return;
		}
	});
}

awWaitExam::~awWaitExam()
{
    awMsgBox::clear(this);
    delete ui;
}

void awWaitExam::setUI(const int nLeft, const int nTop, const int nWidth, const int nHeight)
{
    setGeometry(nLeft, nTop, nWidth, nHeight);
    ui->widget_we_preNotice->setGeometry(0, 0, width()*970/(970+270), height());
    ui->widget_we_paperStruct->setGeometry(ui->widget_we_preNotice->x() + ui->widget_we_preNotice->width() + g_appInfoPtr->m_fRate*20, 0,
                                           width() - ui->widget_we_preNotice->width() - g_appInfoPtr->m_fRate*20, height());
    ui->label_we_notice->adjustSize();
    ui->label_we_notice->setGeometry(g_appInfoPtr->m_fRate*40, g_appInfoPtr->m_fRate*30,
                                     ui->label_we_notice->width(), ui->label_we_notice->height());
    ui->txtb_we_prenotice->setGeometry(ui->label_we_notice->x(), ui->label_we_notice->y() + ui->label_we_notice->height() + g_appInfoPtr->m_fRate*20,
                                       ui->widget_we_preNotice->width() - g_appInfoPtr->m_fRate*40*2, g_appInfoPtr->m_fRate*390);
    ui->btn_we_startExam->setGeometry((ui->widget_we_preNotice->width() - g_appInfoPtr->m_fRate*380)/2,
                                      ui->txtb_we_prenotice->y() + ui->txtb_we_prenotice->height() + g_appInfoPtr->m_fRate*110,
                                      g_appInfoPtr->m_fRate*380, g_appInfoPtr->m_fRate*40);
    ui->label_we_struct->adjustSize();
    ui->label_we_struct->setGeometry(g_appInfoPtr->m_fRate*30, g_appInfoPtr->m_fRate*20,
                                     ui->label_we_struct->width(), ui->label_we_struct->height());
    ui->label_we_totalScore->adjustSize();
    ui->label_we_totalScore->setGeometry(ui->widget_we_paperStruct->width() - g_appInfoPtr->m_fRate*30 - ui->label_we_totalScore->width(),
                                         ui->label_we_struct->y() + (ui->label_we_struct->height() - ui->label_we_totalScore->height())/2,
                                         ui->label_we_totalScore->width(), ui->label_we_totalScore->height());
    ui->lw_we_struct->setGeometry(ui->label_we_struct->x(), ui->label_we_struct->y() + ui->label_we_struct->height() + g_appInfoPtr->m_fRate*20,
                                  ui->widget_we_paperStruct->width() - g_appInfoPtr->m_fRate*30*2,
                                  ui->widget_we_paperStruct->height() - ui->label_we_struct->y() - ui->label_we_struct->height() - g_appInfoPtr->m_fRate*(20 + 30));
    
}

void awWaitExam::onStartExam(CStartExam startExam)
{
	if (startExam.nCode == 200)
	{
		g_appInfoPtr->m_oExamInfo.nExamRecordDataId = startExam.nExamRecordDataId;
		g_appInfoPtr->m_oExamInfo.sCourseCode = startExam.sCourseCode;
		g_appInfoPtr->m_oExamInfo.sCourseName = startExam.sCourseName;
		g_appInfoPtr->m_oExamInfo.nFaceVerifyMinute = startExam.nFaceVerifyMinute;
		CHttpRequestPackage hrp1;
		hrp1.sUri = "/api/ecs_oe_student/client/exam/process/getExamRecordPaperStruct";
		hrp1.sParamList.push_back(QString("examRecordDataId,%1").arg(g_appInfoPtr->m_oExamInfo.nExamRecordDataId));
		hrp1.nRequestType = RequestType::rtGetExamRecordPaperStruct;
		hrp1.eParamType = HttpParamType::hptUrl;
		g_httpBllPtr->post(hrp1);
	}
	else
	{
        if(startExam.sMessage.isEmpty())
        {
            ShowMsg(QString::fromLocal8Bit("开始考试失败,退出登录"), (QWidget*)g_appInfoPtr->m_pAnsBgWidget->parent(), MSG_ICON_TYPE::mit_error);
        }
        else
        {
            ShowMsg(startExam.sMessage, (QWidget*)g_appInfoPtr->m_pAnsBgWidget->parent(), MSG_ICON_TYPE::mit_error);
        }
        QTimer::singleShot(2000, this, [&](){emit gobackLogin();});
	}
}



void awWaitExam::onGetExamRecordPaperStruct(CGetExamRecordPaperStruct getExamRecordPaperStruct)
{
	if (getExamRecordPaperStruct.nCode == 200)
	{
		int nSize = getExamRecordPaperStruct.vPaperGroupStruct.size();
		double tTotalScore = 0;
		for (int i = 0; i < nSize; ++i)
		{
			CPaperGroupStruct pgs = getExamRecordPaperStruct.vPaperGroupStruct[i];
			QListWidgetItem *pItem = new QListWidgetItem;
			QSize size = pItem->sizeHint();
			size.setHeight(g_appInfoPtr->m_fRate * 37);
			pItem->setSizeHint(size);
			ui->lw_we_struct->addItem(pItem);
			QWidget *w = new QWidget;
			//        w->setStyleSheet(QString("QWidget{background:rgba(19, 187, 138, 1); border-radius:%1px}").arg((int)(g_appInfoPtr->m_fRate*10)));
			QHBoxLayout *l = new QHBoxLayout;
			QString sLabelSubjectStyle = QString(R"(QLabel{
                                      font-size:%1px;
                                      font-family:"Microsoft YaHei";
                                      font-weight:500;
                                      color:rgba(68,68,68,1);})").arg((int)(g_appInfoPtr->m_fRate * 12));
			QString sLabelSubjectInfoStyle = QString(R"(QLabel{
                                      font-size:%1px;
                                      font-family:"Microsoft YaHei";
                                      font-weight:500;
                                      color:rgba(153,153,153,1);})").arg((int)(g_appInfoPtr->m_fRate * 12));
			QLabel *label_subject = new QLabel;
			label_subject->setText(QString::fromLocal8Bit("%1、%2").arg(i+1).arg(pgs.sGroupName));
			label_subject->setStyleSheet(sLabelSubjectStyle);
			label_subject->adjustSize();
			QLabel *label_subjectInfo = new QLabel;
			
			label_subjectInfo->setText(QString::fromLocal8Bit("共%1题，共%2分").arg(pgs.nTotalSubquestion).arg(pgs.fGroupScore));
			label_subjectInfo->setStyleSheet(sLabelSubjectInfoStyle);
			l->setContentsMargins(0, 0, 0, 0);
			l->addWidget(label_subject);
			l->addStretch();
			l->addWidget(label_subjectInfo);
			w->setLayout(l);
			ui->lw_we_struct->setItemWidget(pItem, w);

			tTotalScore += pgs.fGroupScore;
		}
		ui->label_we_totalScore->setText(QString::fromLocal8Bit("总分：%1").arg(tTotalScore));
		bHasStruct = true;
	}
	else
	{
        if(getExamRecordPaperStruct.sMessage.isEmpty())
        {
            ShowMsg(QString::fromLocal8Bit("获取试卷结构失败,退出登录"), g_appInfoPtr->m_pAnsBgWidget, MSG_ICON_TYPE::mit_error);
        }
        else
        {
            ShowMsg(getExamRecordPaperStruct.sMessage, g_appInfoPtr->m_pAnsBgWidget, MSG_ICON_TYPE::mit_error);
        }
        QTimer::singleShot(2000, this, [&](){emit gobackLogin();});
	}
}

void awWaitExam::onGetExamProperty(CGetExamProperty getExamProperty)
{
	if (getExamProperty.nCode == 200)
	{
		if (getExamProperty.sType == "BEFORE_EXAM_REMARK")
		{
			ui->txtb_we_prenotice->insertHtml(getExamProperty.sBeforeExamRemark);
		}
	}
	else
	{
        if(getExamProperty.sMessage.isEmpty())
        {
            ShowMsg(QString::fromLocal8Bit("获取考试信息失败,退出登录"), g_appInfoPtr->m_pAnsBgWidget, MSG_ICON_TYPE::mit_error);
        }
        else
        {
            ShowMsg(getExamProperty.sMessage, g_appInfoPtr->m_pAnsBgWidget, MSG_ICON_TYPE::mit_error);
        }
        QTimer::singleShot(2000, this, [&](){emit gobackLogin();});
	}
}

void awWaitExam::on_btn_we_startExam_clicked()
{
	if (bHasStruct && nCountDownCount < 110)
	{
		m_pTimer->stop();
		emit enterExam();
	}    
}
