#include "formbudget.h"
#include "ui_formbudget.h"
#include "qmtreeview.h"
#include "treemodel.h"
#include "treeitem.h"
#include "mapplication.h"

#include "../src/elementtype.h"

#include "widgetcomboboxpopuptable.h"

#include "itemnumberdelegate.h"
#include "itemtextdelegate.h"
#include "itemtextpopupdelegate.h"
#include "itemcomboboxdelegate.h"

#include <QStringList>
#include <QFile>
#include <QAction>
#include <QtSql>
#include "utils/dbutils.h"

formBudget::formBudget(QString aID, int amEditMode, QWidget *parent) :
    formBase(aID, amEditMode, parent),
    ui(new Ui::formBudget)
{
    ui->setupUi(this);
    QStringList headers;
    headers << tr("Índice") << tr("Código") << tr("Título") << tr("Descriction")
            << tr("Cantidad") << tr("Cantidad total") << tr("Unidad")
            << tr("N. Precio Unitario")<< tr("N. Precio Total") << tr("Ganacia")
            << tr("Descuento") << tr("Precio Venta") << tr("Margen") << tr("Tipo")
            << tr("Imprimir") << tr("");

    TreeModel *model = new TreeModel(headers, QByteArray());
    ui->treeView->setModel(model);

    //for (int column = 0; column < model->columnCount(); ++column)
    //    ui->treeView->resizeColumnToContents(column);
    ui->treeView->setColumnWidth( 0, 120);
    ui->treeView->setColumnWidth( 1, 120);
    ui->treeView->setColumnWidth( 2, 250);
    ui->treeView->setColumnWidth( 3, 250);
    ui->treeView->setColumnWidth( 4, 80);
    ui->treeView->setColumnWidth( 5, 80);
    ui->treeView->setColumnWidth( 6, 50);
    ui->treeView->setColumnWidth( 7, 80);
    ui->treeView->setColumnWidth( 8, 80);
    ui->treeView->setColumnWidth( 9, 80);
    ui->treeView->setColumnWidth(10, 80);
    ui->treeView->setColumnWidth(11, 80);
    ui->treeView->setColumnWidth(12, 80);
    ui->treeView->setColumnWidth(13, 40);

    ui->treeView->setColumnHidden( 3, true);
    //ui->treeView->setColumnHidden(13, true);

    // Texto con Popup
    ItemTextPopupDelegate *LineTextPopup = new ItemTextPopupDelegate(this);
    ui->treeView->setItemDelegateForColumn(1, LineTextPopup);

    // Texto:
    ItemTextDelegate *LineTextEditor = new ItemTextDelegate(this);
    ui->treeView->setItemDelegateForColumn(0, LineTextEditor);
    ui->treeView->setItemDelegateForColumn(2, LineTextEditor);

    // Números:
    ItemNumberDelegate *doubleNumberEditor = new ItemNumberDelegate(0.0, 999999.99, 0.05, 2, this);
    ui->treeView->setItemDelegateForColumn(4, doubleNumberEditor);
    ui->treeView->setItemDelegateForColumn(7, doubleNumberEditor);
    ui->treeView->setItemDelegateForColumn(9, doubleNumberEditor);
    ui->treeView->setItemDelegateForColumn(10, doubleNumberEditor);

    // Combobox:
    ItemComboboxDelegate *ComboboxEditor = new ItemComboboxDelegate(this);
    ui->treeView->setItemDelegateForColumn(6, ComboboxEditor);


    // Prueba:
    connect(dApp, &QApplication::focusChanged, this, [=](QWidget* old, QWidget* now)
    {
        //qDebug() << "old: " << old << "\n" << "new: " << now;
        if (!this->isAncestorOf(old))
            qDebug() << "Se deshabilita la toolbox";

        //Q_UNUSED(old);
        //if (now != this && !this->isAncestorOf(now))
        if (now == this && !this->isAncestorOf(now))
        {
            //hide();
            qDebug() << "Se habilita la toolbox";
        }
    });

    dApp->Enterprise().open();
    QSqlQuery qry = QSqlQuery(dApp->Enterprise());
    if (qry.exec(QString("SELECT ID FROM SALEPROPOSAL ORDER BY ID DESC LIMIT 1;")))
    {
        int val = 0;
        while(qry.next())
            val = qry.value(qry.record().indexOf("ID")).toInt();

        ui->editCode->setText(QString("(PRO%1)").arg(val + 1));
    }

    dApp->Enterprise().close();
}


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

