#include "sfilterdlg.h"
#include "mainwindow.h"

#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <QSqlQuery>

#include "./../common.h"

SFilterDlg::SFilterDlg()
{
    setWindowTitle("Filter");
    setWidgets();
}

SFilterDlg::~SFilterDlg()
{

}

void SFilterDlg::setWidgets()
{
    QHBoxLayout *hMainlayout = new QHBoxLayout();
    {
        QVBoxLayout *vlayout = new QVBoxLayout();
        m_plwFilterGroup = new QListWidget;
        m_pleFilterGroup = new QLineEdit;
        vlayout->addWidget(m_plwFilterGroup);
        vlayout->addWidget(m_pleFilterGroup);
        connect(m_plwFilterGroup,SIGNAL(currentItemChanged(QListWidgetItem *,QListWidgetItem *)),this,SLOT(GroupItemChanged(QListWidgetItem *, QListWidgetItem *)));
        {
            QHBoxLayout *hlayout = new QHBoxLayout();
            QPushButton *pbInsert = new QPushButton("Insert");
            connect(pbInsert, SIGNAL(released()),this, SLOT(Group_Insert()));
            QPushButton *pbDelete = new QPushButton("Delete");
            connect(pbDelete, SIGNAL(released()),this, SLOT(Group_Delete()));
            QPushButton *pbModify = new QPushButton("Modfiy");
            connect(pbModify, SIGNAL(released()),this, SLOT(Group_Modify()));
            QPushButton *pbCopy_Paste = new QPushButton("Copy&Paste");
            connect(pbCopy_Paste, SIGNAL(released()),this, SLOT(Group_CopyPaste()));
            QPushButton *pbRefresh = new QPushButton("Refresh");
            connect(pbRefresh, SIGNAL(released()),this, SLOT(Group_Refresh()));
            QPushButton *pbRun = new QPushButton("Run");
            connect(pbRun, SIGNAL(released()), this, SLOT(Group_Run()));

            hlayout->addWidget(pbInsert);
            hlayout->addWidget(pbDelete);
            hlayout->addWidget(pbModify);
            hlayout->addWidget(pbCopy_Paste);
            hlayout->addWidget(pbRefresh);
            hlayout->addWidget(pbRun);
            vlayout->addLayout(hlayout);
        }
        hMainlayout->addLayout(vlayout,1);
    }
    {
        QVBoxLayout *vlayout = new QVBoxLayout();
        m_plwFilter = new QListWidget;
        vlayout->addWidget(m_plwFilter);
        connect(m_plwFilter,SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),this,SLOT(ItemChanged(QListWidgetItem*,QListWidgetItem*)));
        QHBoxLayout *hlayout = new QHBoxLayout();
        QPushButton *pbUp = new QPushButton("Up");
        QPushButton *pbDown = new QPushButton("Down");
        QPushButton *pbDelete = new QPushButton("Delete");
        hlayout->addWidget(pbUp);
        hlayout->addWidget(pbDown);
        hlayout->addWidget(pbDelete);
        vlayout->addLayout(hlayout);
        connect(pbUp, SIGNAL(released()),this, SLOT(Up()));
        connect(pbDown, SIGNAL(released()),this, SLOT(Down()));
        connect(pbDelete, SIGNAL(released()),this, SLOT(Delete()));
        hMainlayout->addLayout(vlayout,3);
    }
    setLayout(hMainlayout);
}

void SFilterDlg::Group_Insert()
{
    QSqlQuery query;
    QString strQuery = QString("insert into filtergroup set "
            "name = '" + m_pleFilterGroup->text() + "'");
    query.exec(strQuery.toUtf8());
    Group_Refresh();
}

void SFilterDlg::Group_Delete()
{
    QSqlQuery query;
    foreach (QListWidgetItem *item,m_plwFilterGroup->selectedItems())
    {
        QString strQuery = QString("delete from filtergroup where id = " + item->data(Qt::UserRole).toString());
        query.exec(strQuery.toUtf8());
        strQuery = QString("delete from filter where filtergroup_id = " + item->data(Qt::UserRole).toString());
        query.exec(strQuery.toUtf8());
        RefreshFilter(item->data(Qt::UserRole).toInt());
    }
    Group_Refresh();
}

void SFilterDlg::Group_Modify()
{
    QSqlQuery query;
    foreach (QListWidgetItem *item,m_plwFilterGroup->selectedItems())
    {
        QString strQuery = QString("update filtergroup set name = '" + m_pleFilterGroup->text() +  "' where id = " + item->data(Qt::UserRole).toString());
        query.exec(strQuery.toUtf8());
    }
    Group_Refresh();
}

void SFilterDlg::Group_CopyPaste()
{
    foreach (QListWidgetItem *item,m_plwFilterGroup->selectedItems())
    {
        QSqlQuery query;
        QString strQuery = QString("insert into filtergroup set "
                "name = '" + item->text() + "-'");
        query.exec(strQuery.toUtf8());
        query.exec("select max(id) from filtergroup");
        if (query.next())
        {
            strQuery = QString("insert into filter (type,data,filtergroup_id) select type,data,");
            strQuery += query.value(0).toString();
            strQuery += " from filter where filtergroup_id = " + item->data(Qt::UserRole).toString();
            query.exec(strQuery.toUtf8());
        }
    }
    Group_Refresh();
}

