#include "eweatherattr.h"
#include "ui_eweatherattr.h"

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

    ui->wCountry->setCurrentIndex(0);
    ui->wProvince->setVisible(true);
    ui->wCity->setVisible(true);
    ui->wState->setVisible(false);
    ui->wDistrict->setVisible(false);
    ui->wLabelProvince->setVisible(true);
    ui->wLabelCity->setVisible(true);
    ui->wLabelState->setVisible(false);
    ui->wLabelDistrict->setVisible(false);
    connect(ui->wCountry, SIGNAL(currentIndexChanged(int)), this, SLOT(onCountrySwitched(int)));

    ui->wCountry->setCurrentIndex(data.country);
    switch (data.country) {
    case 0:
        ui->wProvince->setCurrentIndex(data.region1);
        ui->wCity->setCurrentIndex(data.region2);
        break;
    case 1:
        ui->wState->setCurrentIndex(data.region1);
        ui->wDistrict->setCurrentIndex(data.region2);
        break;
    default:
        break;
    }
    ui->wWeather->setChecked(data.weather);
    ui->wTemp->setChecked(data.temp);
    ui->wWind->setChecked(data.wind);
    ui->wHumi->setChecked(data.humidity);
    ui->wCurTemp->setChecked(data.curTemp);
    if(data.tempType == 0) {
        ui->wCelsius->setChecked(true);
    } else {
        ui->wFahrenheit->setChecked(true);
    }
    if(data.tempUnitType == 0) {
        ui->wDegreeX->setChecked(true);
    } else {
        ui->wDegree->setChecked(true);
    }
    ui->wLabelWeather->setText(data.labelWeather);
    ui->wLabelTemp->setText(data.labelTemp);
    ui->wLabelWind->setText(data.labelWind);
    ui->wLabelHumi->setText(data.labelHumidity);
    ui->wLabelCurTemp->setText(data.labelCurTemp);
    switch(data.lineStyle) {
    case 0: ui->wMultiline->setChecked(true);  break;
    case 1: ui->wScrollline->setChecked(true); break;
    case 2: ui->wStaticline->setChecked(true); break;
    default: break;
    }
    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->wColor->setColor(data.textColor);
    ui->wPlayRefresh->setValue(data.playRefresh);
    ui->wPlayDuration->setValue(data.playDuration);

    connect(ui->wCountry,       SIGNAL(currentIndexChanged(int)),     this, SLOT(onAttrChanged()));
    connect(ui->wProvince,      SIGNAL(currentIndexChanged(int)),     this, SLOT(onAttrChanged()));
    connect(ui->wCity,          SIGNAL(currentIndexChanged(int)),     this, SLOT(onAttrChanged()));
    connect(ui->wState,         SIGNAL(currentIndexChanged(int)),     this, SLOT(onAttrChanged()));
    connect(ui->wDistrict,      SIGNAL(currentIndexChanged(int)),     this, SLOT(onAttrChanged()));
    connect(ui->wWeather,       SIGNAL(toggled(bool)),                this, SLOT(onAttrChanged()));
    connect(ui->wTemp,          SIGNAL(toggled(bool)),                this, SLOT(onAttrChanged()));
    connect(ui->wWind,          SIGNAL(toggled(bool)),                this, SLOT(onAttrChanged()));
    connect(ui->wHumi,          SIGNAL(toggled(bool)),                this, SLOT(onAttrChanged()));
    connect(ui->wCurTemp,       SIGNAL(toggled(bool)),                this, SLOT(onAttrChanged()));
    connect(ui->wCelsius,       SIGNAL(toggled(bool)),                this, SLOT(onRadioChecked(bool)));
    connect(ui->wFahrenheit,    SIGNAL(toggled(bool)),                this, SLOT(onRadioChecked(bool)));
    connect(ui->wDegreeX,       SIGNAL(toggled(bool)),                this, SLOT(onRadioChecked(bool)));
    connect(ui->wDegree,        SIGNAL(toggled(bool)),                this, SLOT(onRadioChecked(bool)));
    connect(ui->wLabelWeather,  SIGNAL(textChanged(const QString &)), this, SLOT(onAttrChanged()));
    connect(ui->wLabelTemp,     SIGNAL(textChanged(const QString &)), this, SLOT(onAttrChanged()));
    connect(ui->wLabelWind,     SIGNAL(textChanged(const QString &)), this, SLOT(onAttrChanged()));
    connect(ui->wLabelHumi,     SIGNAL(textChanged(const QString &)), this, SLOT(onAttrChanged()));
    connect(ui->wLabelCurTemp,  SIGNAL(textChanged(const QString &)), this, SLOT(onAttrChanged()));
    connect(ui->wMultiline,     SIGNAL(toggled(bool)),                this, SLOT(onRadioChecked(bool)));
    connect(ui->wScrollline,    SIGNAL(toggled(bool)),                this, SLOT(onRadioChecked(bool)));
    connect(ui->wStaticline,    SIGNAL(toggled(bool)),                this, SLOT(onRadioChecked(bool)));
    connect(ui->wFontFamily,    SIGNAL(currentIndexChanged(int)),     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->wColor,         SIGNAL(sColorChanged(const QColor&)), this, SLOT(onAttrChanged()));
    connect(ui->wPlayRefresh,   SIGNAL(valueChanged(int)),            this, SLOT(onAttrChanged()));
    connect(ui->wPlayDuration,  SIGNAL(valueChanged(int)),            this, SLOT(onAttrChanged()));
}

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

