#include "paydvouchersdialog.h"
#include "ui_paydvouchersdialog.h"

#include <QFileDialog>
#include <QScroller>

#include "receiptgenerator.h"
#include "shopservice.h"
#include "eetbatchdialog.h"

#include "shop-odb.hxx"

PaydVouchersDialog::PaydVouchersDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::PaydVouchersDialog)
{
    ui->setupUi(this);

    m_voucherModel = new AutoTableModel<Voucher>(this);
    m_itemModel = new AutoTableModel<VoucherItem>(this);
    m_voucherModel->setTranslations(Context::instance().plugin("SHOP")->translations());

    m_itemModel->setTranslations(Context::instance().plugin("SHOP")->translations());

    ui->tableVouchers->setModel(m_voucherModel);
    ui->tableItems->setModel(m_itemModel);
    ui->tableVouchers->setSortingEnabled(true);
    ui->tableVouchers->setColumnHidden(2, true);
    ui->tableVouchers->setColumnHidden(6, true);
    ui->tableVouchers->setColumnHidden(7, true);
    ui->tableVouchers->setColumnHidden(8, true);
    ui->tableVouchers->setColumnHidden(9, true);
    ui->tableVouchers->setColumnHidden(10, true);
    ui->tableVouchers->setColumnHidden(11, true);
    ui->tableVouchers->setColumnHidden(13, true);
    ui->tableVouchers->setColumnHidden(14, true);
    ui->tableVouchers->setColumnHidden(15, true);
    ui->tableVouchers->setColumnHidden(16, true);
    ui->tableVouchers->setColumnHidden(17, true);
    ui->tableVouchers->setColumnHidden(18, true);
    ui->tableVouchers->horizontalHeader()->setSectionResizeMode(3, QHeaderView::Stretch);
    ui->tableVouchers->horizontalHeader()->setSectionResizeMode(4, QHeaderView::Stretch);
    ui->tableVouchers->horizontalHeader()->setSectionResizeMode(5, QHeaderView::Stretch);

    ui->tableItems->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch);
    ui->tableItems->setColumnHidden(0, true);

    ShopService srv;
    m_voucherModel->setData(srv.paiedVouchers());
    ui->tableVouchers->sortByColumn(0,Qt::AscendingOrder);

    QScroller::grabGesture(ui->tableVouchers, QScroller::LeftMouseButtonGesture);
    QScroller::grabGesture(ui->tableItems, QScroller::LeftMouseButtonGesture);

    connect(ui->tableVouchers->selectionModel(), &QItemSelectionModel::currentRowChanged, [this, &srv](const QModelIndex &current, const QModelIndex &) {
        QSharedPointer<Voucher> voucher = m_voucherModel->itemFromIndex(current);
        srv.loadItems(voucher);
        m_itemModel->setData(voucher->items());
        ui->total->setText(QString::number(voucher->totalPrice().toDouble(), 'f', 2));

        switch (voucher->eetStatus()) {
        case Voucher::EET_NOT_ENTERING:
            ui->lblEetStatus->setText(tr("not entering"));
            break;
        case Voucher::EET_FOR_SEND:
            ui->lblEetStatus->setText(tr("for send"));
            break;
        case Voucher::EET_ERROR:
            ui->lblEetStatus->setText(tr("error"));
            break;
        case Voucher::EET_SENT:
            ui->lblEetStatus->setText(tr("sent"));
            break;
        default:
            break;
        }

        ui->btnPrint->setEnabled(true);
        ui->btnSave->setEnabled(true);
    });
}

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

void PaydVouchersDialog::on_btnPrint_clicked()
{
    QSharedPointer<Voucher> voucher = m_voucherModel->itemFromIndex(ui->tableVouchers->currentIndex());

    ReceiptGenerator generator;
    generator.setVoucher(voucher);
    generator.print();
}

void PaydVouchersDialog::on_btnSave_clicked()
{
    QString fileToSave = QFileDialog::getSaveFileName(this, tr("Save receipt"), "", tr("Text files (*.txt)"));
    QSharedPointer<Voucher> voucher = m_voucherModel->itemFromIndex(ui->tableVouchers->currentIndex());

    if (!fileToSave.isEmpty())
    {
        ReceiptGenerator generator;
        generator.setVoucher(voucher);
        generator.setOutputFile(fileToSave);
        generator.save();
    }
}

void PaydVouchersDialog::on_btnEetNotSen_clicked(bool checked)
{
    ShopService srv;

    if (checked)
    {
        m_voucherModel->setData(srv.vouchersForEet());
    }
    else
    {
        m_voucherModel->setData(srv.paiedVouchers());
    }

    m_itemModel->setData(QList<VoucherItemPtr>());
}

void PaydVouchersDialog::on_btnSendEet_clicked()
{
    ShopService srv;
    QList<VoucherPtr> vouchers = srv.vouchersForEet();
    EetBatchDialog *dialog = new EetBatchDialog(this);
    dialog->setAttribute(Qt::WA_DeleteOnClose);
    dialog->show();

    foreach (VoucherPtr vch, vouchers) {
        QString msg;
        bool sent = srv.processEet(vch, msg);
        dialog->addLog(vch->numSer() + ": ");
        dialog->addLog(sent ? "OK\n" : tr("Error"));
        dialog->addLog((msg.isEmpty() && !sent) ? "\n" : ": " + msg);
    }
}
