#include "fbBlank.h"
#include "ui_fbBlank.h"

#include <QFile>
#include <QTextBlock>
//#include "MsgDlg.h"

#include "CAppInfo.h"

fbBlank::fbBlank(int nOrder, QWidget *parent) :
    CBaseWidget(parent),
    ui(new Ui::fbBlank)
{
    ui->setupUi(this);

    setAttribute(Qt::WA_DeleteOnClose);

    m_nOrder = nOrder;

//    QFile qssFile(":/qss/main.qss");
//    qssFile.open(QFile::ReadOnly);
//    QString qss;
//    qss = qssFile.readAll();
//    setStyleSheet(qss);
    setStyleSheet(g_appInfoPtr->m_sQssStr);
}

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

int fbBlank::setUI(const int nWidth)
{
    setGeometry(0, 0, nWidth, g_appInfoPtr->m_fRate*60);
    QString sText = QChar('A' + m_nOrder -1);
    ui->btn_blankName->setText(sText);
    ui->btn_blankName->setGeometry(0, g_appInfoPtr->m_fRate*15, g_appInfoPtr->m_fRate*30, g_appInfoPtr->m_fRate*30);
    ui->tedt_blank->setGeometry(ui->btn_blankName->x() + ui->btn_blankName->width() + g_appInfoPtr->m_fRate*20, g_appInfoPtr->m_fRate*10, g_appInfoPtr->m_fRate*640, g_appInfoPtr->m_fRate*40);
    return g_appInfoPtr->m_fRate*60;
}

int fbBlank::widgetType()
{
    return wt_blank;
}

void fbBlank::on_tedt_blank_textChanged()
{    
    QApplication::processEvents();
    emit answerChanged();
}

QString fbBlank::getAnswer()
{
    return ui->tedt_blank->toPlainText();

//    Json::Value jBody = Json::Value::null;
//    auto bodyDoc = ui->tedt_blank->document();
//    if(bodyDoc->isEmpty())
//    {
//        jBody = Json::ValueType::arrayValue;
//    }
//    else
//    {
//        Json::Value sectionJson = Json::Value::null;
//        for (QTextBlock it = bodyDoc->begin(); it != bodyDoc->end(); it = it.next())
//        {
//            Json::Value blocksJson = Json::Value::null;
//            for(auto fragit = it.begin(); !fragit.atEnd(); ++fragit)
//            {
//                if(fragit.fragment().charFormat().isImageFormat())
//                {//图片
//                    const std::string& txt = fragit.fragment().charFormat().toImageFormat().name().toLocal8Bit().toStdString();

//                    if(0 == txt.find_first_of("data:image/"))
//                    {

//                    }
//                    else
//                    {

//                    }
//                }
//                else
//                {//文本
//                    Json::Value blockItem;
//                    blockItem["type"] = "text";
//                    blockItem["value"] = fragit.fragment().text().toStdString();
////                    //加粗
////                    if(fragit.fragment().charFormat().fontWeight() == QFont::Bold)
////                    {
////                        blockItem["param"]["font"]["bold"] = true;
////                    }

////                    //斜体
////                    if(fragit.fragment().charFormat().fontItalic())
////                    {
////                        blockItem["param"]["font"]["italic"] = true;
////                    }

////                    //下划线
////                    if(fragit.fragment().charFormat().fontUnderline())
////                    {
////                        blockItem["param"]["font"]["underline"] = true;
////                    }
//                    blocksJson.append(blockItem);
//                }
//            }
//            Json::Value sectionItem = Json::Value::null;
//            if(blocksJson.size() > 0)
//            {
//                sectionItem["blocks"] = blocksJson;
//            }
//            else
//            {
//                sectionItem["blocks"] = Json::ValueType::arrayValue;
//            }
//            sectionJson.append(sectionItem);
//        }

//        if(sectionJson.size() > 0)
//        {
//            jBody = sectionJson;
//        }
//        else
//        {
//            jBody = Json::ValueType::arrayValue;
//        }
//    }
//    return jBody;
}

void fbBlank::setAnswer(QString sAnswer)
{
    ui->tedt_blank->setText(sAnswer);
//    QTextCursor cursor = ui->tedt_blank->textCursor();

//        int nSize = jAnswer.size();
//        for(int i = 0; i < nSize; i ++)
//        {
//            Json::Value blocks = jAnswer[i];
//            for(int j = 0; j < (int)blocks["blocks"].size(); j++)
//            {
//                if(blocks["blocks"][j]["type"].asString() == "text")
//                {
//                    QTextCharFormat fmt;
//                    QFont font;
//                    font.setFamily("Microsoft YaHei");
//                    font.setPixelSize(g_appInfoPtr->m_fRate*14);
//                    font.setWeight(QFont::Medium);
//                    fmt.setFont(font);
//                    cursor.insertText((blocks["blocks"][j]["value"].asString().c_str()), fmt);
//                }
//            }
//            if(i < nSize - 1)
//            {
//                cursor.insertText("\n");
//            }
//        }
//    ui->tedt_blank->setTextCursor(cursor);
}