void eWeatherAttr::onAttrChanged()
{
    eWeather::Data data;
    data.country = ui->wCountry->currentIndex();
    switch (data.country) {
    case 0:
        data.region1 = ui->wProvince->currentIndex();
        data.region2 = ui->wCity->currentIndex();
        break;
    case 1:
        data.region1 = ui->wState->currentIndex();
        data.region2 = ui->wDistrict->currentIndex();
        break;
    default:
        break;
    }
    data.weather = ui->wWeather->isChecked();
    data.temp = ui->wTemp->isChecked();
    data.wind = ui->wWind->isChecked();
    data.humidity = ui->wHumi->isChecked();
    data.curTemp = ui->wCurTemp->isChecked();
    if(ui->wCelsius->isChecked()) {
        data.tempType = 0;
    } else {
        data.tempType = 1;
    }
    if(ui->wDegreeX->isChecked()) {
        data.tempUnitType = 0;
    } else {
        data.tempUnitType = 1;
    }
    data.labelWeather = ui->wLabelWeather->text();
    data.labelTemp = ui->wLabelTemp->text();
    data.labelWind = ui->wLabelWind->text();
    data.labelHumidity = ui->wLabelHumi->text();
    data.labelCurTemp = ui->wLabelCurTemp->text();
    if(ui->wMultiline->isChecked()) {
        data.lineStyle = 0;
     } else if(ui->wScrollline->isChecked()) {
        data.lineStyle = 1;
    } else {
        data.lineStyle = 2;
    }
    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->wColor->color();
    data.playRefresh = ui->wPlayRefresh->value();
    data.playDuration = ui->wPlayDuration->value();
    emit sAttrChanged(data);
}

void eWeatherAttr::onCelsiusChecked(bool f)    { if(f) ui->wDegreeX->setText(QStringLiteral("℃")); }
void eWeatherAttr::onFahrenheitChecked(bool f) { if(f) ui->wDegreeX->setText(QStringLiteral("℉")); }
void eWeatherAttr::onCountrySwitched(int i)
{
    switch (i) {
    case 0:
        ui->wProvince->setVisible(true);
        ui->wCity->setVisible(true);
        ui->wState->setVisible(false);
        ui->wDistrict->setVisible(false);
        ui->wLabelProvince->setVisible(true);
        ui->wLabelCity->setVisible(true);
        ui->wLabelState->setVisible(false);
        ui->wLabelDistrict->setVisible(false);
        break;

    case 1:
        ui->wProvince->setVisible(false);
        ui->wCity->setVisible(false);
        ui->wState->setVisible(true);
        ui->wDistrict->setVisible(true);
        ui->wLabelProvince->setVisible(false);
        ui->wLabelCity->setVisible(false);
        ui->wLabelState->setVisible(true);
        ui->wLabelDistrict->setVisible(true);
        break;

    default:
        break;
    }
}
