#include "clopReport.h"
#include "ui_clopReport.h"

#include "CAppInfo.h"
#include "clOperation.h"
#include "clopPaperReport.h"
#include "clopPaperDetail.h"

#include <QScrollBar>

clopReport::clopReport(CPracticeRecord pr, bool bFromCache, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::clopReport), m_pr(pr), m_bFromCache(bFromCache)
{
    ui->setupUi(this);

    setStyleSheet(g_appInfoPtr->m_sQssStr);
	
	ui->lw_reportInfo->setVerticalScrollMode(QListWidget::ScrollPerPixel);
	ui->lw_reportInfo->verticalScrollBar()->setSingleStep(g_appInfoPtr->m_fRate*5);

    ui->label_clop_course->setText(QString::fromLocal8Bit("/  科目：%1").arg(pr.sCourseName));
    ui->label_clop_correctRate->setText(QString::fromLocal8Bit("%1%").arg(pr.fObjectiveAccuracy));
}

clopReport::~clopReport()
{
    delete ui;
}

void clopReport::setUI(const int nLeft, const int nTop, const int nWidth, const int nHeight)
{
    setGeometry(nLeft, nTop, nWidth, nHeight);
    ui->widget_clop_BG->setGeometry(0, 0, width(), height());
    ui->widget_clop_top->setGeometry(0, 0, width(), g_appInfoPtr->m_fRate*56);
    ui->btn_clop_goback->setGeometry(ui->widget_clop_top->width() - g_appInfoPtr->m_fRate*(108 + 30), (ui->widget_clop_top->height() - g_appInfoPtr->m_fRate*32)/2, 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_clop_reportTitle->adjustSize();
    ui->label_clop_reportTitle->setGeometry(g_appInfoPtr->m_fRate*30, (ui->widget_clop_top->height() - ui->label_clop_reportTitle->height())/2, ui->label_clop_reportTitle->width(), ui->label_clop_reportTitle->height());
    ui->label_clop_course->adjustSize();
    ui->label_clop_course->setGeometry(ui->label_clop_reportTitle->x() + ui->label_clop_reportTitle->width() + g_appInfoPtr->m_fRate*10,
                                       (ui->widget_clop_top->height() - ui->label_clop_course->height())/2,
                                       ui->label_clop_course->width(), ui->label_clop_course->height());
    ui->label_clop_correctRate->adjustSize();
    ui->label_clop_correctRate->setGeometry(ui->btn_clop_goback->x() - g_appInfoPtr->m_fRate*30 - ui->label_clop_correctRate->width(),
                                            (ui->widget_clop_top->height() - ui->label_clop_correctRate->height())/2,
                                            ui->label_clop_correctRate->width(), ui->label_clop_correctRate->height());
    ui->label_clop_correctRateHint->adjustSize();
    ui->label_clop_correctRateHint->setGeometry(ui->label_clop_correctRate->x() - ui->label_clop_correctRateHint->width() - g_appInfoPtr->m_fRate*10,
                                                (ui->widget_clop_top->height() - ui->label_clop_correctRateHint->height())/2,
                                                ui->label_clop_correctRateHint->width(), ui->label_clop_correctRateHint->height());
    ui->lw_reportInfo->setGeometry(0, ui->widget_clop_top->height(), width(), height() - ui->widget_clop_top->height());

    {
        clopPaperReport *paperReport = new clopPaperReport(m_pr.nId, m_bFromCache);
		connect(paperReport, &clopPaperReport::heightChange, this, [&](int nReportHeight) {
			QListWidgetItem *pItem = ui->lw_reportInfo->item(0);
			if (pItem)
			{
				QSize size = pItem->sizeHint();
				size.setHeight(nReportHeight);
				pItem->setSizeHint(size);
			}
		});

        connect(paperReport, &clopPaperReport::objectiveAccuracy, this, [&](double fAccuracy) {
            ui->label_clop_correctRate->setText(QString::fromLocal8Bit("%1%").arg(fAccuracy));
            ui->label_clop_correctRate->adjustSize();
            ui->label_clop_correctRate->setGeometry(ui->btn_clop_goback->x() - g_appInfoPtr->m_fRate*30 - ui->label_clop_correctRate->width(),
                                                    (ui->widget_clop_top->height() - ui->label_clop_correctRate->height())/2,
                                                    ui->label_clop_correctRate->width(), ui->label_clop_correctRate->height());
            ui->label_clop_correctRateHint->adjustSize();
            ui->label_clop_correctRateHint->setGeometry(ui->label_clop_correctRate->x() - ui->label_clop_correctRateHint->width() - g_appInfoPtr->m_fRate*10,
                                                        (ui->widget_clop_top->height() - ui->label_clop_correctRateHint->height())/2,
                                                        ui->label_clop_correctRateHint->width(), ui->label_clop_correctRateHint->height());
        });

        QListWidgetItem *pItem = new QListWidgetItem;
        ui->lw_reportInfo->addItem(pItem);
        ui->lw_reportInfo->setItemWidget(pItem, paperReport);

        int npaperReportHeight = paperReport->setUI(ui->lw_reportInfo->width() - g_appInfoPtr->m_fRate*60);
        QSize size = pItem->sizeHint();
        size.setHeight(npaperReportHeight);
        pItem->setSizeHint(size);
    }

    {
        clopPaperDetail *paperDetail = new clopPaperDetail(m_pr.nId, m_pr.sCourseCode, m_bFromCache);
		connect(paperDetail, &clopPaperDetail::heightChange, this, [&](int nReportHeight) {
			QListWidgetItem *pItem = ui->lw_reportInfo->item(1);
			if (pItem)
			{
				QSize size = pItem->sizeHint();
				size.setHeight(nReportHeight);
				pItem->setSizeHint(size);
			}
		});
        QListWidgetItem *pItem = new QListWidgetItem;
        ui->lw_reportInfo->addItem(pItem);
        ui->lw_reportInfo->setItemWidget(pItem, paperDetail);

        int nPaperDetailHeight = paperDetail->setUI(ui->lw_reportInfo->width() - g_appInfoPtr->m_fRate*60);
        QSize size = pItem->sizeHint();
        size.setHeight(nPaperDetailHeight);
        pItem->setSizeHint(size);
    }
}




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