#include "question.h"
#include "ui_question.h"

#include <QFile>
#include <QScrollBar>

#include "choiceQuesiton.h"
#include "fillBlank.h"
#include "questionAndAnswer.h"
#include "standardAnswer.h"
#include "CAppInfo.h"

question::question(CQuestionStruct &qs, int nOrder, int nTotalSubQuestion, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::question), m_qs(qs), m_nOrder(nOrder), m_nTotalSubQuestion(nTotalSubQuestion)
{
    ui->setupUi(this);

    setAttribute(Qt::WA_DeleteOnClose);

    m_pWidgetChangeQuestion = nullptr;


    setStyleSheet(g_appInfoPtr->m_sQssStr);

    ui->list_question->setVerticalScrollMode(QListWidget::ScrollPerPixel);
    ui->list_question->verticalScrollBar()->setSingleStep(g_appInfoPtr->m_fRate * 5);
}

question::~question()
{
    if(m_pWidgetChangeQuestion != nullptr)
    {
        delete m_pWidgetChangeQuestion;
        m_pWidgetChangeQuestion = nullptr;
    }

    
	delete ui;
}

void question::setUI(const int nLeft, const int nTop, const int nWidth, const int nHeight)
{
    setGeometry(nLeft, nTop, nWidth, nHeight);

    ui->label_bigQuesionName->adjustSize();
    ui->label_bigQuesionName->setGeometry(g_appInfoPtr->m_fRate*40, 0, ui->label_bigQuesionName->width(), ui->label_bigQuesionName->height());
    ui->label_groupProgress->adjustSize();
    ui->label_groupProgress->setGeometry(width() - g_appInfoPtr->m_fRate*40 - g_appInfoPtr->m_fRate*6 - ui->label_groupProgress->width(),
                                         ui->label_bigQuesionName->y() + ui->label_bigQuesionName->height() - ui->label_groupProgress->height(),
                                         ui->label_groupProgress->width(), ui->label_groupProgress->height());
    int nNameWidth = ui->label_groupProgress->x() - ui->label_bigQuesionName->x() - g_appInfoPtr->m_fRate*20;
    int nRowCount = (ui->label_bigQuesionName->width() + nNameWidth - 1)/nNameWidth;
    if(nRowCount > 1)
    {
         ui->label_bigQuesionName->setWordWrap(true);
         ui->label_bigQuesionName->setGeometry(g_appInfoPtr->m_fRate*40, 0,
                                               nNameWidth, nRowCount*ui->label_bigQuesionName->height());
    }
    ui->line_question->setGeometry(g_appInfoPtr->m_fRate*40, ui->label_bigQuesionName->y() + ui->label_bigQuesionName->height() + g_appInfoPtr->m_fRate*20, width() - g_appInfoPtr->m_fRate*40*2, g_appInfoPtr->m_fRate*2);
    int nQuestionTop = ui->label_bigQuesionName->y() + ui->label_bigQuesionName->height() + g_appInfoPtr->m_fRate*40;
    ui->list_question->setGeometry(g_appInfoPtr->m_fRate*40, nQuestionTop,
                                         width() - g_appInfoPtr->m_fRate*40 - g_appInfoPtr->m_fRate*6, nHeight - nQuestionTop - g_appInfoPtr->m_fRate*52);

    m_nListHeight = 0;
    for (int i = 0; i < m_qs.vSubQuestionStruct.size(); ++i)
    {
        if (m_qs.vSubQuestionStruct[i].nOrder == m_nOrder)
        {
            initQuestionItem(m_qs.vSubQuestionStruct[i]);
        }
    }

    if(m_nListHeight + g_appInfoPtr->m_fRate*10 < ui->list_question->height())
    {
        ui->list_question->setGeometry(g_appInfoPtr->m_fRate*40, ui->label_bigQuesionName->y() + ui->label_bigQuesionName->height() + 40,
                                             width() - g_appInfoPtr->m_fRate*40 - g_appInfoPtr->m_fRate*6, m_nListHeight + g_appInfoPtr->m_fRate*10);
    }
    if(m_pWidgetChangeQuestion == nullptr)
    {
        m_pWidgetChangeQuestion = new changeQuestion(this);
        m_pWidgetChangeQuestion->setUI(0, ui->list_question->y() + ui->list_question->height(), width(), g_appInfoPtr->m_fRate*52);
    }
    m_pWidgetChangeQuestion->show();
    connect(m_pWidgetChangeQuestion, &changeQuestion::previousQuestion, this, &question::onPreviousQuestion);
    connect(m_pWidgetChangeQuestion, &changeQuestion::nextQuestion, this, &question::onNextQuestion);

    m_pWidgetChangeQuestion->showBtn(3);
    if(m_nOrder == 1)
    {
        m_pWidgetChangeQuestion->showBtn(1);
    }
    else if(m_nOrder == m_nTotalSubQuestion)
    {
        m_pWidgetChangeQuestion->showBtn(2);
    }
}

void question::showBtn(int nType)
{
    m_pWidgetChangeQuestion->showBtn(nType);
}

void question::onPreviousQuestion()
{
    emit previousQuestion(m_nOrder);
}

