/***************************************************************************
 *   Copyright (C) 2009 by Docteur Pierre-Marie Desombre.   *
 *   pm.desombre@medsyn.fr  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; either version 3 of the    *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program 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 Library General Public     *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include <QtGui>
#include <QtCore>
#include <QtSql>

#include "textdialog.h"

extern QString g_pathabsolu;
extern int version;
textdialog::textdialog(QString &name,QString &surname,QString &user,QStringList &texte,QString &resutext,QString police, QString codec){
  QSqlDatabase db = QSqlDatabase::database();
  setupUi(this);
  pushButton_4 -> hide();
  setAttribute(Qt::WA_DeleteOnClose);
  //police, codec
  m_police = police;
  m_codec = codec;
  //liste lignes d'entêtes
  comboBox_3->setEditable(true);
  comboBox_3->setInsertPolicy(QComboBox::InsertAtTop);
  m_resources = qApp->applicationDirPath()+"/../resources";
  QFile titresini2(m_resources+"/titres.ini");
  if(!titresini2.open(QIODevice::ReadWrite|QIODevice::Text))
		{
      QMessageBox::warning(0,tr("erreur"),trUtf8("erreur ouverture fichier titres.ini !"),QMessageBox::Ok);
		}
  QStringList listetitres;
  QTextStream streamtitres2(&titresini2);
  while(!streamtitres2.atEnd()){
      QString lignetitre = streamtitres2.readLine();
      listetitres << ""+lignetitre+"";
				}
  m_titres2 = listetitres;
  comboBox_3->addItems(listetitres);
  //recupération des arguments du constructeur
  m_nompatient = name;
  m_prenompatient = surname;
  m_resutext = resutext;
  m_userpref = user;
  m_textelist = texte;
  m_texte = texte.join("\n");

  QSqlQuery query;
  query.exec("SELECT FchGnrl_IDDos FROM IndexNomPrenom WHERE FchGnrl_NomDos = '"+m_nompatient+"' AND FchGnrl_Prenom = '"+m_prenompatient+"'");
  QStringList listiddos;
  while(query.next())
	{
  QString iddos = query.value(0).toString();
  listiddos << ""+iddos+"";
	}
  int taillelist = listiddos.size();
  if(taillelist == 0){
      pushButton->setEnabled(false);
      pushButton->hide();
      }

  //construction tableWidget
  tableWidget->setRowCount(taillelist);
  tableWidget->setColumnCount(7);
  tableWidget->setHorizontalHeaderLabels(QStringList() << tr("Nom") << trUtf8("Prénom") << trUtf8("né(e) le:") << trUtf8("lieu") << tr("CP") << trUtf8("Ville") << tr("guid"));
  for(int i=0;i<taillelist;i++)
	{
    QString naissance;
    QString adresse;
    QString cp;
    QString ville;
    QSqlQuery query1;
    query1.exec("SELECT FchPat_Nee,FchPat_Adresse,FchPat_CP,FchPat_Ville FROM fchpat WHERE FchPat_GUID_Doss = '"+listiddos[i]+"'");
    while(query1.next())	{
        naissance = query1.value(0).toString();
        adresse = query1.value(1).toString();
        cp = query1.value(2).toString();
        ville = query1.value(3).toString();
        }
  QTableWidgetItem *itemnom = new QTableWidgetItem(""+m_nompatient+"");
  tableWidget->setItem(i,0,itemnom);
  QTableWidgetItem *itemprenom = new QTableWidgetItem(""+m_prenompatient+"");
  tableWidget->setItem(i,1,itemprenom);
  QTableWidgetItem *itemnaissance = new QTableWidgetItem(""+naissance+"");
  tableWidget->setItem(i,2,itemnaissance);
  QTableWidgetItem *itemadresse = new QTableWidgetItem(""+adresse+"");
  tableWidget->setItem(i,3,itemadresse);
  QTableWidgetItem *itemcp = new QTableWidgetItem(""+cp+"");
  tableWidget->setItem(i,4,itemcp);
  QTableWidgetItem *itemville = new QTableWidgetItem(""+ville+"");
  tableWidget->setItem(i,5,itemville);
  QTableWidgetItem *itemguid = new QTableWidgetItem(""+listiddos[i]+"");
  tableWidget->setItem(i,6,itemguid);
  tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
  tableWidget->show();
	}

  m_document = new QTextDocument;
  m_document->setPlainText(m_texte);
  textEdit->setDocument(m_document);
  textEdit->show();
  pushButton_2  -> setShortcut(QKeySequence::InsertParagraphSeparator);
  connect(pushButton,SIGNAL(pressed()),this,SLOT(enregistretexte()));
  connect(pushButton_2,SIGNAL(pressed()),this,SLOT(close()));
  connect(pushButton_3,SIGNAL(pressed()),this,SLOT(ouvrirmanager()));
  connect(pushButton_5,SIGNAL(pressed()),this,SLOT(effacerFichier()));
  connect(textEdit,SIGNAL(textChanged()),this,SLOT(modifiertexte()));
}//constructeur

textdialog::~textdialog(){
     m_nompatient   = "";
     m_prenompatient= "";
     m_resutext     = "";
     m_userpref     = "";
     m_texte        = "";
     m_textelist.clear();
     m_titres2  .clear();
     delete m_document;
    }


void textdialog::enregistretexte(){
  QString avanttexte;
  avanttexte = m_texte;
  avanttexte.replace("\n","<br/>");
  avanttexte.replace("\r","<br/>");
  avanttexte.replace("\t"," ");
  avanttexte.replace("\'","\\'");

  QString texte;
  switch(version){
      case 12 :
           qWarning() << __FILE__ << QString::number(__LINE__) << "version 12" ;
          texte = "<?xml version=\"1.0\" encoding=\""+m_codec+"\" standalone=\"yes\" ?><MedinTux_Multimedia_data><HTML_Data><html><head><meta name=\"qrichtext\" content=\"1\" /></head><body style=\"font-size:10pt;font-family:"+m_police+"\"><p>"+avanttexte+"</p></body></html></HTML_Data><ImageListe></ImageListe></MedinTux_Multimedia_data>";
          break;
      case 14 :
          qWarning() << __FILE__ << QString::number(__LINE__) << "version 14" ;
          texte = QString("<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body style=\"font-size:10pt;font-family:%2\"><p>%1</p></body></html>")
                        .arg(avanttexte,m_police);
          break;
      default :
          break;    
      }
  int row = tableWidget->currentRow();
  if(row == -1){QMessageBox::warning(0,tr("erreur"),trUtf8("vous n'avez pas sélectionné de patient !"),QMessageBox::Reset);}
  QTableWidgetItem *itemguid = tableWidget->item(row,6);
  QString guid = itemguid->text();
  QSqlQuery query;
  if (!query.exec("INSERT INTO RubriquesBlobs (RbDate_DataRub,RbDate_IDDos) VALUES ('"+texte+"','"+guid+"')"))
  {
        qWarning() << __FILE__ << QString::number(__LINE__) << query.lastError().text() ;
      }
  
  //intégration données dans rubriqueheads
  int id_head = query.lastInsertId().toInt();
  QString titre_head = fonctiontitre(m_textelist);
  if(titre_head.isEmpty()){
	titre_head = "docs_externes";
		}
  QDateTime datetime = QDateTime::currentDateTime();
  QString dateheure = datetime.toString("yyyy-MM-dd hh:mm:ss");
  QSqlQuery query2;
  if (!  query2.exec("INSERT INTO RubriquesHead"
                     "(RbDate_PrimKey,RbDate_IDDos,RbDate_TypeRub,RbDate_NomDate,RbDate_Date,RbDate_CreateUser,RbDate_CreateSignUser,RbDate_RefBlobs_Primkey)"
                     " VALUES (NULL,'"+guid+"',20080000,'"+titre_head+"','"+dateheure+"','"+m_userpref+"','"+m_userpref+"','"+QString::number(id_head)+"')"))
  {
        qWarning() << __FILE__ << QString::number(__LINE__) << query2.lastError().text() ;
      }
  m_textelist.clear();
  if((m_nompatient.isEmpty()==false) && (m_prenompatient.isEmpty()==false))
  {
     QFile file(m_resutext);
     file.remove();}
     emit close();
    }

  QString textdialog::fonctiontitre(QStringList &texte){
  QString titrechoisi;

  QStringList textelist = texte;


	for(int i=0;i<m_titres2.size();i++)
			{
	QStringList titresplit = m_titres2[i].split("=");
	int ligne = titresplit[0].toInt();
	QString terme = titresplit[1];
	QString titre = titresplit[2];
	if(textelist[ligne].contains(""+terme+""))
		{
	titrechoisi += titre;
		}
	else {titrechoisi += "";}
			}//for
    return titrechoisi;
}

void textdialog::ouvrirmanager(){
    QString managerDir = g_pathabsolu+"/../../Manager";
    QDir dirManager;
    if (!dirManager.exists(managerDir))
    {
          QMessageBox::warning(0,trUtf8("Erreur"),trUtf8("Vous devez placer le répertoire apitux \ndans Medintux/programmes à côté de Manager."),QMessageBox::Ok);
        }
    
    QString manager = g_pathabsolu+"/../../Manager/bin/Manager";
    #ifdef Q_OS_WIN
    manager = ""+g_pathabsolu+"/../../Manager/bin/Manager.exe";
    #endif
    QProcess *process = new QProcess;
    process->start(manager);
}

void textdialog::modifiertexte(){
  QString texte;
  texte = textEdit->toPlainText();
  QFile file(m_resutext);
  if(!file.open(QIODevice::ReadWrite|QIODevice::Text)){
	QMessageBox::warning(0,tr("erreur"),trUtf8("resutext ne peut pas être ouvert !"),QMessageBox::Ok);
	}
  QTextStream tamise(&file);
  tamise.setCodec("ISO-8859-1");
  tamise << texte;
  file.close();
}

void textdialog::effacerFichier(){
  QFile file(m_resutext);
  QString warningText = "Voulez vous vraiment effacer le fichier : "+m_resutext+" ?";
  QMessageBox mess;
  mess.setWindowTitle("Attention !");
  mess.setIcon(QMessageBox::Warning);
  mess.setText(warningText);
  mess.setStandardButtons(QMessageBox::Ok|QMessageBox::Abort);
  mess.setDefaultButton(QMessageBox::Abort);
  if(mess.exec() == QMessageBox::Ok){
	file.remove();
	emit close();
	}
}

