#include "awExamScore.h"
#include "ui_awExamScore.h"

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

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

    setStyleSheet(g_appInfoPtr->m_sQssStr);

	qRegisterMetaType<CGetExamProperty>("CGetExamProperty");
	connect(g_httpBllPtr.get(), &CHttpBll::sgnGetExamProperty, this, &awExamScore::onGetExamProperty);
	qRegisterMetaType<CGetEndExamInfo>("CGetEndExamInfo");
	connect(g_httpBllPtr.get(), &CHttpBll::sgnGetEndExamInfo, this, &awExamScore::onGetEndExamInfo);

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

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

void awExamScore::setUI(const int nLeft, const int nTop, const int nWidth, const int nHeight)
{
    setGeometry(nLeft, nTop, nWidth, nHeight);
    ui->widget_awes_BG->setGeometry(0, 0, nWidth, nHeight);
    ui->label_awes_title->adjustSize();
    ui->label_awes_title->setGeometry((ui->widget_awes_BG->width() - ui->label_awes_title->width())/2, g_appInfoPtr->m_fRate*58,
                                      ui->label_awes_title->width(), ui->label_awes_title->height());
    ui->label_awes_ptoto->setGeometry((ui->widget_awes_BG->width() - g_appInfoPtr->m_fRate*90)/2,
                                      ui->label_awes_title->y() + ui->label_awes_title->height() + g_appInfoPtr->m_fRate*20,
                                      g_appInfoPtr->m_fRate*90, g_appInfoPtr->m_fRate*120);
    QString sFileName = g_appInfoPtr->m_sStudentPhotoPath.right(g_appInfoPtr->m_sStudentPhotoPath.length() - g_appInfoPtr->m_sStudentPhotoPath.lastIndexOf("/") - 1);
    sFileName = g_appInfoPtr->m_sCacheFileDir + sFileName;

    ui->label_awes_ptoto->setPixmap(QPixmap(sFileName).scaled(ui->label_awes_ptoto->width(), ui->label_awes_ptoto->height(),
                                                                Qt::IgnoreAspectRatio));
    ui->label_awes_result->adjustSize();
    ui->label_awes_result->setGeometry((ui->widget_awes_BG->width() - ui->label_awes_result->width())/2, ui->label_awes_ptoto->y() + ui->label_awes_ptoto->height() + g_appInfoPtr->m_fRate*20,
                                       ui->label_awes_result->width(), ui->label_awes_result->height());
    ui->txtb_awes_postNotice->setGeometry(g_appInfoPtr->m_fRate*40, ui->label_awes_result->y() + ui->label_awes_result->height() + g_appInfoPtr->m_fRate*40,
                                          ui->widget_awes_BG->width() - g_appInfoPtr->m_fRate*40*2, g_appInfoPtr->m_fRate*164);
    ui->txtb_awes_breachNotice->setVisible(false);
    ui->txtb_awes_breachNotice->setGeometry(ui->txtb_awes_postNotice->x() + ui->txtb_awes_postNotice->width() + g_appInfoPtr->m_fRate*20,
                                            ui->txtb_awes_postNotice->y(), ui->txtb_awes_postNotice->width(), ui->txtb_awes_postNotice->height());
    ui->btn_awes_goback->setGeometry((ui->widget_awes_BG->width() - g_appInfoPtr->m_fRate*200)/2, ui->txtb_awes_postNotice->y() + ui->txtb_awes_postNotice->height() + g_appInfoPtr->m_fRate*30,
                                     g_appInfoPtr->m_fRate*200, g_appInfoPtr->m_fRate*40);	
}