void formBudget::setupTreeView()
{
    QStringList headers = {
        tr("Índice"), tr("Código"), tr("Título"), tr("Descripción"),
        tr("Cantidad"), tr("Cantidad total"), tr("Unidad"),
        tr("N. Precio Unitario"), tr("N. Precio Total"), tr("Ganacia"),
        tr("Descuento"), tr("Precio Venta"), tr("Margen"), tr("Tipo"),
        tr("Imprimir"), tr("")
    };

    m_treeModel = new TreeModel(headers, QByteArray(), this);
    ui->treeView->setModel(m_treeModel);

    // Configurar anchos de columna
    QVector<int> widths = {120, 120, 250, 250, 80, 80, 50, 80, 80, 80, 80, 80, 80, 40};
    for (int i = 0; i < widths.size(); ++i) {
        ui->treeView->setColumnWidth(i, widths[i]);
    }

    ui->treeView->setColumnHidden(3, true);

    // Configurar delegados
    setupDelegates();
}

void formBudget::setupDelegates()
{
    // Texto con Popup
    auto* lineTextPopup = new ItemTextPopupDelegate(this);
    ui->treeView->setItemDelegateForColumn(1, lineTextPopup);

    // Texto simple
    auto* lineTextEditor = new ItemTextDelegate(this);
    ui->treeView->setItemDelegateForColumn(0, lineTextEditor);
    ui->treeView->setItemDelegateForColumn(2, lineTextEditor);

    // Números
    auto* doubleNumberEditor = new ItemNumberDelegate(0.0, 999999.99, 0.05, 2, this);
    ui->treeView->setItemDelegateForColumn(4, doubleNumberEditor);
    ui->treeView->setItemDelegateForColumn(7, doubleNumberEditor);
    ui->treeView->setItemDelegateForColumn(9, doubleNumberEditor);
    ui->treeView->setItemDelegateForColumn(10, doubleNumberEditor);

    // Combobox
    auto* comboboxEditor = new ItemComboboxDelegate(this);
    ui->treeView->setItemDelegateForColumn(6, comboboxEditor);

    connect(m_treeModel, &TreeModel::dataChanged,
            this, &formBudget::onModelDataChanged);
}

void formBudget::setupConnections()
{
    connect(ui->buttonInsertRow, &QPushButton::released,
            this, &formBudget::on_buttonInsertRow_released);

    connect(ui->buttonInsertChild, &QPushButton::released,
            this, &formBudget::on_buttonInsertChild_released);

    connect(ui->buttonRemoveRow, &QPushButton::released,
            this, &formBudget::on_buttonRemoveRow_released);

    // ... otras conexiones ...
}

void formBudget::initializeDocument()
{
    if (m_dbUtils.isConnected()) {
        QString lastId = m_dbUtils.getLastDocumentId("SALEPROPOSAL");
        ui->editCode->setText(QString("(PRO%1)").arg(lastId.toInt() + 1));
    }
}

void formBudget::newDocument()
{

}

void formBudget::openDocument(QString id)
{
    formBase::openDocument(id);
}

