#include "etextattr.h"
#include "ui_etextattr.h"

#include "qstackedwidget.h"
#include <QListWidget>
#include <QMessageBox>
eTextAttr::eTextAttr(const eText::Data &data, QWidget *parent) :
    eAttr(parent),
    ui(new Ui::eTextAttr)
{
    ui->setupUi(this);
    //创建动态页面控件
    m_pStackedW = new QStackedWidget;
    m_pTextFlipAttr = new eTextFlipAttr(data,this);
    m_pTextScrollAttr = new eTextScrollAttr(data,this);
    m_pTextStaticAttr = new eTextStaticAttr(data,this);
    m_pStackedW->addWidget(m_pTextFlipAttr);
    m_pStackedW->addWidget(m_pTextScrollAttr);
    m_pStackedW->addWidget(m_pTextStaticAttr);
    ui->horizontalLayout_playmethod->addWidget(m_pStackedW);
    // Play
    ui->wPlayStyle->setId(ui->wPlayTurning, eText::Turning);
    ui->wPlayStyle->setId(ui->wPlayRolling, eText::Rolling);
    ui->wPlayStyle->setId(ui->wPlayStatic,  eText::Static);

    // Widget
    ui->wColorSetting->setId(ui->wTextColor,       0);
    ui->wColorSetting->setId(ui->wTextShadowColor, 1);
    ui->wColorSetting->setId(ui->wBackgroundColor, 2);
    ui->wTextAlignH  ->setId(ui->wTextAlignHL,     Qt::AlignLeft);
    ui->wTextAlignH  ->setId(ui->wTextAlignHC,     Qt::AlignHCenter);
    ui->wTextAlignH  ->setId(ui->wTextAlignHR,     Qt::AlignRight);
    ui->wTextAlignV  ->setId(ui->wTextAlignVT,     Qt::AlignTop);
    ui->wTextAlignV  ->setId(ui->wTextAlignVC,     Qt::AlignVCenter);
    ui->wTextAlignV  ->setId(ui->wTextAlignVB,     Qt::AlignBottom);

    QString str=QString::number(data.iPageCount);
    ui->label_pageCount->setText(str);
    ui->spinBoxPagePreview->setRange(1,data.iPageCount);
    ui->label_2->setVisible(false);
    ui->wTextShadowColor->setVisible(false);
    connect(ui->wPlayStyle,SIGNAL(buttonClicked(int)),this, SIGNAL(sPlayStyleChanged(int)));
    //绑定信号和槽函数
    connect(ui->wPlayStyle,SIGNAL(buttonClicked(int)),m_pStackedW, SLOT(setCurrentIndex(int)));

    init(data);

    ui->wTextEdit->setAcceptRichText(false);
    //文本的预览窗口，只要接收textEdit控件的textchanged事件，保存tohtml的字符串内容即可。
    connect(ui->wTextEdit, SIGNAL(textChanged()), this, SLOT(onTextChanged()));
    connect(ui->wFontFamily,        SIGNAL(currentFontChanged(const QFont &)), this, SLOT(textFamily(const QFont &)));
    connect(ui->wFontSize,          SIGNAL(valueChanged(int)),                 this, SLOT(textSize(int)));
    connect(ui->wFontBold,          SIGNAL(toggled(bool)),                     this, SLOT(textBold(bool)));
    connect(ui->wFontItalics,       SIGNAL(toggled(bool)),                     this, SLOT(textItalic(bool)));
    connect(ui->wFontUnderline,     SIGNAL(toggled(bool)),                     this, SLOT(textUnderline(bool)));
    connect(ui->wTextColor,         SIGNAL(sColorChanged(const QColor &)),     this, SLOT(textColor(const QColor &)));
    connect(ui->wTextShadowColor,   SIGNAL(sColorChanged(const QColor &)),     this, SLOT(TextShadowColor(const QColor &)));
    connect(ui->wBackgroundColor,   SIGNAL(sColorChanged(const QColor &)),     this, SLOT(BackgroundColor(const QColor &)));
    connect(ui->wTextAlignH,        SIGNAL(buttonClicked(int)),                this, SIGNAL(sTextHAlignChanged(int)));
    connect(ui->wTextAlignV,        SIGNAL(buttonClicked(int)),                this, SIGNAL(sTextVAlignChanged(int)));
    connect(ui->wTextLetterSpacing, SIGNAL(valueChanged(int)),                 this, SIGNAL(sTextLetterSpacingChanged(int)));
    connect(ui->spinBoxPagePreview, SIGNAL(valueChanged(int)),                 this, SIGNAL(sPageIndexPreview(int)));
    connect(ui->wTextLineSpacing,   SIGNAL(valueChanged(int)),                 this, SIGNAL(sTextLineSpacingChanged(int)));
    connect(this,SIGNAL(sPageCountChanged(int)),m_pTextFlipAttr,SLOT(OnPageCountChanged(int)));

    setFocus();

}
void eTextAttr::OnPageIndexPreview(int  index)
{
    //int i=index;
    Q_UNUSED(index)
}
eTextAttr::~eTextAttr()
{
    m_pStackedW->deleteLater()  ;
    m_pTextFlipAttr->deleteLater() ;
    m_pTextScrollAttr->deleteLater()  ;
    m_pTextStaticAttr->deleteLater()  ;
    delete ui;

}

