#include "etextflipattr.h"
#include "ui_etextflipattr.h"
#include <QMessageBox>
#include <LoUIClass/x_uimsgboxok.h>
eTextFlipAttr::eTextFlipAttr(const eText::Data &data, QWidget *parent) :
    eAttr(parent),
    ui(new Ui::eTextFlipAttr)
{
    ui->setupUi(this);
    ui->timeEdit->setReadOnly(true);
    ui->timeEdit->setStyleSheet("background-color:#D5D5D5;");
    init(data);
}

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

void eTextFlipAttr::init(const eText::Data &data)
{
 //   ui->timeEdit->setVisible(false);
 //   ui->label_10->setVisible(false);
//    QMessageBox::warning(this, "Title", "eTextFlipAttr widget init");
    m_iPageCount=data.iPageCount;
    QTime aa(0,0,0);
    QTime bb = aa.addSecs(data.stTurning.playDuration);
    ui->timeEdit->setTime(bb);
    ui->timeEdit->setCurrentSectionIndex(2);

    QTime cc(0,0,0);
    QTime dd = cc.addSecs(data.stTurning.iEffectTime);
    ui->everyPageDuration->setTime(dd);
    ui->wEffectSpeed->setValue(data.stTurning.iEffectSpeed);
    ui->comboBox_effect->setCurrentIndex(returnComboBoxIndex(data.stTurning.strEffect));


    connect(ui->timeEdit,      SIGNAL(timeChanged(QTime)),        this, SIGNAL(sPlayDurationChanged(QTime)));
    connect(ui->comboBox_effect, SIGNAL(currentIndexChanged(int)), this, SLOT(OnEffectChanged(int)));
    connect(ui->wEffectSpeed,SIGNAL(valueChanged(int)),        this, SLOT(OnTurningEffectSpeedChanged(int)));
    connect(ui->everyPageDuration,SIGNAL(timeChanged(QTime)),        this, SLOT(OnPlayDurationChanged(QTime)));


}

void eTextFlipAttr::OnEffectChanged(int iEffect)
{
    QString strEffect=MACRO_EFFECT_STR_NO;
    switch (iEffect) {
    case 0:
        strEffect=MACRO_EFFECT_STR_NO;
        break;
    case 1:
        strEffect=MACRO_EFFECT_STR_RANDOM;
        break;
    case 2:
        strEffect=MACRO_EFFECT_STR_RIGHT_TO_LEFT;
        break;
    case 3:
        strEffect=MACRO_EFFECT_STR_BOTTOM_TO_TOP;
        break;
    case 4:
        strEffect=MACRO_EFFECT_STR_LEFT_TO_RIGHT;
        break;
    case 5:
        strEffect=MACRO_EFFECT_STR_TOP_TO_BOTTOM;
        break;
    default:
        strEffect=MACRO_EFFECT_STR_NO;
        break;
    }
    emit sEffectChanged(strEffect);
}
int eTextFlipAttr::returnComboBoxIndex(QString strEffect)
{
    if(strEffect==MACRO_EFFECT_STR_NO)
        return 0;
    else if(strEffect==MACRO_EFFECT_STR_RANDOM)
        return 1;
    else if(strEffect==MACRO_EFFECT_STR_RIGHT_TO_LEFT)
        return 2;
    else if(strEffect==MACRO_EFFECT_STR_BOTTOM_TO_TOP)
        return 3;
    else if(strEffect==MACRO_EFFECT_STR_LEFT_TO_RIGHT)
        return 4;
    else if(strEffect==MACRO_EFFECT_STR_TOP_TO_BOTTOM)
        return 5;
    else
        return 0;
}
void eTextFlipAttr::OnPageCountChanged(int iCount)
{
    m_iPageCount=iCount;
    QTime aa(0,0,0);
    int iSecs= aa.secsTo(ui->everyPageDuration->dateTime().time());

    QTime cc(0,0,0);
    QTime dd = cc.addSecs(iSecs*iCount);
    ui->timeEdit->setTime(dd);

}
void eTextFlipAttr::OnPlayDurationChanged(QTime t)
{
    OnPageCountChanged(m_iPageCount);
    QTime aa(0,0,0);
    int iSecs= aa.secsTo(t);
    int iSec=ui->wEffectSpeed->value();
    if(iSec>iSecs)
    {
        X_UIMsgBoxOk *dlg=new X_UIMsgBoxOk(tr("Tip Info"),tr("Effect time cannot be longer than duration time"),this,0);
        dlg->exec();
        QTime cc(0,0,0);
        QTime dd = cc.addSecs(iSec);
        ui->everyPageDuration->setTime(dd);
        ui->everyPageDuration->setFocus();
        QTime ddd = cc.addSecs(iSec*m_iPageCount);
        ui->timeEdit->setTime(ddd);
        emit sEveryPagePlayDurationChanged(dd);
        emit sPlayDurationChanged(ddd);

    }
    else {
        QTime cc(0,0,0);
        QTime ddd = cc.addSecs(iSecs*m_iPageCount);
        ui->timeEdit->setTime(ddd);
        emit sPlayDurationChanged(ddd);
        emit sEveryPagePlayDurationChanged(t);

    }

}
void eTextFlipAttr::OnTurningEffectSpeedChanged(int iSec)
{
    QTime aa(0,0,0);
    int iSecs=aa.secsTo(ui->everyPageDuration->time());
    if(iSec>iSecs)
    {
        X_UIMsgBoxOk *dlg=new X_UIMsgBoxOk(tr("Tip Info"),tr("Effect time cannot be longer than duration time"),this,0);
        dlg->exec();
        if(iSecs>1)
        {
            ui->wEffectSpeed->setValue(iSecs-1);
            emit sTurningEffectSpeedChanged(iSecs-1);

        }
        else {
            ui->wEffectSpeed->setValue(0);
            emit sTurningEffectSpeedChanged(0);

        }
        ui->wEffectSpeed->setFocus();
    }
    else {
        emit sTurningEffectSpeedChanged(iSec);
    }

}
