#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;

	if (!g_appInfoPtr->m_oExamInfo.sCourseCode.isEmpty() &&
		!g_appInfoPtr->m_oExamInfo.sCourseName.isEmpty())
	{
		ui->label_awbg_exam->setText(QString::fromLocal8Bit("%1(%2)")
			.arg(g_appInfoPtr->m_oExamInfo.sCourseName)
			.arg(g_appInfoPtr->m_oExamInfo.sCourseCode));
	}

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

    
	m_pServerTime = std::make_shared<QTimer>();
	m_pServerTime->setInterval(1000);
	connect(m_pServerTime.get(), &QTimer::timeout, this, [&]() {
        ui->label_serverTime->adjustSize();
		ui->label_serverTime->setText(QString::fromLocal8Bit("服务器时间：%1")
			.arg(QDateTime::fromMSecsSinceEpoch(g_appInfoPtr->serverMTime()).toString("yyyy-MM-dd hh:mm:ss")));
	});
	m_pServerTime->start();
}

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

void awBackground::initUI()
{
    ui->label_cl_company->setVisible(false);
    QString sStudentInfo = "";
    if(g_appInfoPtr->m_bShowStudentName)
    {
        sStudentInfo = g_appInfoPtr->m_sStudentName;
    }

    if(g_appInfoPtr->m_bShowStudentCode)
    {
        if(sStudentInfo.isEmpty())
        {
            sStudentInfo = g_appInfoPtr->m_sStudentCode;
        }
        else
        {
            sStudentInfo = QString::fromLocal8Bit("%1 | %2").arg(sStudentInfo).arg(g_appInfoPtr->m_sStudentCode);
        }
    }

    if(g_appInfoPtr->m_bShowStudentIdentity)
    {
        if(sStudentInfo.isEmpty())
        {
            sStudentInfo = g_appInfoPtr->m_sStudentIdentityNumber;
        }
        else
        {
            sStudentInfo = QString::fromLocal8Bit("%1 | %2").arg(sStudentInfo).arg(g_appInfoPtr->m_sStudentIdentityNumber);
        }
    }

    if(sStudentInfo.isEmpty())
    {
        sStudentInfo = g_appInfoPtr->m_sOrgName;
    }
    else
    {
        sStudentInfo = QString::fromLocal8Bit("%1 | %2").arg(sStudentInfo).arg(g_appInfoPtr->m_sOrgName);
    }

    ui->label_awbg_student->setText(sStudentInfo);

//	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_sStudentIdentityNumber).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->btn_aw_close->setVisible(!g_appInfoPtr->m_bFullScreenTop);
    ui->btn_aw_minisize->setVisible(!g_appInfoPtr->m_bFullScreenTop);
	ui->label_awbg_student->adjustSize();
	int nStudentLeft = ui->widget_awbg_top->width() - g_appInfoPtr->m_fRate*30 - ui->label_awbg_student->width();
    
	if(!g_appInfoPtr->m_bFullScreenTop)
    {
        ui->btn_aw_close->setGeometry(ui->widget_awbg_top->width() - g_appInfoPtr->m_fRate*(30 + 24),
                                      ui->label_awbg_exam->y(), g_appInfoPtr->m_fRate * 24, g_appInfoPtr->m_fRate * 24);
        ui->btn_aw_minisize->setGeometry(ui->btn_aw_close->x() - g_appInfoPtr->m_fRate*6 - ui->btn_aw_close->width(),
                                      ui->btn_aw_close->y(), ui->btn_aw_close->width(), ui->btn_aw_close->height());
        nStudentLeft = ui->btn_aw_minisize->x() - g_appInfoPtr->m_fRate*20 - ui->label_awbg_student->width();
    }
    
    ui->label_awbg_student->setGeometry(nStudentLeft,
                                        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());
    if(!g_appInfoPtr->m_sVersionCode.isEmpty())
    {
        ui->label_cl_version->setText(QString("V%1").arg(g_appInfoPtr->m_sVersionCode));
    }
    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->setText(QString::fromLocal8Bit("服务器时间：%1")
		.arg(QDateTime::fromMSecsSinceEpoch(g_appInfoPtr->serverMTime()).toString("yyyy-MM-dd hh:mm:ss")));
	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::gobackLogin, this, &awBackground::gobackLogin);
            connect(m_pWaitExam.get(), &awWaitExam::exitEaxm, this, [&](){
                m_pWaitExam.reset();
                emit closeWidget();
            });

            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;

            });
        }
		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);

	}	
	else if (m_wt == AW_WIDGET_TYPE::awwt_examScore)
	{
        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_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();
	}
}

void awBackground::showExamAnswer()
{
    if(m_pAnswerWidget == nullptr)
    {
        m_pAnswerWidget = std::make_shared<answerWidget>(this);
        connect(m_pAnswerWidget.get(), &answerWidget::gobackLogin, this, &awBackground::gobackLogin);
        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();
}

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;
        if(m_wt == AW_WIDGET_TYPE::awwt_answerWidget)
        {
            showExamAnswer();
        }
	}
	else
	{
        if(getCourseInfo.sMessage.isEmpty())
        {
            ShowMsg(QString::fromLocal8Bit("获取科目信息失败,退出登录"), g_appInfoPtr->m_pAnsBgWidget, MSG_ICON_TYPE::mit_error);
        }
        else
        {
            ShowMsg(getCourseInfo.sMessage, g_appInfoPtr->m_pAnsBgWidget, MSG_ICON_TYPE::mit_error);
        }
        QTimer::singleShot(2000, this, [&](){emit gobackLogin();});
	}
}


void awBackground::on_btn_aw_close_clicked()
{
    QApplication::quit();
}

void awBackground::on_btn_aw_minisize_clicked()
{
    emit minisize();
}
