#include "eaclockattr.h"
#include "ui_eaclockattr.h"

eAClockAttr::eAClockAttr(const eAClock::Data &data, QWidget *parent) :
    eAttr(parent),
    ui(new Ui::eAClockAttr)
{
    ui->setupUi(this);
    QList<QByteArray> ids = QTimeZone::availableTimeZoneIds();
    foreach(QByteArray id, ids) {
        ui->wTimeZone->addItem(QString(id));
    }

    ui->wTimeZone         ->setCurrentText(data.timeZoneId);
    ui->wHourMark         ->setCurrentIndex(data.hourMark);
    ui->wHourMarkSize     ->setValue(data.hourMarkSize);
    ui->wHourMarkColor    ->setColor(data.hourMarkColor);
    ui->wMinMark          ->setCurrentIndex(data.minMark);
    ui->wMinMarkSize      ->setValue(data.minMarkSize);
    ui->wMinMarkColor     ->setColor(data.minMarkColor);
    ui->wHourHandColor    ->setColor(data.hourHandColor);
    ui->wMinHandColor     ->setColor(data.minHandColor);
    ui->wSecHandColor     ->setColor(data.secHandColor);
    ui->wDate             ->setChecked(data.date);
    ui->wDateFontFamily   ->setCurrentText(data.dateFontFamily);
    ui->wDateFontSize     ->setValue(data.dateFontSize);
    ui->wDateFontBold     ->setChecked(data.dateFontBold);
    ui->wDateFontItalics  ->setChecked(data.dateFontItalics);
    ui->wDateFontUnderline->setChecked(data.dateFontUnderline);
    ui->wDateColor        ->setColor(data.dateColor);
    ui->wText             ->setText(data.text);
    ui->wTextFontFamily   ->setCurrentText(data.textFontFamily);
    ui->wTextFontSize     ->setValue(data.textFontSize);
    ui->wTextFontBold     ->setChecked(data.textFontBold);
    ui->wTextFontItalics  ->setChecked(data.textFontItalics);
    ui->wTextFontUnderline->setChecked(data.textFontUnderline);
    ui->wTextColor        ->setColor(data.textColor);
    ui->wPlayDuration     ->setValue(data.playDuration);
    if(data.bCustomDial)
    {
        ui->lineEditBiaoPan->setText(data.path+MACRO_FENGEDANFU+data.name);
    }
    ui->wDate->setVisible(false);
    ui->wDateFontFamily->setVisible(false);
    ui->wDateFontSize->setVisible(false);
    ui->wDateFontBold->setVisible(false);
    ui->wDateFontUnderline->setVisible(false);
    ui->wDateFontItalics->setVisible(false);
    ui->wDateColor->setVisible(false);
    ui->wDateColor->setVisible(false);
    ui->groupBox_customDial->setChecked(data.bCustomDial);
    ui->groupBox_3->setVisible(!data.bCustomDial);
    if(data.bCustomDial)
    {
        ui->pushButtonOpenBiaoPanPic->setStyleSheet("background-color:#1c9ad2;");
        ui->pushButtonOpenBiaoPanPic->setStyleSheet("color:#000000;");
        ui->lineEditBiaoPan->setStyleSheet("background-color:#ffffff;");
        ui->lineEditBiaoPan->setStyleSheet("color:#000000;");

    }
    else {
        ui->pushButtonOpenBiaoPanPic->setStyleSheet("background-color:#d8d8d8;");
        ui->pushButtonOpenBiaoPanPic->setStyleSheet("color:#a8a8a8;");
        ui->lineEditBiaoPan->setStyleSheet("background-color:#d8d8d8;");
        ui->lineEditBiaoPan->setStyleSheet("color:#a8a8a8;");

    }
    connect(ui->wTimeZone,          SIGNAL(currentIndexChanged(int)),      this, SLOT(onAttrChanged()));
    connect(ui->wHourMark,          SIGNAL(currentIndexChanged(int)),      this, SLOT(onAttrChanged()));
    connect(ui->wHourMarkSize,      SIGNAL(valueChanged(int)),             this, SLOT(onAttrChanged()));
    connect(ui->wHourMarkColor,     SIGNAL(sColorChanged(const QColor &)), this, SLOT(onAttrChanged()));
    connect(ui->wMinMark,           SIGNAL(currentIndexChanged(int)),      this, SLOT(onAttrChanged()));
    connect(ui->wMinMarkSize,       SIGNAL(valueChanged(int)),             this, SLOT(onAttrChanged()));
    connect(ui->wMinMarkColor,      SIGNAL(sColorChanged(const QColor &)), this, SLOT(onAttrChanged()));
    connect(ui->wHourHandColor,     SIGNAL(sColorChanged(const QColor &)), this, SLOT(onAttrChanged()));
    connect(ui->wMinHandColor,      SIGNAL(sColorChanged(const QColor &)), this, SLOT(onAttrChanged()));
    connect(ui->wSecHandColor,      SIGNAL(sColorChanged(const QColor &)), this, SLOT(onAttrChanged()));
    connect(ui->wDate,              SIGNAL(toggled(bool)),                 this, SLOT(onAttrChanged()));
    connect(ui->wDateFontFamily,    SIGNAL(currentIndexChanged(int)),      this, SLOT(onAttrChanged()));
    connect(ui->wDateFontSize,      SIGNAL(valueChanged(int)),             this, SLOT(onAttrChanged()));
    connect(ui->wDateFontBold,      SIGNAL(toggled(bool)),                 this, SLOT(onAttrChanged()));
    connect(ui->wDateFontItalics,   SIGNAL(toggled(bool)),                 this, SLOT(onAttrChanged()));
    connect(ui->wDateFontUnderline, SIGNAL(toggled(bool)),                 this, SLOT(onAttrChanged()));
    connect(ui->wDateColor,         SIGNAL(sColorChanged(const QColor &)), this, SLOT(onAttrChanged()));
    connect(ui->wText,              SIGNAL(textChanged()),                 this, SLOT(onAttrChanged()));
    connect(ui->wTextFontFamily,    SIGNAL(currentIndexChanged(int)),      this, SLOT(onAttrChanged()));
    connect(ui->wTextFontSize,      SIGNAL(valueChanged(int)),             this, SLOT(onAttrChanged()));
    connect(ui->wTextFontBold,      SIGNAL(toggled(bool)),                 this, SLOT(onAttrChanged()));
    connect(ui->wTextFontItalics,   SIGNAL(toggled(bool)),                 this, SLOT(onAttrChanged()));
    connect(ui->wTextFontUnderline, SIGNAL(toggled(bool)),                 this, SLOT(onAttrChanged()));
    connect(ui->wTextColor,         SIGNAL(sColorChanged(const QColor &)), this, SLOT(onAttrChanged()));
    connect(ui->wPlayDuration,      SIGNAL(valueChanged(int)),             this, SLOT(onAttrChanged()));
    connect(ui->pushButtonOpenBiaoPanPic,      SIGNAL(clicked()),             this, SLOT(onOpenBiaoPanPic()));
    connect(ui->groupBox_customDial,   SIGNAL(toggled(bool)),                 this, SLOT(onSetGroup3Visiable(bool)));

}

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

