#include "settingdatas.h"
#include "ui_settingdatas.h"
#include "src/frame/configuration.h"
#include "src/frame/logger.h"
#include "src/po/translation.h"
#include "src/view/modaldialog.h"

SettingDatas::SettingDatas(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SettingDatas)
{
    QFont f;
    ui->setupUi(this);
    f = ui->title->font();
    f.setPixelSize(16);
    f.setBold(true);
    ui->title->setFont(f);
    connect(this, SIGNAL(title(QString)), ui->title, SLOT(setText(QString)));
    f = ui->labelAddData->font();
    f.setPixelSize(14);
    f.setWeight(60);
    ui->labelAddData->setFont(f);
    ui->labelDeleteData->setFont(f);
    ui->labelCleanData->setFont(f);
    ui->label->setFont(f);

    ui->labelAddData->setText(_("Add Data"));
    ui->checkBoxAddAll->setFont(f);


    ui->checkBoxAddAll->setText(_("All"));
    ui->checkBoxAddCurrent->setFont(f);
    ui->checkBoxAddCurrent->setText(_("Current"));

    ui->checkBoxAddAll->setCheckable(true);
    ui->checkBoxAddAll->setAutoExclusive(true);
    ui->checkBoxAddCurrent->setCheckable(true);
    ui->checkBoxAddCurrent->setAutoExclusive(true);

    ui->labelDeleteData->setText(_("Delete Data"));
    ui->checkBoxDeleteAll->setFont(f);
    ui->checkBoxDeleteAll->setText(_("All"));
    ui->checkBoxDeleteCurrent->setFont(f);
    ui->checkBoxDeleteCurrent->setText(_("Current"));

    ui->checkBoxDeleteAll->setCheckable(true);
    ui->checkBoxDeleteAll->setAutoExclusive(true);
    ui->checkBoxDeleteCurrent->setCheckable(true);
    ui->checkBoxDeleteCurrent->setAutoExclusive(true);

    ui->labelCleanData->setText(_("Clean Data"));
    ui->checkBoxCleanAll->setFont(f);
    ui->checkBoxCleanAll->setText(_("All"));
    ui->checkBoxCleanCurrent->setFont(f);
    ui->checkBoxCleanCurrent->setText(_("Current"));

    ui->checkBoxCleanAll->setCheckable(true);
    ui->checkBoxCleanAll->setAutoExclusive(true);
    ui->checkBoxCleanCurrent->setCheckable(true);
    ui->checkBoxCleanCurrent->setAutoExclusive(true);

    ui->label->setFont(f);
    ui->label->setText(_("Duplicate Data"));
    ui->checkBoxRename->setFont(f);
    ui->checkBoxRename->setText(_("Rename"));
    ui->checkBoxAddSuffix->setFont(f);
    ui->checkBoxAddSuffix->setText(_("Add Suffix"));
    ui->checkBoxRepleace->setFont(f);
    ui->checkBoxRepleace->setText(_("Replace"));

    ui->checkBoxRename->setCheckable(true);
    ui->checkBoxRename->setAutoExclusive(true);
    ui->checkBoxAddSuffix->setCheckable(true);
    ui->checkBoxAddSuffix->setAutoExclusive(true);
    ui->checkBoxRepleace->setCheckable(true);
    ui->checkBoxRepleace->setAutoExclusive(true);

    connect(ui->checkBoxAddAll, SIGNAL(clicked(bool)), this, SLOT(check()));
    connect(ui->checkBoxAddCurrent, SIGNAL(clicked(bool)), this, SLOT(check()));
    connect(ui->checkBoxDeleteAll, SIGNAL(clicked(bool)), this, SLOT(check()));
    connect(ui->checkBoxDeleteCurrent, SIGNAL(clicked(bool)), this, SLOT(check()));
    connect(ui->checkBoxCleanAll, SIGNAL(clicked(bool)), this, SLOT(check()));
    connect(ui->checkBoxCleanCurrent, SIGNAL(clicked(bool)), this, SLOT(check()));
    connect(ui->checkBoxRename, SIGNAL(clicked(bool)), this, SLOT(check()));
    connect(ui->checkBoxRepleace, SIGNAL(clicked(bool)), this, SLOT(check()));
    connect(ui->checkBoxAddSuffix, SIGNAL(clicked(bool)), this, SLOT(check()));
}

void SettingDatas::load()
{
    title(_("Data Setting"));
    m_iDuplicate = CFG->get("DataSetting", "Duplicate Data", 0).toInt();
    switch (m_iDuplicate)
    {
    case 0:
        ui->checkBoxRename->setChecked(true);
        break;
    case 1:
        ui->checkBoxAddSuffix->setChecked(true);
        break;
    default:
        ui->checkBoxRepleace->setChecked(true);
        if (m_iDuplicate > 2) m_iDuplicate = 2;
        break;
    }
    m_bAdd = CFG->get("DataSetting", "Add Data", true).toBool();
    ui->checkBoxAddAll->setChecked(m_bAdd);
    ui->checkBoxAddCurrent->setChecked(!m_bAdd);
    m_bDelete = CFG->get("DataSetting", "Delete Data", false).toBool();
    ui->checkBoxDeleteAll->setChecked(m_bDelete);
    ui->checkBoxDeleteCurrent->setChecked(!m_bDelete);
    m_bClear = CFG->get("DataSetting", "Clean Data", false).toBool();
    ui->checkBoxCleanAll->setChecked(m_bClear);
    ui->checkBoxCleanCurrent->setChecked(!m_bClear);
    check();
}

void SettingDatas::accept()
{
    int duplicate;

    if (ui->checkBoxRename->isChecked()) duplicate = 0;
    else if (ui->checkBoxAddSuffix->isChecked()) duplicate = 1;
    else duplicate = 2;

    CFG->set("DataSetting", "Duplicate Data", duplicate);
    CFG->set("DataSetting", "Add Data", ui->checkBoxAddAll->isChecked());
    CFG->set("DataSetting", "Delete Data", ui->checkBoxDeleteAll->isChecked());
    CFG->set("DataSetting", "Clean Data", ui->checkBoxCleanAll->isChecked());
}

void SettingDatas::check()
{
    int duplicate;

    if (ui->checkBoxRename->isChecked()) duplicate = 0;
    else if (ui->checkBoxAddSuffix->isChecked()) duplicate = 1;
    else duplicate = 2;

    do
    {
        if (duplicate != m_iDuplicate) break;
        if (m_bAdd != ui->checkBoxAddAll->isChecked()) break;
        if (m_bDelete != ui->checkBoxDeleteAll->isChecked()) break;
        if (m_bClear != ui->checkBoxCleanAll->isChecked()) break;
        change(false);
        return;
    } while (0);
    change(true);
}

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