void awExamScore::onGetExamProperty(CGetExamProperty getExamProperty)
{
	if (getExamProperty.nCode == 200)
	{
		if (getExamProperty.sType == "AFTER_EXAM_REMARK,IS_OBJ_SCORE_VIEW,SHOW_CHEATING_REMARK,CHEATING_REMARK")
		{			
			if (getExamProperty.bShowCheatingRemark)
			{
                ui->txtb_awes_breachNotice->setVisible(true);
				ui->txtb_awes_postNotice->setGeometry(g_appInfoPtr->m_fRate * 40, ui->label_awes_result->y() + ui->label_awes_result->height() + g_appInfoPtr->m_fRate * 40,
					(ui->widget_awes_BG->width() - g_appInfoPtr->m_fRate * 40 * 2 - g_appInfoPtr->m_fRate*20)/2, g_appInfoPtr->m_fRate * 164);
				ui->txtb_awes_breachNotice->setGeometry(ui->txtb_awes_postNotice->x() + ui->txtb_awes_postNotice->width() + g_appInfoPtr->m_fRate * 20,
					ui->txtb_awes_postNotice->y(), ui->txtb_awes_postNotice->width(), ui->txtb_awes_postNotice->height());
                QTextCursor cursor = ui->txtb_awes_breachNotice->textCursor();
				QTextBlockFormat textStyleFormat = cursor.blockFormat();				
				textStyleFormat.setLineHeight(g_appInfoPtr->m_fRate * 5, QTextBlockFormat::LineDistanceHeight);
				cursor.setBlockFormat(textStyleFormat);
				QTextCharFormat fmt;
                QFont font;
                font.setFamily("Microsoft YaHei");
                font.setPixelSize(g_appInfoPtr->m_fRate*16);
                font.setWeight(QFont::Bold);
				fmt.setFont(font);
                cursor.insertText(QString::fromLocal8Bit("违纪说明：\n"), fmt);				
				cursor.insertHtml(getExamProperty.sCheatingRemark);
				ui->txtb_awes_breachNotice->setTextCursor(cursor);
			}

			{
				QTextCursor cursor = ui->txtb_awes_postNotice->textCursor();
				QTextBlockFormat textStyleFormat = cursor.blockFormat();
				textStyleFormat.setLineHeight(g_appInfoPtr->m_fRate * 5, QTextBlockFormat::LineDistanceHeight);
				cursor.setBlockFormat(textStyleFormat);
				QTextCharFormat fmt;
				QFont font;
				font.setFamily("Microsoft YaHei");
				font.setPixelSize(g_appInfoPtr->m_fRate * 16);
				font.setWeight(QFont::DemiBold);
				fmt.setFont(font);
				cursor.insertText(QString::fromLocal8Bit("考后说明：\n"), fmt);
				cursor.insertHtml(getExamProperty.sAfterExamRemark);
				ui->txtb_awes_postNotice->setTextCursor(cursor);
			}

			if (getExamProperty.bIsObjScoreView)
			{
				//获取客观分
				ui->label_awes_result->setText(QString::fromLocal8Bit("考试结果计算中..."));
				m_getScoreEndTime = g_appInfoPtr->serverMTime() + +2 * 60 * 1000;
				getObjectiveScore();
			}
			else
			{
				//显示交卷成功
				ui->label_awes_result->setText(QString::fromLocal8Bit("交卷完成"));
			}
		}
	}
	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);
        }
	}
}

void awExamScore::getObjectiveScore()
{
	CHttpRequestPackage hrp;
	hrp.sUri = QString("/api/ecs_oe_student/client/exam/process/getEndExamInfo");
	hrp.sParamList.push_back(QString("examRecordDataId,%1").arg(g_appInfoPtr->m_oExamInfo.nExamRecordDataId));
	hrp.nRequestType = RequestType::rtGetEndExamInfo;
	hrp.eParamType = HttpParamType::hptUrl;
	g_httpBllPtr->post(hrp);
}

void awExamScore::onGetEndExamInfo(CGetEndExamInfo getEndExamInfo)
{
	if (getEndExamInfo.nCode == 200)
	{
		if (getEndExamInfo.bIsCalculate)
		{
			ui->label_awes_result->setText(QString::fromLocal8Bit("考试结果计算中…"));
			if (m_getScoreEndTime > g_appInfoPtr->serverMTime())
			{
				QTimer::singleShot(3 * 1000, [&]() {
					getObjectiveScore();
				});
			}
			else
			{

			}
		}
		else if (getEndExamInfo.bIsWarn)
		{
			ui->label_awes_result->setText(QString::fromLocal8Bit("成绩待审核"));
		}
		else
		{
			ui->label_awes_result->setText(QString::fromLocal8Bit("客观题得分：%1").arg(getEndExamInfo.fObjectiveScore));
		}
		
	}
	else
	{
        if(getEndExamInfo.sMessage.isEmpty())
        {
            ShowMsg(QString::fromLocal8Bit("获取考试信息失败"), g_appInfoPtr->m_pAnsBgWidget, MSG_ICON_TYPE::mit_error);
        }
        else
        {
            ShowMsg(getEndExamInfo.sMessage, g_appInfoPtr->m_pAnsBgWidget, MSG_ICON_TYPE::mit_error);
        }
	}
}

void awExamScore::on_btn_awes_goback_clicked()
{
    emit goHomePage();
}