void formBudget::save()
{
    QString budget;
    QString budgetdata;

    if(mEditMode == false)
    {
        budget = "INSERT INTO SALEPROPOSAL ("
                "ID, TYPE, CODE, TITLE, VERSION, "
                "CUSTOMER_CODE, CUSTOMER_NAME, PROJECT_CODE, PROJECT_TITLE, "
                "COST, PRICE, TAX, STATE, STATE_NUMBER, DESCRIPTION, "
                "CREATEDAT, VALIDUNTILL, DELIVERY_DATE, CREATEDBY"
                ") VALUES ("
                ":ID, :TYPE, :CODE, :TITLE, :VERSION, "
                ":CUSTOMER_CODE, :CUSTOMER_NAME, :PROJECT_CODE, :PROJECT_TITLE, "
                ":COST, :PRICE, :TAX, :STATE, :STATE_NUMBER, :DESCRIPTION, "
                ":CREATEDAT, :VALIDUNTILL, :DELIVERY_DATE, :CREATEDBY"
                ");";

        budgetdata = "INSERT INTO SALEPROPOSALDATA ("
                "ID, SALEDOCUMENT_CODE, VERSION, NODEINDEX, "
                "ELEMENT_CODE, ELEMENT_TYPE, ELEMENT_INDEX, ELEMENT_TITLE, ELEMENT_DESCRIPTION, "
                "ELEMENT_UNIT_AMOUNT, ELEMENT_TOTAL_AMOUNT, ELEMENT_UNIT, ELEMENT_PRICE, "
                "BENEFIT, DISCOUNT, TAX, PRINT, "
                "CREATEDAT, CREATEDBY"
                ") VALUES ("
                ":ID, :SALEDOCUMENT_CODE, :VERSION, :NODEINDEX, "
                ":ELEMENT_CODE, :ELEMENT_TYPE, :ELEMENT_INDEX, :ELEMENT_TITLE, :ELEMENT_DESCRIPTION, "
                ":ELEMENT_UNIT_AMOUNT, :ELEMENT_TOTAL_AMOUNT, :ELEMENT_UNIT, :ELEMENT_PRICE, "
                ":BENEFIT, :DISCOUNT,  :TAX, :PRINT, "
                ":CREATEDAT, :CREATEDBY"
                ");";
    }
    else
    {
        budget = "UPDATE SALEPROPOSAL SET "
                ":ID, :TYPE, :CODE, :TITLE, :VERSION, "
                ":CUSTOMER_CODE, :CUSTOMER_NAME, :PROJECT_CODE, :PROJECT_TITLE, "
                ":COST, :PRICE, :TAX, :STATE, :STATE_NUMBER, :DESCRIPTION, "
                ":CREATEDAT, :VALIDUNTILL, :DELIVERY_DATE, :CREATEDBY "
                "WHERE CODE = :CODE"
                ";";

        budgetdata = "UPDATE SALEPROPOSALDATA SET "
                ":ID, :SALEDOCUMENT_CODE, :VERSION, :NODEINDEX, "
                ":ELEMENT_CODE, :ELEMENT_TYPE, :ELEMENT_INDEX, :ELEMENT_TITLE, :ELEMENT_DESCRIPTION, "
                ":ELEMENT_UNIT_AMOUNT, :ELEMENT_TOTAL_AMOUNT, :ELEMENT_UNIT, :ELEMENT_PRICE, "
                ":BENEFIT, :DISCOUNT, :TAX, :PRINT, "
                ":CREATEDAT, :CREATEDBY "
                "WHERE CODE = :CODE"
                ";";
    }

    dApp->Enterprise().open();
    QSqlQuery qry = QSqlQuery(dApp->Enterprise());

    // 1. Guardar los datos:
    QAbstractItemModel *model = ui->treeView->model();

    // Borrar toda las filas al final que están vacias
    //  -- de momento se hace chequeando si la columna 13 está vacía
    /*
    int count = model->rowCount() - 1;
    for (int i = model->rowCount() - 1; i >= 0; i--)
    {
        count = i;

        QString dat = model->index(i, 13).data().toString();
        if(!dat.isEmpty())
            break;
    }

    for (int i = 0; i <= count; i++)
    {
        qry.prepare(budgetdata);
        qry.bindValue(":ID",                    i);
        qry.bindValue(":SALEDOCUMENT_CODE",     ui->editCode->text());
        qry.bindValue(":VERSION",               ui->editVersion->currentText());
        qry.bindValue(":NODEINDEX",             model->index(i, 0).data().toString());
        qry.bindValue(":ELEMENT_CODE",          model->index(i, 1).data().toString());
        qry.bindValue(":ELEMENT_TYPE",          model->index(i, 13).data().toString());
        qry.bindValue(":ELEMENT_TITLE",         model->index(i, 2).data().toString());
        qry.bindValue(":ELEMENT_DESCRIPTION",   model->index(i, 3).data().toString());
        qry.bindValue(":ELEMENT_UNIT_AMOUNT",   model->index(i, 4).data().toDouble());
        //qry.bindValue(":ELEMENT_TOTAL_AMOUNT",  model->index(i, 2).data().toDouble());
        qry.bindValue(":ELEMENT_UNIT",          model->index(i, 6).data().toString());
        qry.bindValue(":ELEMENT_PRICE",         model->index(i, 7).data().toString());
        qry.bindValue(":BENEFIT",               model->index(i, 9).data().toString());
        qry.bindValue(":DISCOUNT",              model->index(i, 10).data().toString());
        //qry.bindValue(":TAX",                   model->index(i, 10).data().toString());
        //qry.bindValue(":PRINT",                 model->index(i, 10).data().toBool());

        if(!qry.exec())
        {
            qDebug() << "" << qry.lastError().text();
            continue;
        }
    }
    */



    // 2. Si todo fue bien, guardar el documento:
    qry.prepare(budget);
    //qry.bindValue(":ID",);
    qry.bindValue(":TYPE", "");
    qry.bindValue(":CODE", ui->editCode->text());
    qry.bindValue(":TITLE", ui->editTitle->text());
    qry.bindValue(":VERSION", ui->editVersion->currentText());
    qry.bindValue(":CUSTOMER_CODE", ui->editClientCode->currentText());
    qry.bindValue(":CUSTOMER_NAME", ui->editClientName->text());
    qry.bindValue(":PROJECT_CODE", ui->editProjectCode->currentText());
    qry.bindValue(":PROJECT_TITLE", ui->editProjectName->text());
    qry.bindValue(":COST", ui->editCost->text());
    qry.bindValue(":PRICE", ui->editPrice->text());
    //qry.bindValue(":TAX", );
    qry.bindValue(":STATE", ui->editPrice->text());
    qry.bindValue(":STATE_NUMBER", ui->editPrice->text());
    //qry.bindValue(":DESCRIPTION", );
    qry.bindValue(":CREATEDAT", ui->editdateCreated->text());
    qry.bindValue(":VALIDUNTILL", ui->editdateValidUntill->text());
    //qry.bindValue(":DELIVERY_DATE", ui->editPrice->text());
    //qry.bindValue(":CREATEDBY", ui->editPrice->text());

    if(!qry.exec())
    {
        qDebug() << "" << qry.lastError().text();
    }
    else
      mEditMode = true;

    dApp->Enterprise().close();
}

