#include "copyMyChoices.h"

#include <QSqlTableModel>
#include <QMessageBox>
#include <QClipboard>
#include <QDebug>

CopyMyChoices::CopyMyChoices(QWidget * parent){
    setupUi(this);
    qDebug() << __FILE__ << QString::number(__LINE__) << " 1 "  ;
    on_lineEdit_textChanged("a");
    qDebug() << __FILE__ << QString::number(__LINE__) << " 2 "  ;
    m_io = new CopyIO();
    okButton->setShortcut(QKeySequence("CTRL+v"));
    connect(tableView,SIGNAL(pressed(const QModelIndex&)),this,SLOT(chooseValue(const QModelIndex&)));
    connect(listChoosenWidget,SIGNAL(itemClicked(QListWidgetItem *)),this,SLOT(supprItemChoosen(QListWidgetItem *)));
    connect(plusButton,SIGNAL(pressed()),this,SLOT(addToDatabase()));
    connect(quitButton,SIGNAL(pressed()),this,SLOT(quitApplication()));
    connect(okButton,SIGNAL(pressed()),this,SLOT(apply()));
    connect(deleteButton,SIGNAL(pressed()),this,SLOT(deleteFromDatabase()));
    
}

CopyMyChoices::~CopyMyChoices(){}

void CopyMyChoices::run(QString &arg){
    arg = m_arg;
    show(); 
}

void CopyMyChoices::quitApplication(){
    close();
}

void CopyMyChoices::apply(){
    QString result ;
    foreach(QString strItem,m_ValuesChoosen){
        result += strItem+" ";
        }
    qDebug() << __FILE__ << QString::number(__LINE__) << " result =" << result ;
    QClipboard *clipboard = QApplication::clipboard();
    clipboard->setText(result);
    m_arg = result;
    qApp->closeAllWindows();
}

QString CopyMyChoices::returnString()
{
    QString htmlArg = QString("<html><body><font size=\"16\">%1</font></body></html>").arg(m_arg);
    return htmlArg;
}

void CopyMyChoices::clear(){
    listChoosenWidget->clear();
    m_ValuesChoosen.clear();
}

void CopyMyChoices::on_lineEdit_textChanged(const QString & text){
    qDebug() << __FILE__ << QString::number(__LINE__) << " C "  ;
    QStandardItemModel * model = new QStandardItemModel(0,1);
    model = m_io->modelAccordingToText(text);
    qDebug() << __FILE__ << QString::number(__LINE__) << "model rowCount " << QString::number(model->rowCount())   ;
    qDebug() << __FILE__ << QString::number(__LINE__) << " model columnCount =" << QString::number(model->columnCount()) ;    
    tableView->setModel(model);
    tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    tableView->setSelectionMode(QAbstractItemView::SingleSelection);
    tableView->horizontalHeader()->setStretchLastSection ( true );
    tableView->setGridStyle(Qt::NoPen);
}

void CopyMyChoices::chooseValue(const QModelIndex& index){
    QModelIndex inIndex(index);
    //get datas
    int row = inIndex.row();
    QAbstractItemModel * model = tableView->model();
    QModelIndex indexData = model->index(row,0,QModelIndex());
    QString data = model->data(indexData,Qt::DisplayRole).toString();//ITEM
    listChoosenWidget->addItem(data);
    m_ValuesChoosen << data;
    lineEdit->setFocus();
    lineEdit->selectAll();
}

void CopyMyChoices::supprItemChoosen(QListWidgetItem * item){
    QString dataToRemove = item->data(Qt::DisplayRole).toString();
    m_ValuesChoosen.removeAll(dataToRemove);
    delete item;
    lineEdit->setFocus();
    lineEdit->selectAll();
}

QStringList CopyMyChoices::getChoosenValues(){
    return m_ValuesChoosen;
}

void CopyMyChoices::addToDatabase(){
   QString text = lineEdit->text();
   if (!m_io->addToDatabase(text))
   {
   	  QMessageBox::warning(0,trUtf8("Warning"),trUtf8("Unable to insert in database"),QMessageBox::Ok);
       }
   else
   {
   	addToTableView(text);
       }
}

void CopyMyChoices::deleteFromDatabase(){
    QModelIndex index = tableView->currentIndex();
    QAbstractItemModel * model = tableView->model();
    QString text = model->data(model->index(index.row(),index.column()),Qt::DisplayRole).toString();
    qDebug() << __FILE__ << QString::number(__LINE__) << " text to delete =" << text ;
    if (!m_io->deleteFromDatabase(text))
    {
    	  QMessageBox::warning(0,trUtf8("Warning"),trUtf8("Unable to delete from database"),QMessageBox::Ok);
        }
    else
    {
    	on_lineEdit_textChanged("a");
    	clear();
        }
}

void CopyMyChoices::addToTableView(const QString & text){
    QStandardItemModel * model = new QStandardItemModel(1,1);
    model->setData(model->index(0,0),text,Qt::EditRole);
    tableView->setModel(model);
    tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    tableView->setSelectionMode(QAbstractItemView::SingleSelection);
    tableView->horizontalHeader()->setStretchLastSection ( true );
    tableView->setGridStyle(Qt::NoPen);
}

void CopyMyChoices::keyReleaseEvent(QKeyEvent *e)
{
    if (e->key()== Qt::Key_Return)
    {
        QMessageBox::information(0,"info","info",QMessageBox::Ok);
    	apply();
        }

     QWidget::keyPressEvent(e);
}



