#include "awQuestionNavigate.h"
#include "ui_awQuestionNavigate.h"

#include "CAppInfo.h"

#include "CCommonTools.h"

#include <QGridLayout>

awQuestionNavigate::awQuestionNavigate(CPaperGroupStruct &pgs, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::awQuestionNavigate), m_pgs(pgs)
{
    ui->setupUi(this);

    setStyleSheet(g_appInfoPtr->m_sQssStr);
}

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

int awQuestionNavigate::setUI(const int nWidth)
{
	ui->label_questionName->setText(QString::fromLocal8Bit("%1 %2").arg(CCommonTools::Arab2Sinogram(m_pgs.nNumber)).arg(m_pgs.sGroupName));
    ui->label_questionName->setToolTip(ui->label_questionName->text());
	ui->label_questionScore->setText(QString::fromLocal8Bit("%1分").arg(m_pgs.fGroupScore));

    ui->label_questionScore->adjustSize();
    ui->label_questionScore->setGeometry(nWidth - ui->label_questionScore->width(), ui->label_questionName->y() + (ui->label_questionName->height() - ui->label_questionScore->height())/2,
                                         ui->label_questionScore->width(), ui->label_questionScore->height());

    ui->label_questionName->setGeometry(0, g_appInfoPtr->m_fRate*20, ui->label_questionScore->x() - g_appInfoPtr->m_fRate*10, ui->label_questionName->height());
    ui->label_questionName->adjustSize();
    int nNameWidth = ui->label_questionScore->x() - ui->label_questionName->x() - g_appInfoPtr->m_fRate*10;
    int nRowCount = (ui->label_questionName->width() + nNameWidth - 1)/nNameWidth;
    if(nRowCount > 1)
    {
         ui->label_questionName->setWordWrap(true);
         ui->label_questionName->setGeometry(0, g_appInfoPtr->m_fRate*20,
                                               nNameWidth, nRowCount*ui->label_questionName->height());
    }


    ui->widget_qn_groupInfo->setGeometry(0, 0, nWidth, g_appInfoPtr->m_fRate*30 + ui->label_questionName->height());
//	QGridLayout *layout = new QGridLayout;
//	layout->setHorizontalSpacing(g_appInfoPtr->m_fRate*10);
//	layout->setVerticalSpacing(g_appInfoPtr->m_fRate*10);
//	layout->setAlignment(Qt::AlignLeft);
//	layout->setContentsMargins(0, 0, 0, 0);
	
    m_vItemList.clear();
    int nTotal = 0;
    int nQuestionSize = m_pgs.vQuestionStruct.size();
    for(int i = 0; i < nQuestionSize; ++i)
    {
        CQuestionStruct &qs = m_pgs.vQuestionStruct[i];
        int nSubQuestionSize = qs.vSubQuestionStruct.size();
        for(int j = 0; j < nSubQuestionSize; ++j)
        {
            CSubQuestionStruct &sqs = qs.vSubQuestionStruct[j];
            awqn_item *item = new awqn_item(sqs, ui->widget_qn_questionList);
            connect(item, &awqn_item::showSubQuestion, this, [&](int nOrder) {
                    emit showSubQuestion(qs.sQuestionId, nOrder, qs.bHasContent);
			});

            int nCol = nTotal%5;
            int nRow = (nTotal + 1 + 4)/5 - 1;
            int nY = g_appInfoPtr->m_fRate*nRow * 40;
            int nX = g_appInfoPtr->m_fRate*nCol * 40;

            item->setUI(nX, nY, (g_appInfoPtr->m_fRate*30), (30*g_appInfoPtr->m_fRate));


            m_vItemList.push_back(item);
            nTotal++;
        }
    }
    int nRow = (nTotal + 4)/5;
    ui->widget_qn_questionList->setGeometry(0, ui->widget_qn_groupInfo->y() + ui->widget_qn_groupInfo->height(),
                                            nWidth, nRow*40*g_appInfoPtr->m_fRate);
//	ui->widget_qn_questionList->setLayout(layout);
    setGeometry(0, 0, nWidth, ui->widget_qn_questionList->height() + ui->widget_qn_groupInfo->height());
    return height();
}

int awQuestionNavigate::showItems(std::vector<awqn_item*> vItemList)
{
    int nTotal = 0;
    int nQuestionSize = vItemList.size();
    for(int i = 0; i < nQuestionSize; ++i)
    {
        awqn_item *item = vItemList[i];

        int nCol = nTotal%5;
        int nRow = (nTotal + 1 + 4)/5 - 1;
        int nY = g_appInfoPtr->m_fRate*nRow * 40;
        int nX = g_appInfoPtr->m_fRate*nCol * 40;
        item->setUI(nX, nY, g_appInfoPtr->m_fRate*30, g_appInfoPtr->m_fRate*30);

        ++nTotal;
    }
    int nRow = (nTotal + 4)/5;
    int nWidth = width();
    ui->widget_qn_questionList->setGeometry(0, ui->widget_qn_groupInfo->y() + ui->widget_qn_groupInfo->height(),
                                            nWidth, nRow*40*g_appInfoPtr->m_fRate);

    setFixedHeight(ui->widget_qn_questionList->height() + ui->widget_qn_groupInfo->height());
    return height();
}

void awQuestionNavigate::refreshStatus()
{
    int nSize = m_vItemList.size();
    for(int i = 0; i < nSize; ++i)
    {
        awqn_item *item = m_vItemList[i];
        if(item)
        {
            item->refreshStatus();
        }
    }
}

int awQuestionNavigate::setShowType(ITEM_SHOW_TYPE type)
{
    std::vector<awqn_item*> vItemList;
    int nSize = m_vItemList.size();
    for(int i = 0; i < nSize; ++i)
    {
        awqn_item *item = m_vItemList[i];
        if(item)
        {
            if(type == ITEM_SHOW_TYPE::ist_showAll)
            {
                item->setVisible(true);
                vItemList.push_back(item);
            }
            else if(type == ITEM_SHOW_TYPE::ist_showMarked)
            {
                if(item->isMarked())
                {
                    item->setVisible(true);
                    vItemList.push_back(item);
                }
                else
                {
                    item->setVisible(false);
                }
            }
            else if(type == ITEM_SHOW_TYPE::ist_showAnswered)
            {
                if(item->isAnswered())
                {
                    item->setVisible(true);
                    vItemList.push_back(item);
                }
                else
                {
                    item->setVisible(false);
                }
            }
            else if(type == ITEM_SHOW_TYPE::ist_showUnanswered)
            {
                if(!item->isAnswered())
                {
                    item->setVisible(true);
                    vItemList.push_back(item);
                }
                else
                {
                    item->setVisible(false);
                }
            }
        }
    }

    return showItems(vItemList);
}
