#include "campgrid.h"
#include "campwizard.h"
#include "campservice.h"
#include "campshopitem.h"

#include <data/shop-data.h>
#include <shopservice.h>
#include <paydialog.h>

CampGrid::CampGrid(QWidget *parent) : GridForm<CampData>(parent)
{
    setTableModel(new AutoTableModel<CampData>);

    QHBoxLayout *tbLayout = qobject_cast<QHBoxLayout*>(this->toolbar()->layout());

    if (tbLayout != NULL)
    {
        QToolButton *btnImport = new QToolButton(this->toolbar());
        btnImport->setIcon(QIcon(":/icons/pay.svg"));
        btnImport->setAutoRaise(true);
        btnImport->setIconSize(QSize(24, 24));
        btnImport->setToolTip(tr("Pay"));
        tbLayout->insertWidget(tbLayout->count() - 1, btnImport);

        connect(btnImport, &QToolButton::clicked, [this](){
            CampDataPtr data = currentEntity();

            if (!data.isNull())
            {
                addToVoucher(data);
            }
        });
    }

    m_detail = new DetailWidget(this);
    mainLayout()->addWidget(m_detail);
}

void CampGrid::handleNewRecord()
{
    if (!checkPermAdd())
    {
        return;
    }

    CampService srv;
    CampDataPtr data = srv.create();
    CampWizard *wizard = new CampWizard();
    wizard->setAttribute(Qt::WA_DeleteOnClose);
    wizard->setData(data);
    wizard->setNewRecord(true);

    connect(wizard, &QDialog::accepted, [this, data](){
        addRow(data);
    });

    wizard->show();
}

void CampGrid::handleEditRecord()
{
    if (!checkPermEdit())
    {
        return;
    }

    CampDataPtr data = currentEntity();
    if (data.isNull())
    {
        return;
    }

    if (data->onVoucher())
    {
        QMessageBox::critical(this, tr("Can not edit"), tr("This record is asociated with voucher. Can not edit paid items"));
        return;
    }

    CampService srv;
    srv.loadItems(data);

    CampWizard *wizard = new CampWizard();
    wizard->setAttribute(Qt::WA_DeleteOnClose);
    wizard->setData(data);
    wizard->show();
}

void CampGrid::doDelete(CampDataPtr entity)
{
    if (!checkPermDelete())
    {
        return;
    }

    if (entity->onVoucher())
    {
        QMessageBox::critical(this, tr("Can not delete"), tr("This record is asociated with voucher. Can not delete paid items"));
        return;
    }

    CampService srv;
    srv.eraseCamp(entity);
}

void CampGrid::addToVoucher(CampDataPtr data)
{
    if (data->onVoucher())
    {
        QMessageBox::information(this, tr("Can not pay"), tr("This record is already paid"));
        return;
    }

    SettingsService settSrv("CAMP");
    CampSettingsPtr settings = settSrv.loadSettings<CampSettings>();
    CampShopItemPtr campItem(new CampShopItem);

    campItem->setId(data->id());
    campItem->setUnitPrice(data->totalPrice());
    campItem->setVatType(settings->vatType());

    ShopService shopSrv;
    VoucherPtr voucher = shopSrv.createVoucher();

    shopSrv.addShopItem(voucher, campItem, 1);
    shopSrv.calculate(voucher);
    shopSrv.saveVoucher(voucher);

    data->setOnVoucher(true);

    CampService srvCamp;
    srvCamp.update(data);

    PayDialog *payDlg = new PayDialog(voucher->totalPrice(), this);
    payDlg->setAttribute(Qt::WA_DeleteOnClose);
    payDlg->show();

    connect(payDlg, &QDialog::accepted, [payDlg, voucher](){
        payVoucherFromUI(voucher, payDlg);
    });

    connect(payDlg, &QDialog::rejected, [voucher, &shopSrv, data](){
        CampService srvCamp;
        data->setOnVoucher(false);
        srvCamp.update(data);
        voucher->clearItems();
        shopSrv.eraseVoucher(voucher);
    });
}

void CampGrid::currentIndexChanged(const QModelIndex &current)
{
    if (current.isValid())
    {
        m_detail->setData(currentEntity());
    }
}

QList<CampDataPtr> CampGrid::listForGrid()
{
    CampService srv;
    return srv.allForSeason();
}
