#include "optionBody.h"
#include "ui_optionBody.h"

#include "CAppInfo.h"

optionBody::optionBody(Json::Value jBody, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::optionBody)
{
    ui->setupUi(this);

    setAttribute(Qt::WA_DeleteOnClose);

    m_jBody = jBody;
}

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

int optionBody::setUI(const int nWidth)
{
    setGeometry(0, 0, nWidth, g_appInfoPtr->m_fRate*300);
    ui->tb_option->setGeometry(0, 0, nWidth, g_appInfoPtr->m_fRate*300);
    ui->tb_option->setFixedWidth(nWidth);
    QTextCursor cursor = ui->tb_option->textCursor();
    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*14);
                font.setWeight(QFont::Normal);
                if(blocks["blocks"][j].isMember("param"))
                {
                    if(blocks["blocks"][j]["param"].isMember("font"))
                    {
                        if(blocks["blocks"][j]["param"]["font"].isMember("bold"))
                        {
                            font.setBold(blocks["blocks"][j]["param"]["font"]["bold"].asBool());
                        }

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

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

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

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

                        if(blocks["blocks"][j]["param"]["font"].isMember("name"))
                        {
                            font.setFamily(blocks["blocks"][j]["param"]["font"]["name"].asString().c_str());
                        }
                    }
                }
                fmt.setFont(font);
                cursor.insertText(blocks["blocks"][j]["value"].asString().c_str(), fmt);
            }
            else if(blocks["blocks"][j]["type"].asString() == "image")
            {
                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\" />";
                }
                cursor.insertHtml(strHtml);
            }
        }
        if(i < nSize - 1)
        {
            cursor.insertText("\n");
        }
    }


//    ui->tb_opstion->selectAll();
//    ui->tb_opstion->setAlignment(Qt::AlignJustify);

    ui->tb_option->adjustSize();
    cursor.movePosition(QTextCursor::End);
    ui->tb_option->setTextCursor(cursor);

    ui->tb_option->setFixedHeight(ui->tb_option->document()->size().height()+7);
    setGeometry(0, 0, nWidth, ui->tb_option->y() + ui->tb_option->height() + 7);
    return ui->tb_option->y() + ui->tb_option->height() + 7;
}
