#include "settingsdialog.h"
#include "settings/settings.h"
#include "ui_settingsdialog.h"
#include "utils/utils.h"

SettingsDialog::SettingsDialog(QWidget *parent)
    : QDialog(parent), ui(new Ui::settingsdialog),
    settings(Settings(get_path_relative_to_home(
          ".local/share/checks_parser/settings.json"))) {

    ui->setupUi(this);

    ui->OFDModulesDirEdit->setText(QString::fromStdString(settings.get_all_settings()["ofds_modules_dir"]));
    ui->OFDModulesURLEdit->setText(QString::fromStdString(settings.get_all_settings()["ofds_modules_url"]));
    ui->storesModulesDirEdit->setText(QString::fromStdString(settings.get_all_settings()["stores_modules_dir"]));
    ui->storesModulesURLEdit->setText(QString::fromStdString(settings.get_all_settings()["stores_modules_url"]));

    ui->goodsNamePositionSpin->setValue(this->settings.get_all_settings()["output_order"]["goods_name"]["position"]);
    ui->goodsNameAliasEdit->setText(QString::fromStdString(this->settings.get_all_settings()["output_order"]["goods_name"]["name"]));

    ui->goodsPricePerUnitPositionSpin->setValue(this->settings.get_all_settings()["output_order"]["goods_price_per_unit"]["position"]);
    ui->goodsPricePerUnitAliasEdit->setText(QString::fromStdString(this->settings.get_all_settings()["output_order"]["goods_price_per_unit"]["name"]));

    ui->goodsQuantityPositionSpin->setValue(this->settings.get_all_settings()["output_order"]["goods_quantity"]["position"]);
    ui->goodsQuantityAliasEdit->setText(QString::fromStdString(this->settings.get_all_settings()["output_order"]["goods_quantity"]["name"]));

    ui->goodsNetWeightPositionSpin->setValue(this->settings.get_all_settings()["output_order"]["goods_net_weight"]["position"]);
    ui->goodsNetWeightAliasEdit->setText(QString::fromStdString(this->settings.get_all_settings()["output_order"]["goods_net_weight"]["name"]));

    ui->goodsTotalPositionSpin->setValue(this->settings.get_all_settings()["output_order"]["goods_total"]["position"]);
    ui->goodsTotalAliasEdit->setText(QString::fromStdString(this->settings.get_all_settings()["output_order"]["goods_total"]["name"]));

    ui->printHeaderCheckBox->setChecked(this->settings.get_all_settings()["print_header"]);
    ui->printTotalCheckBox->setChecked(this->settings.get_all_settings()["print_total"]);
}

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

void SettingsDialog::on_OFDModulesDirEdit_editingFinished() {
    this->settings.alter_setting("ofds_modules_dir",
                                 ui->OFDModulesDirEdit->text().toStdString());
}

void SettingsDialog::on_storesModulesDirEdit_editingFinished() {
    this->settings.alter_setting("stores_modules_dir",
                                 ui->storesModulesDirEdit->text().toStdString());
}

void SettingsDialog::on_OFDModulesURLEdit_editingFinished() {
    this->settings.alter_setting("ofds_modules_url",
                                 ui->OFDModulesURLEdit->text().toStdString());
}

void SettingsDialog::on_storesModulesURLEdit_editingFinished() {
    this->settings.alter_setting("stores_modules_url",
                                 ui->storesModulesURLEdit->text().toStdString());
}

void SettingsDialog::on_goodsNamePositionSpin_valueChanged(int value) {
    this->settings.get_all_settings()["output_order"]["goods_name"]["position"] =
        value;
}
void SettingsDialog::on_goodsNameAliasEdit_editingFinished() {
    this->settings.get_all_settings()["output_order"]["goods_name"]["name"] =
        ui->goodsNameAliasEdit->text().toStdString();
}

void SettingsDialog::on_goodsPricePerUnitPositionSpin_valueChanged(int value) {
    this->settings
        .get_all_settings()["output_order"]["goods_price_per_unit"]["position"] =
        value;
}

void SettingsDialog::on_goodsPricePerUnitAliasEdit_editingFinished() {
    this->settings.get_all_settings()["output_order"]["goods_price_per_unit"]["name"] =
        ui->goodsPricePerUnitAliasEdit->text().toStdString();
}

void SettingsDialog::on_goodsQuantityPositionSpin_valueChanged(int value) {
    this->settings
        .get_all_settings()["output_order"]["goods_quantity"]["position"] = value;
}

void SettingsDialog::on_goodsQuantityAliasEdit_editingFinished() {
    this->settings.get_all_settings()["output_order"]["goods_quantity"]["name"] =
        ui->goodsQuantityAliasEdit->text().toStdString();
}

void SettingsDialog::on_goodsNetWeightPositionSpin_valueChanged(int value) {
    this->settings
        .get_all_settings()["output_order"]["goods_net_weight"]["position"] =
        value;
}

void SettingsDialog::on_goodsNetWeightAliasEdit_editingFinished() {
    this->settings.get_all_settings()["output_order"]["goods_net_weight"]["name"] =
        ui->goodsNetWeightAliasEdit->text().toStdString();
}


void SettingsDialog::on_goodsTotalPositionSpin_valueChanged(int value) {
    this->settings
        .get_all_settings()["output_order"]["goods_total"]["position"] =
        value;
}

void SettingsDialog::on_goodsTotalAliasEdit_editingFinished() {
    this->settings.get_all_settings()["output_order"]["goods_total"]["name"] =
        ui->goodsTotalAliasEdit->text().toStdString();
}

void SettingsDialog::on_printHeaderCheckBox_stateChanged(int value) {
    this->settings.get_all_settings()["print_header"] = (value? true : false);
}

void SettingsDialog::on_printTotalCheckBox_stateChanged(int value) {
    this->settings.get_all_settings()["print_total"] = (value? true : false);
}

void SettingsDialog::on_buttonBox_accepted() { this->settings.flush(); }

void SettingsDialog::on_buttonBox_rejected() { this->close(); }