void SFilterDlg::Group_Refresh()
{
    m_plwFilterGroup->clear();
    QSqlQuery query("select id,name from filtergroup");
    while (query.next())
    {
        QListWidgetItem *pItem = new QListWidgetItem(query.value(1).toString(),m_plwFilterGroup);
        pItem->setData(Qt::UserRole, QVariant(query.value(0)));
    }
}

void SFilterDlg::RefreshFilter(int _nGroup)
{
    if (_nGroup == D_NOT_SELECT) return;
    QSqlQuery query("select id,type,data from filter where filtergroup_id = " + QString::number(_nGroup));
    m_plwFilter->clear();
    while (query.next())
    {
        QString str = GetMainWindow()->m_pFilterObjectDlg->JsonToString(query.value(1).toInt(),query.value(2).toString());
        QListWidgetItem *pItem = new QListWidgetItem(str, m_plwFilter);
        pItem->setData(Qt::UserRole, QVariant(query.value(0)));
    }
}


void SFilterDlg::GroupItemChanged(QListWidgetItem *_pCurrent, QListWidgetItem *_pPrev)
{
    Q_UNUSED(_pPrev);
    if (_pCurrent == 0) return;
    RefreshFilter(_pCurrent->data(Qt::UserRole).toInt());
    m_pleFilterGroup->setText(_pCurrent->text());
    m_pleFilterGroup->repaint();
}

void SFilterDlg::ItemChanged(QListWidgetItem*_pCurrent, QListWidgetItem *_pPrev)
{
    Q_UNUSED(_pPrev);
    if (_pCurrent == 0) return;
    QSqlQuery query("select id,type,data from filter where id = " + _pCurrent->data(Qt::UserRole).toString());
    if (!query.next()) return;
    GetMainWindow()->m_pFilterObjectDlg->JsonToWidget(query.value(1).toInt(),query.value(2).toString());
}


void SFilterDlg::Up()
{
    int nSelect = m_plwFilter->currentRow();
    if (nSelect <= 0) return;
    QString strID1 = m_plwFilter->item(nSelect)->data(Qt::UserRole).toString();
    QString strID2 = m_plwFilter->item(nSelect-1)->data(Qt::UserRole).toString();
    QString strQuery = "UPDATE filter t1 INNER JOIN filter t2 ON (t1.id, t2.id) IN ((" + strID1 + ","+ strID2 +"),("+ strID2 + "," + strID1 + ")) SET t1.type = t2.type,t1.filtergroup_id = t2.filtergroup_id,t1.data = t2.data";
    QSqlQuery query;
    query.exec(strQuery.toUtf8());
    foreach (QListWidgetItem *item,m_plwFilterGroup->selectedItems())
        RefreshFilter(item->data(Qt::UserRole).toInt());
    m_plwFilter->setCurrentRow(nSelect-1);
}

void SFilterDlg::Down()
{
    int nSelect = m_plwFilter->currentRow();
    if (nSelect < 0) return;
    if (nSelect >= (m_plwFilter->count() - 1)) return;
    QString strID1 = m_plwFilter->item(nSelect)->data(Qt::UserRole).toString();
    QString strID2 = m_plwFilter->item(nSelect+1)->data(Qt::UserRole).toString();
    QString strQuery = "UPDATE filter t1 INNER JOIN filter t2 ON (t1.id, t2.id) IN ((" + strID1 + ","+ strID2 +"),("+ strID2 + "," + strID1 + ")) SET t1.type = t2.type,t1.filtergroup_id = t2.filtergroup_id,t1.data = t2.data";
    QSqlQuery query;
    query.exec(strQuery.toUtf8());
    foreach (QListWidgetItem *item,m_plwFilterGroup->selectedItems())
        RefreshFilter(item->data(Qt::UserRole).toInt());
    m_plwFilter->setCurrentRow(nSelect+1);
}

void SFilterDlg::Delete()
{
    QSqlQuery query;
    foreach (QListWidgetItem *item,m_plwFilter->selectedItems())
    {
        QString strQuery = QString("delete from filter where id = '" + item->data(Qt::UserRole).toString() + "'");
        query.exec(strQuery.toUtf8());
    }
    foreach (QListWidgetItem *item,m_plwFilterGroup->selectedItems())
        RefreshFilter(item->data(Qt::UserRole).toInt());
}

int SFilterDlg::GetSelectGroupID()
{
    int nCurrentFilterGroupID = D_NOT_SELECT;
    foreach (QListWidgetItem *item,m_plwFilterGroup->selectedItems())
        nCurrentFilterGroupID = item->data(Qt::UserRole).toInt();
    return nCurrentFilterGroupID;
}

int SFilterDlg::GetSelectFilterID()
{
    int nCurrentFilterID = D_NOT_SELECT;
    foreach (QListWidgetItem *item,m_plwFilter->selectedItems())
        nCurrentFilterID = item->data(Qt::UserRole).toInt();
    return nCurrentFilterID;
}

void SFilterDlg::Group_Run()
{
    GetMainWindow()->m_pBatchRunDlg->showBatchRun(GetSelectGroupID());
}
