#include "questionOption.h"
#include "ui_questionOption.h"

#include <QFile>
#include <QDebug>
#include "optionBody.h"
#include "audioPlay.h"
#include "CAppInfo.h"

questionOption::questionOption(int nOrder, Json::Value jOption, int nLimitedPlayTimes, QWidget *parent) :
    CBaseWidget(parent),
    ui(new Ui::questionOption)
{
    ui->setupUi(this);

    setAttribute(Qt::WA_DeleteOnClose);

    m_jOption = jOption;
    m_nOrder = nOrder;
    m_nLimitedPlayTimes = nLimitedPlayTimes;
    m_bHasAns = false;
    m_bIsMultiChoice = false;


    setStyleSheet(g_appInfoPtr->m_sQssStr);
}

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

int questionOption::setUI(const int nWidth)
{
    setCheckStyle();

    setGeometry(0, 0, nWidth, g_appInfoPtr->m_fRate*200);
    QString sText = QChar('A' + m_nOrder - 1) + QString::fromLocal8Bit("．");
    ui->chkb_option->setText(sText);
    ui->chkb_option->adjustSize();
    ui->chkb_option->setGeometry(g_appInfoPtr->m_fRate*1, g_appInfoPtr->m_fRate*6,
                                 ui->chkb_option->width(), ui->chkb_option->height());


    m_nListHeight = 0;
    ui->list_options->setFixedWidth(nWidth - ui->chkb_option->x() - ui->chkb_option->width());
    initOption();
    ui->list_options->setGeometry(ui->chkb_option->x() + ui->chkb_option->width(), g_appInfoPtr->m_fRate*0,
                                nWidth - ui->chkb_option->x() - ui->chkb_option->width(),
                                m_nListHeight + g_appInfoPtr->m_fRate*55);
    setGeometry(0, 0, nWidth, ui->list_options->y() + m_nListHeight + g_appInfoPtr->m_fRate*5);    
    return height();
}

void questionOption::initOption()
{
    int nSectionSize = m_jOption["sections"].size();
    Json::Value jSections = Json::Value::null;
    for(int i = 0; i < nSectionSize; i++)
    {
        Json::Value jblocks = Json::Value::null;
        int nBlockSize = m_jOption["sections"][i]["blocks"].size();
        for(int j = 0; j < nBlockSize; j++)
        {
            if(m_jOption["sections"][i]["blocks"][j]["type"].asString() == "audio")
            {
                if(jblocks != Json::Value::null ||
                        jSections != Json::Value::null)
                {
                    if(jblocks != Json::Value::null)
                    {
                        Json::Value jBlockItem = Json::Value::null;
                        jBlockItem["blocks"] = jblocks;
                        jblocks = Json::Value::null;
                        jSections.append(jBlockItem);
                    }
                    Json::Value jQuestionBody = Json::Value::null;
                    jQuestionBody["sections"] = jSections;
                    jSections = Json::Value::null;
                    jblocks = Json::Value::null;
                    optionBody *wQB = new optionBody(jQuestionBody);
                    QListWidgetItem *listItem = new QListWidgetItem;
                    int nQBHeight = wQB->setUI(ui->list_options->width());
                    m_nListHeight += nQBHeight;
                    listItem->setSizeHint(QSize(ui->list_options->width(), nQBHeight));
                    ui->list_options->addItem(listItem);
                    ui->list_options->setItemWidget(listItem, wQB);
                }

                QString sAudioName = m_jOption["sections"][i]["blocks"][j]["ext2"].asString().c_str();
                QString sAudioUrl = m_jOption["sections"][i]["blocks"][j]["value"].asString().c_str();
                audioPlay *wAP = new audioPlay(sAudioName, sAudioUrl, m_nLimitedPlayTimes);
                connect(wAP, &audioPlay::playedCountChange, this, [&](){emit audioPlayCountChange();});
                QListWidgetItem *listItem = new QListWidgetItem;
                int nAPHeight = wAP->setUI(ui->list_options->width());
                m_nListHeight += nAPHeight;
                listItem->setSizeHint(QSize(ui->list_options->width(), nAPHeight));
                ui->list_options->addItem(listItem);
                ui->list_options->setItemWidget(listItem, wAP);
            }
            else
            {
                jblocks.append(m_jOption["sections"][i]["blocks"][j]);
            }
        }
        if(jblocks != Json::Value::null)
        {
            Json::Value jBlockItem = Json::Value::null;
            jBlockItem["blocks"] = jblocks;
            jblocks = Json::Value::null;
            jSections.append(jBlockItem);
        }
    }

    if(jSections != Json::Value::null)
    {
        Json::Value jQuestionBody = Json::Value::null;
        jQuestionBody["sections"] = jSections;
        jSections = Json::Value::null;
        optionBody *wQB = new optionBody(jQuestionBody);
        QListWidgetItem *listItem = new QListWidgetItem;
        int nQBHeight = wQB->setUI(ui->list_options->width());
        m_nListHeight += nQBHeight;
        listItem->setSizeHint(QSize(ui->list_options->width(), nQBHeight));
        ui->list_options->addItem(listItem);
        ui->list_options->setItemWidget(listItem, wQB);
    }
}