void question::onNextQuestion()
{
    emit nextQuestion(m_nOrder);
}

int question::initQuestionItem(CSubQuestionStruct &subQuestionItem)
{

    if(subQuestionItem.sQuestionType == QUESTION_TYPE::SingleChoice ||
            subQuestionItem.sQuestionType == QUESTION_TYPE::MultipleChoice ||
            subQuestionItem.sQuestionType == QUESTION_TYPE::TrueOrFalse)
    {       
            choiceQuesiton *wCQ = new choiceQuesiton(subQuestionItem);
            connect(wCQ, &choiceQuesiton::markedItem, this, &question::onMarkedItem);
            connect(wCQ, &choiceQuesiton::questionAnswered, this, &question::onQuestionAnswered);
            connect(wCQ, &choiceQuesiton::uploadAnswer, this, &question::onUploadAnswer);

            int nHeight = wCQ->setUI(ui->list_question->width() - 8);
            m_nListHeight += nHeight;
            QListWidgetItem *listItem = new QListWidgetItem;
            ui->list_question->addItem(listItem);
            listItem->setSizeHint(QSize(ui->list_question->width() - 8, nHeight));            
            ui->list_question->setItemWidget(listItem, wCQ);

    }
    else if(subQuestionItem.sQuestionType == QUESTION_TYPE::FillUp)
    {
        fillBlank *wFB = new fillBlank(subQuestionItem);
        connect(wFB, &fillBlank::markedItem, this, &question::onMarkedItem);
        connect(wFB, &fillBlank::questionAnswered, this, &question::onQuestionAnswered);
        connect(wFB, &fillBlank::uploadAnswer, this, &question::onUploadAnswer);
        int nHeight = wFB->setUI(ui->list_question->width() - 8);
        m_nListHeight += nHeight;
        QListWidgetItem *listItem = new QListWidgetItem;
        listItem->setSizeHint(QSize(ui->list_question->width() - 8, nHeight));
        ui->list_question->addItem(listItem);
        ui->list_question->setItemWidget(listItem, wFB);

    }
    else if(subQuestionItem.sQuestionType == QUESTION_TYPE::Essay)
    {
        questionAndAnswer *wQAA = new questionAndAnswer(subQuestionItem);
        connect(wQAA, &questionAndAnswer::markedItem, this, &question::onMarkedItem);
        connect(wQAA, &questionAndAnswer::questionAnswered, this, &question::onQuestionAnswered);
        connect(wQAA, &questionAndAnswer::uploadAnswer, this, &question::onUploadAnswer);
        int nHeight = wQAA->setUI(ui->list_question->width() - 8);
        m_nListHeight += nHeight;
        QListWidgetItem *listItem = new QListWidgetItem;
        listItem->setSizeHint(QSize(ui->list_question->width() - 8, nHeight));
        ui->list_question->addItem(listItem);
        ui->list_question->setItemWidget(listItem, wQAA);
    }

    if(g_appInfoPtr->m_oExamInfo.sExamType == "PRACTICE" && g_appInfoPtr->m_oExamInfo.sPracticeType == "IN_PRACTICE")
    {
        standardAnswer *wSA = new standardAnswer(subQuestionItem);
        connect(wSA, &standardAnswer::heightChange, this, [&](int nH){
            for(int i = 0; i < ui->list_question->count(); i++)
            {
                CQuestionBaseWidget *item = dynamic_cast<CQuestionBaseWidget*>(ui->list_question->itemWidget(ui->list_question->item(i)));
                if(item && item->widgetType() == wt_standardAnswer)
                {
                     QListWidgetItem *pItem = ui->list_question->item(i);
                     pItem->setSizeHint(QSize(ui->list_question->width() - 8, nH));
                }
            }
        });


        int nHeight = wSA->setUI(ui->list_question->width() - 8);
        m_nListHeight += nHeight;
        QListWidgetItem *listItem = new QListWidgetItem;
        listItem->setSizeHint(QSize(ui->list_question->width() - 8, nHeight));
        ui->list_question->addItem(listItem);
        ui->list_question->setItemWidget(listItem, wSA);
    }

    return 0;
}

void question::setGroupInfo(QString sTitle, QString sProgress)
{
    if(!sTitle.isEmpty())
    {
        ui->label_bigQuesionName->setText(sTitle);
    }
    ui->label_groupProgress->setText(sProgress);
}

void question::handinPaper()
{
    for(int i = 0; i < ui->list_question->count(); i++)
    {
        CQuestionBaseWidget *item = dynamic_cast<CQuestionBaseWidget*>(ui->list_question->itemWidget(ui->list_question->item(i)));
        if(item && item->widgetType() == wt_question)
        {
            item->handinPaper();
        }
    }
}

void question::onMarkedItem(bool bMarked, int nOrder)
{
    emit markedItem(bMarked, nOrder);
}

void question::onQuestionAnswered(bool bAnswered, int nOrder)
{
    emit questionAnswered(bAnswered, nOrder);
}

void question::onUploadAnswer(int nOrder, __int64 timeStamp, QString sAnswer)
{        
    emit uploadAnswer(nOrder, timeStamp, sAnswer);
}