/*
static void fill_model(QTreeWidget &tree){
    QTreeWidgetItem *foo_item = new QTreeWidgetItem({"foo"});
    QTreeWidgetItem *bar_item = new QTreeWidgetItem({"bar"});
    QTreeWidgetItem *bla_item = new QTreeWidgetItem({"bla"});
    QTreeWidgetItem *baz_item = new QTreeWidgetItem({"baz"});
    for(QTreeWidgetItem *item : {foo_item, bar_item, bla_item, baz_item})
        tree.addTopLevelItem(item);
    QTreeWidgetItem *beer_item = new QTreeWidgetItem({"beer"});
    QTreeWidgetItem *beer_child_item = new QTreeWidgetItem({"beer_child"});
    QTreeWidgetItem *ice_item = new QTreeWidgetItem({"ice"});
    for(QTreeWidgetItem *item : {beer_item, ice_item})
        bar_item->addChild(item);
    beer_item->addChild(beer_child_item);
    beer_child_item->addChild(new QTreeWidgetItem({"beer_child_child"}));
}
static void save_to_db(const QString & tablename, QTreeWidgetItem* parent, int parent_id=0){
    for(int i=0; i< parent->childCount(); ++i){
        QTreeWidgetItem *child_item = parent->child(i);
        QSqlQuery query(QString("INSERT INTO %1 (parentId, name) VALUES (?, ?)").arg(tablename));
        if(parent_id != 0)
            query.bindValue(0, parent_id);
        query.bindValue(1, child_item->text(0));
        if(!query.exec())
            qDebug()<< query.lastError().text();
        save_to_db(tablename, child_item, query.lastInsertId().toInt());
    }
}
*/