void eTextAttr::init(const eText::Data &data)
{
    QString strStyleSheet = QString("background-color: #2a2a2a;color: rgba(255,255,255,1)");
    ui->wTextEdit->setStyleSheet( strStyleSheet );
    ui->wTextEdit->setTextColor(Qt::white);
    BackgroundColor(data.cBackground);
    ui->wTextColor->setColor(Qt::white);
    ui->wTextShadowColor->setColor(Qt::green);
    ui->wBackgroundColor->setColor(Qt::transparent);
    ui->wTextLetterSpacing->setValue      (static_cast<int>(data.wordSpacing));
    ui->wFontSize->setValue(16);
    ui->wFontSize->setRange(7,500);
    unsigned int align = data.opt.alignment();
    int h_align = align & Qt::AlignHorizontal_Mask;
    switch (h_align) {
    case Qt::AlignLeft:    ui->wTextAlignHL->setChecked(true); break;
    case Qt::AlignHCenter: ui->wTextAlignHC->setChecked(true); break;
    case Qt::AlignRight:   ui->wTextAlignHR->setChecked(true); break;
    default: break;
    }
    int v_align = align & Qt::AlignVertical_Mask;
    switch (v_align) {
    case Qt::AlignTop:     ui->wTextAlignVT->setChecked(true); break;
    case Qt::AlignVCenter: ui->wTextAlignVC->setChecked(true); break;
    case Qt::AlignBottom:  ui->wTextAlignVB->setChecked(true); break;
    default: break;
    }
    ui->wTextEdit->setText(data.text);
    ui->wBackgroundColor->setColor(data.cBackground);
    ui->wTextLineSpacing->setValue(data.lineSpacing);
    int playStyle = data.playStyle;
    switch (playStyle) {
    case eText::Turning:
        ui->wPlayTurning->setChecked(true);
        break;
    case eText::Rolling:
        ui->wPlayRolling->setChecked(true);
        break;
    case eText::Static:
        ui->wPlayStatic ->setChecked(true);
        break;
    default: break;
    }
    m_pStackedW->setCurrentIndex(playStyle);

}

void eTextAttr::onFontFamilySetting(const QFont &font)
{
    emit sFontFamilyChanged(font.family());
}
void eTextAttr::onTextChanged()
{
    //emit sTextChanged(ui->wTextEdit->toPlainText());
    QString aa=ui->wTextEdit->toPlainText();
    if(aa.isEmpty())
    {
        ui->wTextEdit->selectAll();
        QTextCharFormat fmt;
        fmt.setForeground(Qt::white);
        mergeFormatOnWordOrSelection(fmt);
    }
     emit sTextRickChanged(ui->wTextEdit->toHtml());
}
///////////////////////////////////////富文本
void eTextAttr::textUnderline(bool b)
{
    QTextCharFormat fmt;
    fmt.setFontUnderline(b);
    mergeFormatOnWordOrSelection(fmt);
}


void eTextAttr::textBold(bool b)
{
    QTextCharFormat fmt;
    fmt.setFontWeight(b ? QFont::Bold : QFont::Normal);
    mergeFormatOnWordOrSelection(fmt);
}

void eTextAttr::textItalic(bool b)
{
    QTextCharFormat fmt;
    fmt.setFontItalic(b);
    mergeFormatOnWordOrSelection(fmt);
}

void eTextAttr::textFamily(const QFont &f)
{
    QTextCharFormat fmt;
    fmt.setFontFamily(f.family());
    mergeFormatOnWordOrSelection(fmt);
}

void eTextAttr::textSize(int iFontSize)
{
    if (iFontSize > 0) {
        QTextCharFormat fmt;
        fmt.setFontPointSize(iFontSize);
        mergeFormatOnWordOrSelection(fmt);
    }
}
void eTextAttr::mergeFormatOnWordOrSelection(const QTextCharFormat &format)
{
    QTextCursor cursor = ui->wTextEdit->textCursor();
    if (!cursor.hasSelection())
        cursor.select(QTextCursor::WordUnderCursor);
    cursor.mergeCharFormat(format);
    ui->wTextEdit->mergeCurrentCharFormat(format);
}
void eTextAttr::textColor(const QColor & col)
{
    if (!col.isValid())
        return;
    QTextCharFormat fmt;
    fmt.setForeground(col);
    mergeFormatOnWordOrSelection(fmt);
}
void eTextAttr::TextShadowColor(const QColor & col)
{
    if (!col.isValid())
        return;
    QTextCharFormat fmt;
    fmt.setBackground(col);
    mergeFormatOnWordOrSelection(fmt);
}
void eTextAttr::BackgroundColor(const QColor & col)
{
    if (!col.isValid())
        return;
    emit sBackgroundColorChanged(col);
//    QString strClr = col.name(); // "#RRGGBB"
//    if(col.alpha()==0)//&&(col.red()!=0||col.green()!=0||col.blue()!=0))
//    {
//        QString strStyleSheet = QString("background-color: #000000").arg(strClr);
////        QColor aa;
////        aa.setRgb(col.red(),col.green(),col.blue());
//        ui->wTextEdit->setStyleSheet( strStyleSheet );
//        emit sBackgroundColorChanged(col);
//    }
//    else {
//        QString strStyleSheet = QString("background-color: #000000").arg(strClr);
//        QString strStyleSheet = QString("background-color: %1").arg( strClr );
//        ui->wTextEdit->setStyleSheet( strStyleSheet );
//        emit sBackgroundColorChanged(col);

//    }

}
void eTextAttr::OnPageCountChanged(int icount)
{
    QString str=QString::number(icount);
    ui->label_pageCount->setText(str);
    ui->spinBoxPagePreview->setRange(1,icount);
    ui->spinBoxPagePreview->setValue(1);
    emit sPageCountChanged(icount);
}
