/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            dbwidget.cc
 *
 *  Mon Sep  1 16:23:54 CEST 2008
 *  Copyright 2008 Bent Bisballe Nyeng
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *  This file is part of Pracro.
 *
 *  Pracro is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  Pracro is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Pracro; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
#include "dbwidget.h"

#include <QDomNodeList>
#include <QCompleter>
#include <QRegExpValidator>
#include <QRegExp>
#include <QStringListModel>
#include <QSqlQuery>
#include <QSqlError>
#include <QLineEdit>
#include "formatparser.h"
#include "common.h"

DBWidget::DBWidget(QDomNode &node, MacroWindow *macrowindow)
  : QComboBox(), Widget(node, macrowindow)
{
  QDomElement elem = node.toElement();

  if(!elem.hasAttribute("driver") ||
     !elem.hasAttribute("server") ||
     !elem.hasAttribute("user") ||
     !elem.hasAttribute("database") ||
     !elem.hasAttribute("select") ||
     !elem.hasAttribute("from") ||
     !elem.hasAttribute("where") ||
     !elem.hasAttribute("format")) {
    printf("Error: missing tag!\n");
  }

  select = elem.attribute("select");
  from = elem.attribute("from");
  where = elem.attribute("where");
  format = elem.attribute("format");

  db = QSqlDatabase::addDatabase(elem.attribute("driver"));
  db.setHostName(elem.attribute("server"));
  if(elem.hasAttribute("port")) db.setPort(elem.attribute("port").toInt());
  db.setDatabaseName(elem.attribute("database"));
  db.setUserName(elem.attribute("user"));
  if(elem.hasAttribute("password")) db.setPassword(elem.attribute("password"));
  db.setConnectOptions("connect_timeout=2000");
  bool ok = db.open();
  if(!ok) {
    printf("DB connect failed!\n");
  }

  setCommonAttributes(this, node);

  setInsertPolicy(QComboBox::InsertAlphabetically);
  setEditable(true);

  // Make empty default selection.
  addItem("Skriv noget i s�gefeltet.");
  setCurrentIndex(-1);

  QStringListModel *strlst = new QStringListModel();
  QCompleter *completer = new QCompleter(this);
  completer->setCaseSensitivity(Qt::CaseInsensitive);
  completer->setCompletionMode(QCompleter::PopupCompletion);
  completer->setModel(strlst);
  setCompleter(completer);

  connect(this, SIGNAL(editTextChanged(QString)), this, SLOT(changed()));
  connect((QWidget*)lineEdit(), SIGNAL(textEdited(QString)), this, SLOT(update_list(QString)));

  changed();
}

DBWidget::~DBWidget()
{
  db.close();
  db = QSqlDatabase();
}


QString DBWidget::getValue()
{
  QString value;

  value = currentText();

  return value;
}

void DBWidget::setValue(QString value)
{
  setEditText(value);
}

bool DBWidget::isValid()
{
  QSqlQuery query = db.exec("SELECT " + select + " FROM " + from + " WHERE LOWER(" + where + ") = '" + currentText().toLower() + "';");

  return query.size() != 0;
}

void DBWidget::changed()
{
  QPalette palette;

  if(isValid() && luaValidator()) {
    // valid string
    palette.setBrush(QPalette::Base, QBrush(QColor(255, 255, 255)));
  } else {
    // invalid string
    palette.setBrush(QPalette::Base, QBrush(QColor(230, 200, 200)));
  }

  lineEdit()->setPalette(palette);
}

void DBWidget::update_list(QString prefix)
{
  /*
  if(prefix == "") {
    clear();
    addItem("Skriv noget i s�gefeltet.");
    setCurrentIndex(-1);
    return;
  }
  */
  QSqlQuery query = db.exec("SELECT " + select + " FROM " + from + " WHERE LOWER(" + where + ") LIKE '" + prefix.toLower() + "%';");

  QStringList lst;
  while(query.next()) {
    lst << format_parser(format, query);
  }

  lst.sort();

  QStringListModel *mdl = (QStringListModel *)completer()->model();
  if(mdl->stringList() != lst) {
    QString val = currentText();
    clear();
    if(lst.size() == 0) lst << "S�gningen passede ikke p� noget.";
    addItems(lst);
    setEditText(val);
    mdl->setStringList(lst);
    //showPopup();
  }
}

void DBWidget::enable()
{
  setEnabled(true);
}

void DBWidget::disable()
{
  setEnabled(false);
}
/*
$ psql -h sensei -d lms -U postgres
===================================================================
         List of relations
 Schema | Name  | Type  |  Owner   
--------+-------+-------+----------
 public | lms01 | table | postgres
(1 row)

lms=# \d lms01
                       Table "public.lms01"
            Column             |         Type          | Modifiers 
-------------------------------+-----------------------+-----------
 drugid                        | character varying(32) | 
 producttype                   | character varying(14) | 
 productsubtype                | character varying(14) | 
 sequencetext                  | character varying(28) | 
 specialitynumber              | character varying(20) | 
 drugname                      | character varying(70) | 
 dosageform_text               | character varying(50) | 
 dosageform_code               | character varying(24) | 
 further_dos_info              | character varying(24) | 
 strength_text                 | character varying(50) | 
 strength_numeric              | character varying(30) | 
 strength_unit                 | character varying(16) | 
 marketing_authorisation_owner | character varying(22) | 
 importer                      | character varying(22) | 
 atc                           | character varying(26) | 
 route_of_administration       | character varying(26) | 
 traffic_warning               | character varying(12) | 
 substitution                  | character varying(12) | 
 substitution_group            | character varying(18) | 
 dose_dispensing               | character varying(12) | 
 deregistration_date           | character varying(26) | 
 quarantine_date               | character varying(26) | 
*/