void formBudget::saveModel(QSqlQuery qry, QString aCommand, QAbstractItemModel *aModel, int aLevel)
{
    Q_UNUSED(aLevel);
    //QObjectList list = aModel->parent()->children();
    //for (auto aChil : list)

    for (int i = 0; i <= aModel->rowCount() - 1; i++)
    {
        qry.prepare(aCommand);
        qry.bindValue(":ID",                    i);
        qry.bindValue(":SALEDOCUMENT_CODE",     ui->editCode->text());
        qry.bindValue(":VERSION",               ui->editVersion->currentText());
        qry.bindValue(":NODEINDEX",             aModel->index(i, 0).data().toString());
        qry.bindValue(":ELEMENT_CODE",          aModel->index(i, 1).data().toString());
        qry.bindValue(":ELEMENT_TYPE",          aModel->index(i, 13).data().toString());
        qry.bindValue(":ELEMENT_TITLE",         aModel->index(i, 2).data().toString());
        qry.bindValue(":ELEMENT_DESCRIPTION",   aModel->index(i, 3).data().toString());
        qry.bindValue(":ELEMENT_UNIT_AMOUNT",   aModel->index(i, 4).data().toDouble());
        //qry.bindValue(":ELEMENT_TOTAL_AMOUNT",  aModel->index(i, 2).data().toDouble());
        qry.bindValue(":ELEMENT_UNIT",          aModel->index(i, 6).data().toString());
        qry.bindValue(":ELEMENT_PRICE",         aModel->index(i, 7).data().toString());
        qry.bindValue(":BENEFIT",               aModel->index(i, 9).data().toString());
        qry.bindValue(":DISCOUNT",              aModel->index(i, 10).data().toString());
        //qry.bindValue(":TAX",                   aModel->index(i, 10).data().toString());
        //qry.bindValue(":PRINT",                 aModel->index(i, 10).data().toBool());

        if(!qry.exec())
        {
            qDebug() << "" << qry.lastError().text();
            continue;
        }

        if(aModel->index(i, 13).data().toString() == "CO")
        {
            //QAbstractItemModel * childModel = aModel->index(i, 13).child(0, 0);
            //saveModel(qry, aCommand,  childModel, aLevel + 1);
        }
    }
}

bool formBudget::needsave()
{
    return formBase::needsave();
}

void formBudget::setEditMode(bool aMode)
{
    formBase::setEditMode(aMode);


}

void formBudget::closeDocument()
{
    if(mNeedSave)
    {

    }
    save();
}

void formBudget::on_editPrice_textChanged(const QString &arg1)
{

}

void formBudget::on_buttonInsertRow_released()
{
    ui->treeView->insertRow();
}

void formBudget::on_buttonInsertChild_released()
{
    QModelIndex selindex = ui->treeView->selectionModel()->currentIndex();
    QAbstractItemModel *treemodel = ui->treeView->model();

    ui->treeView->insertChild();

    QModelIndex child = treemodel->index(selindex.row(), 13, selindex.parent());
    treemodel->setData(child, QVariant("CO"), Qt::EditRole);
}

void formBudget::on_buttonRemoveRow_released()
{
    ui->treeView->removeRow();
}

void formBudget::on_buttonMaterials_released()
{
    ui->treeView->insertRow();
    setLineType("MT", ui->treeView->selectionModel()->currentIndex());
}

void formBudget::on_buttoMachinary_released()
{
    ui->treeView->insertRow();
    setLineType("MQ", ui->treeView->selectionModel()->currentIndex());
}

void formBudget::on_buttonManpower_released()
{
    ui->treeView->insertRow();
    setLineType("MO", ui->treeView->selectionModel()->currentIndex());
}

void formBudget::on_buttonPercent_released()
{
    ui->treeView->insertRow();
    setLineType("%", ui->treeView->selectionModel()->currentIndex());
}

void formBudget::setLineType(QString type, QModelIndex index)
{
    setCellText(type, index, 13);
}

