#include "copyIO.h"
#include "connexion.h"

#include <QDebug>

static const QString BASE = QString("myChoices");

CopyIO::CopyIO(QObject * parent){
    m_db = QSqlDatabase::database("myChoices");
    m_object = parent;
}

CopyIO::~CopyIO(){}

QStandardItemModel *CopyIO::modelAccordingToText(QString text){
    QSqlDatabase db = QSqlDatabase::database(BASE);
    qDebug() << __FILE__ << QString::number(__LINE__) << " a "  ;
    QStandardItemModel * model = new QStandardItemModel;
    model->insertColumns(0,1,QModelIndex());
    qDebug() << __FILE__ << QString::number(__LINE__) << " b "   ;
    QString filterText ;
    QString filter; 
    const QString baseName = BASE;
    qDebug() << __FILE__ << QString::number(__LINE__) << " baseName =" << baseName ;
    const QString item = "ITEM";
    filterText = ""+text+"%";
    filter = QString("WHERE %1 LIKE '%2'").arg("ITEM",filterText); 
    QString req = QString("SELECT %1 FROM %3 ").arg(item,baseName )+filter;
    int row = 0;
    QStringList listOfItems;
    QSqlQuery q(db);
    qDebug() << __FILE__ << QString::number(__LINE__) << filterText  ;
    if (!q.exec(req))
    {
    	 qWarning() << __FILE__ << QString::number(__LINE__) 
    	                        << "Error __FILE__"+QString::number(__LINE__)+q.lastError().text() ; 
        }
    while (q.next())
    {
      	QString n = q.value(0).toString();
    	listOfItems << n;

        }
    listOfItems.sort();
    foreach(QString item,listOfItems){
    	if (!model->insertRows(row,1,QModelIndex()))
    	{
    		  qWarning() << __FILE__ << QString::number(__LINE__) << "unable to insert row" ;
    	    }
    	if (!model->setData(model->index(row,0),item,Qt::EditRole))
    	{
    		  qWarning() << __FILE__ << QString::number(__LINE__) << "unable to set data "<< item ;
    	    }
        model->submit();
        ++row;        
        }
        
    qDebug() << __FILE__ << QString::number(__LINE__) << " B "   ;
    return model;
}

bool CopyIO::addToDatabase(const QString & text){
    QSqlDatabase db = QSqlDatabase::database(BASE);
    QSqlTableModel model(this,db);
    model.setTable(BASE);
    model.select();
    int row = model.rowCount();
    //get the lastIndex.
    int idData = 0 ;
    QSqlQuery q(db);
    QString req = QString("SELECT %1 FROM %2").arg("ID_Primkey",BASE);
    if (!q.exec(req))
    {
    	  qWarning() << __FILE__ << QString::number(__LINE__) << "unable to get last id" ;
        }
    while (q.next())
    {
    	idData = q.value(0).toInt();
        }
    qDebug() << __FILE__ << QString::number(__LINE__) << " idData =" << QString::number(idData) ;
    int newIdData = idData + 1;
    qDebug() << __FILE__ << QString::number(__LINE__) << " row =" << QString::number(row) ;
    if (!model.insertRows(row,1,QModelIndex()))
    {
    	  qWarning() << __FILE__ << QString::number(__LINE__) << "unable to insert row" ;
        }
    qDebug() << __FILE__ << QString::number(__LINE__) << " rowCount =" << QString::number(model.rowCount()) ;
    if (!model.setData(model.index(row,0),newIdData,Qt::EditRole))
    {
    	  qWarning() << __FILE__ << QString::number(__LINE__) << "unable to add id row" ;
        }
    if (!model.setData(model.index(row,1),text,Qt::EditRole))
    {
    	  qWarning() << __FILE__ << QString::number(__LINE__) << "unable to add "+text ;
        }
    return model.submit();
}

bool CopyIO::deleteFromDatabase(const QString & text)
{
    bool success = true;
    QSqlDatabase db = QSqlDatabase::database(BASE);
    QSqlQuery q(db);
    QString req = QString("DELETE FROM %1 WHERE %2 LIKE '%3'").arg(BASE,"ITEM",text);
    if (!q.exec(req))
    {
    	  qWarning() << __FILE__ << QString::number(__LINE__) << "unable to delete "+text+" from database" ;
    	  success = false;
        }
    return success;
}