void questionOption::setCheckStyle()
{
//     height:%1px;
    QString sCheckStyle = QString(R"(QCheckBox
                                  {
                                      outline: none;                                     

                                      font-size:%1px;
                                      font-family:"Microsoft YaHei";
                                      font-weight:400;
                                      color:rgba(102,102,102,1);
                                  }

                                  QCheckBox::indicator
                                  {
                                      width:%2px;
                                      height:%3px;
                                  }

                                  QCheckBox::indicator:unchecked
                                  {
                                      border: %4px solid rgba(228,229,235,1);
                                      border-radius:%5px;
                                      background:rgba(239,240,245,1);
                                  }

                                  QCheckBox::indicator:checked
                                  {
                                      border-image: url(:/images/icon-checkbox-checked.png);
                                  })")//.arg((int)(g_appInfoPtr->m_fRate*26))
                                      .arg((int)(g_appInfoPtr->m_fRate*14))
                                      .arg((int)(g_appInfoPtr->m_fRate*14))
                                      .arg((int)(g_appInfoPtr->m_fRate*14))
                                      .arg((int)(g_appInfoPtr->m_fRate*1 < 1 ? 1 : g_appInfoPtr->m_fRate*1))
                                      .arg((int)(g_appInfoPtr->m_fRate*3));
//    height:%1px;
    QString sRadioStyle = QString(R"(QCheckBox
                                  {
                                      outline: none;

                                      border:0px solid rgba(102,102,102,1);
                                      font-size:%1px;
                                      font-family:"Microsoft YaHei";
                                      font-weight:400;
                                      color:rgba(102,102,102,1);
                                  }

                                  QCheckBox::indicator
                                  {
                                      width:%2px;
                                      height:%3px;
                                  }

                                  QCheckBox::indicator:unchecked
                                  {
                                      border-image:url(:/images/radiobtn-normal.png);
                                  }

                                  QCheckBox::indicator:checked
                                  {
                                      border-image: url(:/images/radiobtn-check.png);
                                  })")//.arg((int)(g_appInfoPtr->m_fRate*26))
                                      .arg((int)(g_appInfoPtr->m_fRate*14))
                                      .arg((int)(g_appInfoPtr->m_fRate*16))
                                      .arg((int)(g_appInfoPtr->m_fRate*16));
    if(m_bIsMultiChoice)
    {
       ui->chkb_option->setStyleSheet(sCheckStyle);
    }
    else
    {
        ui->chkb_option->setStyleSheet(sRadioStyle);
    }
}

void questionOption::setMultiChoice(bool bIsMultiChoice)
{
    m_bIsMultiChoice = bIsMultiChoice;
    setCheckStyle();
}

void questionOption::setChecked(bool bChecked)
{
    ui->chkb_option->setChecked(bChecked);
}

bool questionOption::checked()
{
    return ui->chkb_option->isChecked();
}

void questionOption::setAnswered(bool bAnswered)
{
    m_bHasAns = bAnswered;
}

int questionOption::widgetType()
{
    return wt_optionItem;
}

void questionOption::on_chkb_option_clicked()
{    
    if(!m_bIsMultiChoice) //单选 判断
    {
        if(m_bHasAns)   //已经有答案
        {
            if(ui->chkb_option->isChecked())
            {
                emit answerChanged(m_nOrder);
            }
            else
            {
                emit cancelAnswered();
            }
        }
        else
        {
            if(ui->chkb_option->isChecked())
            {
                emit answered();
            }
        }
    }
    else
    {
        emit mutiAnswerChanged();
    }
}

int questionOption::optionMumber()
{
    if(m_jOption.isMember("number"))
    {
        return m_jOption["number"].asInt();
    }
    return 0;
}
