#include "receiptsaveform.h"
#include "ui_receiptsaveform.h"

#include <QList>
#include <QCompleter>
#include <QSortFilterProxyModel>
#include <QPushButton>

#include <service.h>
#include <iplugin.h>
#include <addressbookservice.h>
#include <addressbookdata.h>

#include "data/voucher.h"
#include "shopservice.h"
#include "shop-odb.hxx"

ReceiptSaveForm::ReceiptSaveForm(QSharedPointer<Voucher> voucher, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ReceiptSaveForm)
{
    ui->setupUi(this);

    m_voucherModel = new AutoTableModel<Voucher>(this);
    Service<Voucher> srv;
    m_voucherModel->setData(srv.all(QString("status = %1").arg(QString::number(Voucher::NOT_PAID))));
    m_voucherModel->setTranslations(Context::instance().plugin("SHOP")->translations());
    ui->tabVouchers->setModel(m_voucherModel);
    ui->tabVouchers->setColumnHidden(0, true);
    ui->tabVouchers->setColumnHidden(1, true);
    ui->tabVouchers->setColumnHidden(6, true);
    ui->tabVouchers->setColumnHidden(7, true);
    ui->tabVouchers->setColumnHidden(8, true);
    ui->tabVouchers->setColumnHidden(9, true);
    ui->tabVouchers->setColumnHidden(10, true);
    ui->tabVouchers->setColumnHidden(11, true);
    ui->tabVouchers->setColumnHidden(13, true);
    ui->tabVouchers->setColumnHidden(14, true);
    ui->tabVouchers->setColumnHidden(15, true);
    ui->tabVouchers->setColumnHidden(16, true);
    ui->tabVouchers->setColumnHidden(17, true);
    ui->tabVouchers->setColumnHidden(18, true);
    ui->tabVouchers->horizontalHeader()->setSectionResizeMode(3, QHeaderView::Stretch);
    ui->tabVouchers->horizontalHeader()->setSectionResizeMode(4, QHeaderView::Stretch);
    ui->tabVouchers->horizontalHeader()->setSectionResizeMode(5, QHeaderView::Stretch);

    connect(ui->tabVouchers->selectionModel(), &QItemSelectionModel::currentRowChanged, [this](QModelIndex, QModelIndex){
        ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
    });

    connect(ui->name, &QLineEdit::textChanged, [this](QString text){
        ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!text.isEmpty());
    });

    m_binder.setData(voucher.data());
    AddressBookService srvAdb;
    m_binder.registerBinding(ui->contact, ComboData::createComboData(srvAdb.all()));
    m_binder.registerBinding(ui->name);
    m_binder.registerBinding(ui->description);
    m_binder.bindToUi();

    ui->contact->completer()->setCompletionMode(QCompleter::PopupCompletion);
    ui->contact->setCurrentIndex(-1);

    m_saveAsNew = false;
    ui->groupBox_2->setEnabled(false);

    ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
}

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

VoucherPtr ReceiptSaveForm::selectedVoucher()
{
    if (ui->tabVouchers->currentIndex().isValid())
    {
        return m_voucherModel->itemFromIndex(ui->tabVouchers->currentIndex());
    }

    return VoucherPtr();
}

bool ReceiptSaveForm::saveAsNew()
{
    return m_saveAsNew;
}

void ReceiptSaveForm::on_lineEdit_textChanged(const QString &text)
{
    QSortFilterProxyModel proxy;
    proxy.setSourceModel(m_voucherModel);
    proxy.setFilterKeyColumn(3);
    proxy.setFilterCaseSensitivity(Qt::CaseInsensitive);
    proxy.setFilterFixedString(text);

    QModelIndex matchingIndex = proxy.mapToSource(proxy.index(0,0));
    if(matchingIndex.isValid()){
        ui->tabVouchers->scrollTo(matchingIndex, QAbstractItemView::EnsureVisible);
        ui->tabVouchers->setCurrentIndex(matchingIndex);
        ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
    }
}

void ReceiptSaveForm::on_radioAdd_toggled(bool checked)
{
    if (checked)
    {
        m_saveAsNew = false;
        ui->groupBox_2->setEnabled(false);
        ui->groupBox->setEnabled(true);

        ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ui->tabVouchers->currentIndex().isValid());
    }
    else
    {
        m_saveAsNew = true;
        ui->groupBox_2->setEnabled(true);
        ui->groupBox->setEnabled(false);

        ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!ui->name->text().isEmpty());
    }
}

void ReceiptSaveForm::accept()
{
    m_binder.bindToData();
    QDialog::accept();
}
