#include "fbFillBlankBody.h"
#include "ui_fbFillBlankBody.h"

#include <QFile>

#include "CAppInfo.h"
#include "CCommonTools.h"
fbFillBlankBody::fbFillBlankBody(Json::Value jBody, QWidget *parent) :
    CBaseWidget(parent),
    ui(new Ui::fbFillBlankBody)
{
    ui->setupUi(this);

    setAttribute(Qt::WA_DeleteOnClose);

    m_jBody = jBody;

    QFile qssFile(":/qss/main.qss");
    qssFile.open(QFile::ReadOnly);
    QString qss;
    qss = qssFile.readAll();
}

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

int fbFillBlankBody::setUI(const int nWidth)
{
    setGeometry(0, 0, nWidth, g_appInfoPtr->m_fRate*300);
    ui->tb_questionBody->setGeometry(0, 0, nWidth, g_appInfoPtr->m_fRate*300);
    ui->tb_questionBody->setFixedWidth(nWidth);
    QTextCursor cursor = ui->tb_questionBody->textCursor();
    QTextBlockFormat textStyleFormat = cursor.blockFormat();
//    textStyleFormat.setTextIndent(26); //首行缩进
    textStyleFormat.setLineHeight(g_appInfoPtr->m_fRate*5, QTextBlockFormat::LineDistanceHeight);
    cursor.setBlockFormat(textStyleFormat);

    int nBlockNumber = 0;
    int nSize = m_jBody["sections"].size();
    for(int i = 0; i < nSize; i ++)
    {
        Json::Value blocks = m_jBody["sections"][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*16);
                font.setWeight(QFont::Medium);
                if(blocks["blocks"][j].isMember("param"))
                {

                    if(blocks["blocks"][j]["param"].isMember("bold"))
                    {
                        font.setBold(blocks["blocks"][j]["param"]["bold"].asBool());
                    }

                    if(blocks["blocks"][j]["param"].isMember("italic"))
                    {
                        bool bstyle = blocks["blocks"][j]["param"]["italic"].asBool();
                        font.setItalic(bstyle);
                    }

                    if(blocks["blocks"][j]["param"].isMember("underline"))
                    {
                        font.setUnderline(blocks["blocks"][j]["param"]["underline"].asBool());
                    }

                    if(blocks["blocks"][j]["param"].isMember("strikeOut"))
                    {
                        font.setStrikeOut(blocks["blocks"][j]["param"]["strikeOut"].asBool());
                    }

                    if(blocks["blocks"][j]["param"].isMember("size"))
                    {
                        font.setPixelSize(blocks["blocks"][j]["param"]["size"].asInt());
                    }

                    if(blocks["blocks"][j]["param"].isMember("name"))
                    {
                        font.setFamily(blocks["blocks"][j]["param"]["name"].asString().c_str());
                    }

                }

                QString sText = blocks["blocks"][j]["value"].asString().c_str();
                QStringList textList =  sText.split("###");
                for(int il = 0; il <  textList.count(); il++)
                {
                    fmt.setFont(font);
                    cursor.insertText(textList[il], fmt);
                    if(il < textList.count() -1)
                    {
                         bool bUnderline = font.underline();
                         font.setUnderline(true);
                         QBrush br = fmt.foreground();
                         fmt.setForeground(QColor(0, 122, 255));
                         fmt.setFont(font);
                         QString sBlockText = QChar('A' + nBlockNumber);
                         nBlockNumber++;
                         cursor.insertText(QString("   %1   ").arg(sBlockText), fmt);
                         font.setUnderline(bUnderline);
                         fmt.setForeground(br);
                    }
                }
            }
            else if(blocks["blocks"][j]["type"].asString() == "image")
            {
                QString sImage = blocks["blocks"][j]["value"].asString().c_str();
                if(!(sImage.startsWith("http") || sImage.startsWith("data:image")))
                {
                    sImage = QString("temp/paper/attachment/")  + sImage;
                }

                if(sImage.startsWith("data:image"))
                {
                    QImage img;
                    img = QImage::fromData(QByteArray::fromBase64(CCommonTools::getImageRawBase64Str(sImage).toLatin1()));
                    if(blocks["blocks"][j]["param"].isMember("width") && !QString(blocks["blocks"][j]["param"]["width"].asString().c_str()).isEmpty() &&
                            !QString(blocks["blocks"][j]["param"]["height"].asString().c_str()).isEmpty())
                    {
                        img = img.scaled(QString(blocks["blocks"][j]["param"]["width"].asString().c_str()).toInt(),
                                QString(blocks["blocks"][j]["param"]["height"].asString().c_str()).toInt(),
                                Qt::IgnoreAspectRatio, Qt::SmoothTransformation);

                    }
                    cursor.insertImage(img);
                }
                else
                {
                    QImage img(sImage);
                    if(blocks["blocks"][j]["param"].isMember("width") && !QString(blocks["blocks"][j]["param"]["width"].asString().c_str()).isEmpty() &&
                            !QString(blocks["blocks"][j]["param"]["height"].asString().c_str()).isEmpty())
                    {
                        img = img.scaled(QString(blocks["blocks"][j]["param"]["width"].asString().c_str()).toInt(),
                                QString(blocks["blocks"][j]["param"]["height"].asString().c_str()).toInt(),
                                Qt::IgnoreAspectRatio, Qt::SmoothTransformation);

                    }
                    cursor.insertImage(img);
                }

//                QString strHtml = "";
//                if(blocks["blocks"][j]["param"].isMember("width"))
//                {
//                    strHtml = QString("<img src=\"%1\" height=\"%2\" width=\"%3\" align=\"bottom\" />")
//                            .arg(blocks["blocks"][j]["value"].asString().c_str())
//                            .arg(blocks["blocks"][j]["param"]["height"].asString().c_str())
//                            .arg(blocks["blocks"][j]["param"]["width"].asString().c_str());
//                }
//                else
//                {
//                    strHtml = "<img src=\"" + QString::fromLocal8Bit(blocks["blocks"][j]["value"].asString().c_str())+"\"  align=\"bottom\" />";
////                    strHtml = "<img src=\""+QString::fromLocal8Bit(blocks["blocks"][j]["value"].asString().c_str())+"\"  align=\"bottom\" />";
//                }
//                cursor.insertHtml(strHtml);
            }
        }
        if(i < nSize - 1)
        {
            cursor.insertText("\n");
        }
    }

    ui->tb_questionBody->adjustSize();

    ui->tb_questionBody->selectAll();
    ui->tb_questionBody->setAlignment(Qt::AlignJustify);
    cursor.movePosition(QTextCursor::End);
    ui->tb_questionBody->setTextCursor(cursor);

    ui->tb_questionBody->setFixedHeight(ui->tb_questionBody->document()->size().height()+g_appInfoPtr->m_fRate*5);
    setGeometry(0, 0, nWidth, ui->tb_questionBody->y() + ui->tb_questionBody->height() + g_appInfoPtr->m_fRate*10);
    return ui->tb_questionBody->y() + ui->tb_questionBody->height() + g_appInfoPtr->m_fRate*10;
}

int fbFillBlankBody::widgetType()
{
    return wt_fillBlankBody;
}

void fbFillBlankBody::setQuesionNo(QString sItemNo)
{
    QTextCursor cursor = ui->tb_questionBody->textCursor();
    QTextCharFormat fmt;
    QFont font;
    font.setFamily("Microsoft YaHei");
    font.setPixelSize(g_appInfoPtr->m_fRate*16);
    font.setWeight(QFont::Medium);
    fmt.setFont(font);
    cursor.insertText(sItemNo, fmt);
    ui->tb_questionBody->setTextCursor(cursor);
}