void eAClockAttr::onSetGroup3Visiable(bool b)
{
    ui->groupBox_3->setVisible(!b);
    if(b)
    {
        ui->pushButtonOpenBiaoPanPic->setStyleSheet("background-color:#1c9ad2;");
        ui->pushButtonOpenBiaoPanPic->setStyleSheet("color:#000000;");
        ui->lineEditBiaoPan->setStyleSheet("background-color:#ffffff;");
        ui->lineEditBiaoPan->setStyleSheet("color:#000000;");

    }
    else {
        ui->pushButtonOpenBiaoPanPic->setStyleSheet("background-color:#d8d8d8;");
        ui->pushButtonOpenBiaoPanPic->setStyleSheet("color:#a8a8a8;");
        ui->lineEditBiaoPan->setStyleSheet("background-color:#d8d8d8;");
        ui->lineEditBiaoPan->setStyleSheet("color:#a8a8a8;");

    }

    onAttrChanged();
}
void eAClockAttr::onAttrChanged()
{
    eAClock::Data data;
    data.timeZoneId        = QByteArray().append(ui->wTimeZone->currentText());
    data.hourMark          = ui->wHourMark->currentIndex();
    data.hourMarkSize      = ui->wHourMarkSize->value();
    data.hourMarkColor     = ui->wHourMarkColor->color();
    data.minMark           = ui->wMinMark->currentIndex();
    data.minMarkSize       = ui->wMinMarkSize->value();
    data.minMarkColor      = ui->wMinMarkColor->color();
    data.hourHandColor     = ui->wHourHandColor->color();
    data.minHandColor      = ui->wMinHandColor->color();
    data.secHandColor      = ui->wSecHandColor->color();
    data.date              = ui->wDate->isChecked();
    data.dateFontFamily    = ui->wDateFontFamily->currentText();
    data.dateFontSize      = ui->wDateFontSize->value();
    data.dateFontBold      = ui->wDateFontBold->isChecked();
    data.dateFontItalics   = ui->wDateFontItalics->isChecked();
    data.dateFontUnderline = ui->wDateFontUnderline->isChecked();
    data.dateColor         = ui->wDateColor->color();
    data.text              = ui->wText->toPlainText();
    data.textFontFamily    = ui->wTextFontFamily->currentText();
    data.textFontSize      = ui->wTextFontSize->value();
    data.textFontBold      = ui->wTextFontBold->isChecked();
    data.textFontItalics   = ui->wTextFontItalics->isChecked();
    data.textFontUnderline = ui->wTextFontUnderline->isChecked();
    data.textColor         = ui->wTextColor->color();
    data.playDuration      = ui->wPlayDuration->value();
    data.bCustomDial       = ui->groupBox_customDial->isChecked();
    QString file=ui->lineEditBiaoPan->text();
    QFileInfo fi(file);
    if(fi.exists())
    {
        data.name = fi.fileName().replace(MACRO_DANYINFANXIEGAN, MACRO_DANYINXIEGAN);
        data.path = fi.absolutePath().replace(MACRO_DANYINFANXIEGAN, MACRO_DANYINXIEGAN);

    }
    else {
        data.name = "";
        data.path = "";
    }
    emit sAttrChanged(data);
}
void eAClockAttr::onOpenBiaoPanPic()
{
    QString s = QFileDialog::getOpenFileName(this, tr("Select Dail file"),
                   "./AClock/",
                   "Dail files(*.png)");
    if(!s.isNull()) {
         ui->lineEditBiaoPan->setText(s);
        onAttrChanged();
    }
}
