#include "awBackground.h"
#include "ui_awBackground.h"

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

#include <QDesktopWidget>

awBackground::awBackground(AW_WIDGET_TYPE wt, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::awBackground), m_wt(wt)
{
    ui->setupUi(this);

    setStyleSheet(g_appInfoPtr->m_sQssStr);

    initUI();
    g_appInfoPtr->m_pAnsBgWidget = this;

    qRegisterMetaType<CGetCourseInfo>("CGetCourseInfo");
    connect(g_httpBllPtr.get(), &CHttpBll::sgnGetCourseInfo, this, &awBackground::onGetCourseInfo);

    
}

awBackground::~awBackground()
{
    g_appInfoPtr->m_pAnsBgWidget = nullptr;
    delete ui;
}

void awBackground::initUI()
{
	//ui->label_awbg_exam->setText(QString::fromLocal8Bit("%1(%2)").arg().arg());
	ui->label_awbg_student->setText(QString::fromLocal8Bit("%1 | %2 | %3 | %4")
		.arg(g_appInfoPtr->m_sStudentName).arg(g_appInfoPtr->m_sStudentCode)
		.arg(g_appInfoPtr->m_nVersionId).arg(g_appInfoPtr->m_sOrgName));
    QDesktopWidget *dekwiget = QApplication::desktop();
    setGeometry(0, 0, dekwiget->width(), dekwiget->height());
    ui->widget_awbg_BG->setGeometry(0, 0, width(), height());
    ui->widget_awbg_top->setGeometry(0, 0, ui->widget_awbg_BG->width(), g_appInfoPtr->m_fRate*112);
    ui->label_awbg_examIcon->setGeometry(g_appInfoPtr->m_fRate*30, g_appInfoPtr->m_fRate*20,
                                         g_appInfoPtr->m_fRate*16, g_appInfoPtr->m_fRate*16);
    ui->label_awbg_exam->adjustSize();
    ui->label_awbg_exam->setGeometry(ui->label_awbg_examIcon->x() + ui->label_awbg_examIcon->width() + g_appInfoPtr->m_fRate*10,
                                     ui->label_awbg_examIcon->y() + (ui->label_awbg_examIcon->height() - ui->label_awbg_exam->height())/2,
                                     ui->label_awbg_exam->width(), ui->label_awbg_exam->height());
    ui->label_awbg_student->adjustSize();
    ui->label_awbg_student->setGeometry(ui->widget_awbg_top->width() - g_appInfoPtr->m_fRate*30 - ui->label_awbg_student->width(),
                                        ui->label_awbg_exam->y(), ui->label_awbg_student->width(), ui->label_awbg_student->height());
    ui->label_awbg_studentIcon->setGeometry(ui->label_awbg_student->x() - g_appInfoPtr->m_fRate*(10 + 16),
                                            ui->label_awbg_examIcon->y(), ui->label_awbg_examIcon->width(), ui->label_awbg_examIcon->height());
    ui->label_cl_company->adjustSize();
    ui->label_cl_company->setGeometry(g_appInfoPtr->m_fRate*30, ui->widget_awbg_BG->height() - g_appInfoPtr->m_fRate*10 - ui->label_cl_company->height(),
                                      ui->label_cl_company->width(), ui->label_cl_company->height());
    ui->label_cl_version->adjustSize();
    ui->label_cl_version->setGeometry(ui->widget_awbg_BG->width() - g_appInfoPtr->m_fRate*30 - ui->label_cl_version->width(),
                                      ui->label_cl_company->y(), ui->label_cl_version->width(), ui->label_cl_version->height());
    ui->label_serverTime->adjustSize();
    ui->label_serverTime->setGeometry(ui->label_cl_version->x() - g_appInfoPtr->m_fRate*20 - ui->label_serverTime->width(),
                                      ui->label_cl_company->y(), ui->label_serverTime->width(), ui->label_serverTime->height());

	if (m_wt == AW_WIDGET_TYPE::awwt_waitExam)
	{
        if(m_pWaitExam == nullptr)
        {
            m_pWaitExam = std::make_shared<awWaitExam>(this);
            connect(m_pWaitExam.get(), &awWaitExam::enterExam, this, [&](){
                m_pWaitExam.reset();

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

				m_wt = AW_WIDGET_TYPE::awwt_answerWidget;
				if (m_pAnswerWidget == nullptr)
				{
					m_pAnswerWidget = std::make_shared<answerWidget>(this);
                    connect(m_pAnswerWidget.get(), &answerWidget::showPracticeInfo, this, [&](){
                        m_pAnswerWidget.reset();
                        emit showPracticeInfo();
                    });

                    connect(m_pAnswerWidget.get(), &answerWidget::gobackCourseList, this, [&](){
                        m_pAnswerWidget.reset();
                        emit closeWidget();
                    });

                    connect(m_pAnswerWidget.get(), &answerWidget::showExamScore, this, [&](){
                        m_pAnswerWidget.reset();

                        m_wt = AW_WIDGET_TYPE::awwt_examScore;
                        if(m_pExamScore == nullptr)
                        {
                            m_pExamScore = std::make_shared<awExamScore>(this);
                            connect(m_pExamScore.get(), &awExamScore::goHomePage, this, [&](){
                               emit closeWidget();
                            });
                        }
                        m_pExamScore->setUI(g_appInfoPtr->m_fRate * 20, g_appInfoPtr->m_fRate * 56, width() - g_appInfoPtr->m_fRate * 20 * 2,
                            height() - g_appInfoPtr->m_fRate*(56 + 38));
                        m_pExamScore->show();
                    });
				}
				m_pAnswerWidget->setUI(g_appInfoPtr->m_fRate * 20, g_appInfoPtr->m_fRate * 56, width() - g_appInfoPtr->m_fRate * 20 * 2,
					height() - g_appInfoPtr->m_fRate*(56 + 38));
				m_pAnswerWidget->show();
            });
        }
		m_pWaitExam->setUI(g_appInfoPtr->m_fRate*20, g_appInfoPtr->m_fRate*56, width() - g_appInfoPtr->m_fRate*20*2,
		                       height() - g_appInfoPtr->m_fRate*(56 + 38));                            
		m_pWaitExam->show();
	}
	else if (m_wt == AW_WIDGET_TYPE::awwt_answerWidget)
	{
		CHttpRequestPackage hrp;
		hrp.sUri = QString("/api/ecs_oe_student/client/exam/process/getCourseInfo/%1").arg(g_appInfoPtr->m_oExamInfo.nExamRecordDataId);
		hrp.sParamList.push_back(QString("examRecordDataId,%1").arg(g_appInfoPtr->m_oExamInfo.nExamRecordDataId));
		hrp.nRequestType = RequestType::rtGetCourseInfo;
		hrp.eParamType = HttpParamType::hptUrl;
		g_httpBllPtr->post(hrp);
        if(m_pAnswerWidget == nullptr)
        {
            m_pAnswerWidget = std::make_shared<answerWidget>(this);
            connect(m_pAnswerWidget.get(), &answerWidget::showPracticeInfo, this, [&](){
                m_pAnswerWidget.reset();
                emit showPracticeInfo();
            });

            connect(m_pAnswerWidget.get(), &answerWidget::gobackCourseList, this, [&](){
                m_pAnswerWidget.reset();
                emit closeWidget();
            });

            connect(m_pAnswerWidget.get(), &answerWidget::showExamScore, this, [&](){
                m_pAnswerWidget.reset();

                m_wt = AW_WIDGET_TYPE::awwt_examScore;
                if(m_pExamScore == nullptr)
                {
                    m_pExamScore = std::make_shared<awExamScore>(this);
                    connect(m_pExamScore.get(), &awExamScore::goHomePage, this, [&](){
                       emit closeWidget();
                    });
                }
                m_pExamScore->setUI(g_appInfoPtr->m_fRate * 20, g_appInfoPtr->m_fRate * 56, width() - g_appInfoPtr->m_fRate * 20 * 2,
                    height() - g_appInfoPtr->m_fRate*(56 + 38));
                m_pExamScore->show();
            });
        }
		m_pAnswerWidget->setUI(g_appInfoPtr->m_fRate * 20, g_appInfoPtr->m_fRate * 56, width() - g_appInfoPtr->m_fRate * 20 * 2,
			height() - g_appInfoPtr->m_fRate*(56 + 38));
		m_pAnswerWidget->show();
	}	
	else if (m_wt == AW_WIDGET_TYPE::awwt_examScore)
	{
        if(m_pExamScore == nullptr)
        {
            m_pExamScore = std::make_shared<awExamScore>(this);
        }
		m_pExamScore->setUI(g_appInfoPtr->m_fRate * 20, g_appInfoPtr->m_fRate * 56, width() - g_appInfoPtr->m_fRate * 20 * 2,
			height() - g_appInfoPtr->m_fRate*(56 + 38));
		m_pExamScore->show();
	}
}

void awBackground::onGetCourseInfo(CGetCourseInfo getCourseInfo)
{
    if (getCourseInfo.nCode == 200)
	{
        ui->label_awbg_exam->setText(QString::fromLocal8Bit("%1(%2)").arg(getCourseInfo.sCourseName).arg(getCourseInfo.sCourseCode));

        g_appInfoPtr->m_oExamInfo.sCourseCode = getCourseInfo.sCourseCode;
        g_appInfoPtr->m_oExamInfo.sCourseName = getCourseInfo.sCourseName;
	}
	else
	{
        if(getCourseInfo.sMessage.isEmpty())
        {
            ShowMsg(QString::fromLocal8Bit("获取科目信息失败"), g_appInfoPtr->m_pAnsBgWidget);
        }
        else
        {
            ShowMsg(getCourseInfo.sMessage, g_appInfoPtr->m_pAnsBgWidget);
        }
	}
}

