#include "cloeViewPaper.h"
#include "ui_cloeViewPaper.h"

#include "CAppInfo.h"
#include "awMsgBox.h"
#include "json/json.h"
#include <QDesktopWidget>
#include <QDateTime>

cloeViewPaper::cloeViewPaper(QString sPaper, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::cloeViewPaper)
{
    ui->setupUi(this);
	
	initPaper(sPaper);

	initUI();		

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

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

void cloeViewPaper::initUI()
{     
     QDesktopWidget *dekwiget = QApplication::desktop();
     setGeometry(0, 0, dekwiget->width(), dekwiget->height());
     ui->label_cl_company->setVisible(false);
     ui->widget_awbg_BG->setGeometry(0, 0, width(), height());
     ui->widget_cloe_viewpaper->setGeometry(g_appInfoPtr->m_fRate*20, g_appInfoPtr->m_fRate*56, width() - g_appInfoPtr->m_fRate*20*2,
                                            height() - g_appInfoPtr->m_fRate*(56 + 38));     
	 ui->label_clopvp_paperName->adjustSize();
	 ui->label_clopvp_paperName->setGeometry((ui->widget_cloe_viewpaper->width() - ui->label_clopvp_paperName->width())/2,
		 g_appInfoPtr->m_fRate * 30, ui->label_clopvp_paperName->width(), ui->label_clopvp_paperName->height());
	 ui->label_clopvp_courseInfo->adjustSize();
	 ui->label_clopvp_courseInfo->setGeometry((ui->widget_cloe_viewpaper->width() - ui->label_clopvp_courseInfo->width()) / 2,
		 ui->label_clopvp_paperName->y() + ui->label_clopvp_paperName->height() + g_appInfoPtr->m_fRate * 10, 
		 ui->label_clopvp_courseInfo->width(), ui->label_clopvp_courseInfo->height());
	 ui->label_HLine->setGeometry(0, ui->label_clopvp_courseInfo->y() + ui->label_clopvp_courseInfo->height() + g_appInfoPtr->m_fRate * 30,
		 ui->widget_cloe_viewpaper->width(), g_appInfoPtr->m_fRate *1);
	 ui->tb_clopvp_paper->setGeometry(g_appInfoPtr->m_fRate * 40, ui->label_HLine->y() + ui->label_HLine->height() + g_appInfoPtr->m_fRate * 30,
                 ui->widget_cloe_viewpaper->width() - g_appInfoPtr->m_fRate * 40*2, ui->widget_cloe_viewpaper->height() - ui->label_HLine->y() - ui->label_HLine->height() - g_appInfoPtr->m_fRate * 30*2);
	 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_clop_goback->setGeometry(ui->widget_awbg_top->width() - g_appInfoPtr->m_fRate*(20 + 108), g_appInfoPtr->m_fRate*12,
                                      g_appInfoPtr->m_fRate*108, g_appInfoPtr->m_fRate*32);
     ui->btn_clop_goback->setIconSize(QSize(g_appInfoPtr->m_fRate*14, g_appInfoPtr->m_fRate*14));
     ui->btn_clop_goback->setIcon(QIcon(":/images/icon-goback-list.png"));
     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());

}

void cloeViewPaper::setCourseCodeAndName(QString sCode, QString sName)
{
	ui->label_awbg_exam->setText(QString::fromLocal8Bit("课程代码：%1 - 课程名称：%2").arg(sCode).arg(sName));
	ui->label_awbg_exam->adjustSize();
}

void cloeViewPaper::initPaper(QString sPaper)
{
	Json::Reader reader;
	Json::Value jPaper = Json::Value::null;
	if (!reader.parse(sPaper.toStdString(), jPaper))
	{
        ShowMsg(QString::fromLocal8Bit("解析试卷出错！"), this, MSG_ICON_TYPE::mit_error);
		return;
	}

	
	ui->label_clopvp_paperName->setText(QString::fromLocal8Bit("%1 试卷").arg(jPaper["name"].asString().c_str()));
	ui->label_clopvp_courseInfo->setText(QString::fromLocal8Bit("课程代码：%1  |  本试卷满分%2分")
		.arg(jPaper["course"]["code"].asString().c_str()).arg(jPaper["totalScore"].asDouble()));

	QTextCursor cursor = ui->tb_clopvp_paper->textCursor();
	QTextBlockFormat textStyleFormat = cursor.blockFormat();
	
	textStyleFormat.setLineHeight(g_appInfoPtr->m_fRate * 5, QTextBlockFormat::LineDistanceHeight);
	cursor.setBlockFormat(textStyleFormat);

	int nIndex = 1;
	int nSize = jPaper["paperDetails"].size();
	for (int i = 0; i < nSize; ++i)
	{
		Json::Value jPaperDetails = jPaper["paperDetails"][i];
		QString sTitle = QString::fromLocal8Bit("%1、%2(本大题共%3小题，满分%4分)")
			.arg(jPaperDetails["cnNum"].asString().c_str()).arg(jPaperDetails["name"].asString().c_str())
			.arg(jPaperDetails["unitCount"].asString().c_str()).arg(jPaperDetails["score"].asString().c_str());
		QTextCharFormat fmt;
		QFont font;
		font.setFamily("Microsoft YaHei");
		font.setPixelSize(g_appInfoPtr->m_fRate * 16);
		font.setWeight(QFont::DemiBold);
		fmt.setFont(font);
		cursor.insertText(sTitle, fmt);
		cursor.insertText("\n");
		int nQuestionSize = jPaperDetails["paperDetailUnits"].size();
		for (int j = 0; j < nQuestionSize; ++j)
		{
			Json::Value jQuestion = jPaperDetails["paperDetailUnits"][j];
			QString sBody = QString::fromLocal8Bit("%1、%2(%3分)")
				.arg(nIndex).arg(jQuestion["question"]["quesBody"].asString().c_str())
				.arg(jQuestion["score"].asDouble());
			
			font.setPixelSize(g_appInfoPtr->m_fRate * 14);
			font.setWeight(QFont::Medium);
			fmt.setFont(font);
			cursor.insertText(QString::fromLocal8Bit("%1、")
				.arg(nIndex), fmt);
			cursor.insertHtml(jQuestion["question"]["quesBody"].asString().c_str());
			cursor.insertText(QString::fromLocal8Bit("(%1分)")
				.arg(jQuestion["score"].asDouble()), fmt);
			cursor.insertText("\n\r");
		}
	}
	
}

void cloeViewPaper::on_btn_clop_goback_clicked()
{
    emit goback();
}
