#include "etimerattr.h"
#include "ui_etimerattr.h"

eTimerAttr::eTimerAttr(const eTimer::Data &data, QWidget *parent) :
    eAttr(parent),
    ui(new Ui::eTimerAttr)
{
    ui->setupUi(this);

    if(data.xWard == 0) {
        ui->wForward->setChecked(true);
    } else {
        ui->wBackward->setChecked(true);
    }
    ui->wDate           ->setDate(data.targetDateTime.date());
    ui->wTime           ->setTime(data.targetDateTime.time());
    ui->wDay            ->setChecked(data.day);
    ui->wHour           ->setChecked(data.hour);
    ui->wMin            ->setChecked(data.min);
    ui->wSec            ->setChecked(data.sec);
    ui->wText           ->setPlainText(data.text);
    ui->wMultiline      ->setChecked(data.multiline);
    ui->wFontFamily     ->setCurrentText(data.fontFamily);
    ui->wFontSize       ->setValue(data.fontSize);
    ui->wFontBold       ->setChecked(data.fontBold);
    ui->wFontItalics    ->setChecked(data.fontItalics);
    ui->wFontUnderline  ->setChecked(data.fontUnderline);
    ui->wTextColor      ->setColor(data.textColor);
    ui->wBackgroundColor->setColor(data.backgroundColor);
    ui->wPlayDuration   ->setValue(data.playDuration);

    ui->gTimingStyle->setId(ui->wForward,  0);
    ui->gTimingStyle->setId(ui->wBackward, 1);
    connect(ui->gTimingStyle    ,SIGNAL(buttonToggled(int,bool)),          this, SLOT(onStyleChanged(int,bool)));
    connect(ui->wDate           ,SIGNAL(dateChanged(const QDate&)),        this, SLOT(onAttrChanged()));
    connect(ui->wTime           ,SIGNAL(timeChanged(const QTime&)),        this, SLOT(onAttrChanged()));
    connect(ui->wDay            ,SIGNAL(toggled(bool)),                    this, SLOT(onAttrChanged()));
    connect(ui->wHour           ,SIGNAL(toggled(bool)),                    this, SLOT(onAttrChanged()));
    connect(ui->wMin            ,SIGNAL(toggled(bool)),                    this, SLOT(onAttrChanged()));
    connect(ui->wSec            ,SIGNAL(toggled(bool)),                    this, SLOT(onAttrChanged()));
    connect(ui->wText           ,SIGNAL(textChanged()),                    this, SLOT(onAttrChanged()));
    connect(ui->wMultiline      ,SIGNAL(toggled(bool)),                    this, SLOT(onAttrChanged()));
    connect(ui->wFontFamily     ,SIGNAL(currentFontChanged(const QFont&)), this, SLOT(onAttrChanged()));
    connect(ui->wFontSize       ,SIGNAL(valueChanged(int)),                this, SLOT(onAttrChanged()));
    connect(ui->wFontBold       ,SIGNAL(toggled(bool)),                    this, SLOT(onAttrChanged()));
    connect(ui->wFontItalics    ,SIGNAL(toggled(bool)),                    this, SLOT(onAttrChanged()));
    connect(ui->wFontUnderline  ,SIGNAL(toggled(bool)),                    this, SLOT(onAttrChanged()));
    connect(ui->wTextColor      ,SIGNAL(sColorChanged(const QColor&)),     this, SLOT(onAttrChanged()));
    connect(ui->wBackgroundColor,SIGNAL(sColorChanged(const QColor&)),     this, SLOT(onAttrChanged()));
    connect(ui->wPlayDuration   ,SIGNAL(valueChanged(int)),                this, SLOT(onAttrChanged()));

    connect(ui->wDateSelector, SIGNAL(sDateSelected(const QDate&)), ui->wDate,  SLOT(setDate(const QDate&)));
}

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

void eTimerAttr::onStyleChanged(int id, bool f)
{
    Q_UNUSED(id);
    if(f == true) {
        emit onAttrChanged();
    }
}

void eTimerAttr::onAttrChanged()
{
    eTimer::Data data;
    data.xWard           = ui->wForward->isChecked() ? 0 : 1;
    data.targetDateTime  = QDateTime(ui->wDate->date(), ui->wTime->time());
    data.day             = ui->wDay->isChecked();
    data.hour            = ui->wHour->isChecked();
    data.min             = ui->wMin->isChecked();
    data.sec             = ui->wSec->isChecked();
    data.text            = ui->wText->toPlainText();
    data.multiline       = ui->wMultiline->isChecked();
    data.fontFamily      = ui->wFontFamily->currentText();
    data.fontSize        = ui->wFontSize->value();
    data.fontBold        = ui->wFontBold->isChecked();
    data.fontItalics     = ui->wFontItalics->isChecked();
    data.fontUnderline   = ui->wFontUnderline->isChecked();
    data.textColor       = ui->wTextColor->color();
    data.backgroundColor = ui->wBackgroundColor->color();
    data.playDuration    = ui->wPlayDuration->value();
    emit sAttrChanged(data);
}
