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

#include "CAppInfo.h"
#include "CCommonTools.h"
#include "logproc.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("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());
                    }

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


    ui->tb_option->selectAll();
    ui->tb_option->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()+g_appInfoPtr->m_fRate*7);
    setGeometry(0, 0, nWidth, ui->tb_option->y() + ui->tb_option->height() + g_appInfoPtr->m_fRate*7);
    return ui->tb_option->y() + ui->tb_option->height() + g_appInfoPtr->m_fRate*7;
}