void formBudget::setCellText(QString val, QModelIndex index, int col)
{
    QAbstractItemModel *treemodel = ui->treeView->model();

    QModelIndex child = treemodel->index(index.row(), col, index.parent().isValid() ? index.parent() : QModelIndex());
    treemodel->setData(child, QVariant(val), Qt::EditRole);
}

bool formBudget::InsertElement(QString ID, QModelIndex index)
{
    if (ID.isEmpty())
        return false;

    dApp->Enterprise().open();
    QSqlQuery qry = QSqlQuery(dApp->Enterprise());
    if (qry.exec(QString("SELECT * FROM ELEMENT WHERE CODE = '%1';").arg(ID)))
    {
        qry.first();
        setCellText(qry.value(qry.record().indexOf("TITLE")).toString(), index, 2);         // TITLE
        setCellText(qry.value(qry.record().indexOf("DESCRIPTION")).toString(), index, 3);   // DESCRIPTION
        setCellText(qry.value(qry.record().indexOf("UNIT_ID")).toString(), index, 6);       // UNIT

        if (qry.value(qry.record().indexOf("TYPE1")).toInt() == 0)
        {
            QSqlQuery comp = QSqlQuery(dApp->Enterprise());
            QAbstractItemModel *treemodel = ui->treeView->model();

            if (comp.exec(QString("SELECT * FROM ELEMENTCOMPOSITION WHERE CODE = '%1';").arg(ID)))
            {
                QList<QPair<QString, QString> > list;

                while(comp.next())
                {
                    list.append(qMakePair(comp.value(comp.record().indexOf("ELEMENT_CODE")).toString(),
                                          comp.value(comp.record().indexOf("ELEMENT_AMOUNT")).toString()));
                }

                int row = 0;
                while (row < list.size())
                {
                    if(!treemodel->insertRow(treemodel->rowCount(index), treemodel->index(index.row(), 0, index.parent())))
                        break;

                    qDebug() << treemodel->rowCount(index);

                    QModelIndex child = treemodel->index(row, 0, treemodel->index(index.row(), 0, index.parent()));
                    setCellText(list.at(row).first, child, 1);
                    setCellText(list.at(row).second, child, 4);   //AMOUNT*/
                    row++;

                }
            }
        }
        else
        {
            setCellText(qry.value(qry.record().indexOf("PURCHASE_PRICE")).toString(), index, 7);        //PRICE
        }

        // TODO: mirar de hacer esto con una enumeración o algo que automice el porceso:
        switch (static_cast<ElementType>(qry.value(qry.record().indexOf("TYPE1")).toInt()))
        {
            case ElementType::Composed:
                setLineType("CO", index);             // type
                break;
            case ElementType::Material:
                setLineType("MT", index);             // type
                break;
            case ElementType::ManPower:
                setLineType("MO", index);             // type
                break;
            case ElementType::Machinery:
                setLineType("MQ", index);             // type
                break;
            default:
                break;
        }
    }
    else
    {
        qDebug() << qry.lastError().text();
    }

    dApp->Enterprise().close();
    return false;
}

void formBudget::on_buttonValidate_released()
{
    // TODO: generar el código definitivo

    mDocumentID = ui->editCode->text();
    setWindowTitle(mDocumentID);

    QTabWidget * prt = static_cast<QTabWidget*>(parent()->parent());
    prt->setTabText(prt->currentIndex(), mDocumentID);
}

void formBudget::on_buttonSave_released()
{
    save();
}

void formBudget::onModelDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
{
    if(mEditMode)
        return;

    Q_UNUSED(bottomRight);

    if(topLeft.column() == 1)
    {
        qDebug() << topLeft.data().toString();
        InsertElement(topLeft.data().toString(), topLeft);
    }
}

void formBudget::focusInEvent(QFocusEvent *event)
{
    qDebug() << "focusInEvent";
}


void formBudget::focusOutEvent(QFocusEvent *event)
{
    qDebug() << "focusOutEvent";
}


void formBudget::on_tabWidget_currentChanged(int index)
{
    if (index == 1)
    {
        ui->ToolbarLines->setVisible(true);
    }
    else
    {
        ui->ToolbarLines->setVisible(false);
    }
}
