#include "shopsettingsform.h"
#include "ui_shopsettingsform.h"

#include <settingsservice.h>
#include <combodata.h>
#include <QFileDialog>
#include "shopservice.h"

ShopSettingsForm::ShopSettingsForm(QWidget *parent) :
    FormBinder<ShopSettings>(parent),
    ui(new Ui::ShopSettingsForm)
{
    ui->setupUi(this);

    registerBinding(ui->output);
    registerBinding(ui->lettersPerLine);
    registerBinding(ui->byMessage);

    registerBinding(ui->eetActive);
    registerBinding(ui->eetShopId);
    registerBinding(ui->eetRegisterId);
    QList<ComboData> listModes;
    listModes << ComboData(0, tr("Simplifyed")) << ComboData(1, tr("Standard"));
    registerBinding(ui->eetMode, listModes);
    registerBinding(ui->eetCertificate);
    registerBinding(ui->eetKeyPassword);
    registerBinding(ui->eetTest);
    registerBinding(ui->eetPlayground);

    m_itemModel = new AutoTableModel<ShopItem>();
}

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

void ShopSettingsForm::loadEntity()
{
    SettingsService srv("SHOP");
    ShopSettingsPtr settings = srv.loadSettings<ShopSettings>();
    setEntity(settings);

    ShopService srvShop;
    m_itemModel->setData(srvShop.allSellableItems());
    ui->tableItems->setModel(m_itemModel);
}

bool ShopSettingsForm::saveRecord()
{
    bindToData();
    SettingsService srv("SHOP");
    srv.saveSettings(entity());

    return true;
}

void ShopSettingsForm::on_btnCertBrowse_clicked()
{
    QString certFile = QFileDialog::getOpenFileName(this, "Certificate file", "", "P12 Files (*.p12)");

    if (!certFile.isEmpty())
    {
        ui->eetCertificate->setText(certFile);
    }
}
