#include "parametrages.h"

#include "../../common/configure/sitesdialog.h"
#include "../../common/configure/parametredialog.h"
#include "../../common/configure/payeursdialog.h"
#include "../../common/configure/pourcentages.h"
#include "../../common/configure/actesdispo.h"
#include "../../common/configure/automatisme.h"
#include "../../common/configure/rapport.h"
#include "../../common/configure/modifutil.h"
#include "../../common/configure/utilisateur.h"
#include "../../common/configure/thesaurus.h"
#include "../../common/completeBase.h"
#include "../../common/print/headingUserParam/headingParam.h"
#include <common/icore.h>
#include <common/settings.h>
#include <common/position.h>
#include <QtGui>
#include <QtCore>
#include <QtSql>

using namespace Common;
static inline Common::Settings *settings(){return Common::ICore::instance()->settings();}
parametrages::parametrages(QWidget *parent)
{
    setupUi(this);
    Position *position = new Position;
    position->centralPosition(this,width(),height());
    //setWindowFlags(Qt::WindowStaysOnTopHint);
    pushButton_10   -> setShortcut(QKeySequence("Ctrl+Q"));
    //-----le bouton compléter la base est-il accessible ou non ? -----
    alerteBoutonCompleteBase(pushButton_8);
    //----------------------------------------------------------------
    connect(pushButton,    SIGNAL(pressed()), this , SLOT(utilisateurPreferentiel()));
    connect(pushButton_2,  SIGNAL(pressed()), this , SLOT(utilisateursComptabilite()));
    connect(pushButton_3,  SIGNAL(pressed()), this , SLOT(parametreSite()));
    connect(pushButton_4,  SIGNAL(pressed()), this , SLOT(entrePayeur()));
    connect(pushButton_5,  SIGNAL(pressed()), this , SLOT(entrePourcentage()));
    connect(pushButton_6,  SIGNAL(pressed()), this , SLOT(entreActe()));
    connect(pushButton_7,  SIGNAL(pressed()), this , SLOT(autoFermeture()));
    connect(pushButton_8,  SIGNAL(pressed()), this , SLOT(completeBase()));
    connect(pushButton_9,  SIGNAL(pressed()), this , SLOT(rapportBug()));
    connect(pushButton_10, SIGNAL(pressed()), this , SLOT(close()));
    connect(pushButton_11, SIGNAL(pressed()), this , SLOT(headingsParams()));
    connect(thesaurusButton,SIGNAL(pressed()), this , SLOT(thesaurusModif()));
}

parametrages::~parametrages()
{}

void parametrages::utilisateurPreferentiel()
{
    parametredialog *m =  new parametredialog ;
    m -> show();
}
void parametrages::utilisateursComptabilite()
{
    QString str = "";
    utilisateur *u =  new utilisateur(str);
    u -> show();
}
void parametrages::parametreSite(){
    sites       *s =  new sites;
    s -> show();
}
void parametrages::entrePayeur(){
    payeurs     *p =  new payeurs;
    p -> show();
}
void parametrages::entrePourcentage(){
    pourcentage *pc =  new pourcentage;
    pc -> show();
}
void parametrages::entreActe(){
    actesdispo  *act =  new actesdispo(this);
    act -> show();
}
void parametrages::autoFermeture(){
    automatismeDialog *au =  new automatismeDialog;
    au -> show();
}
void parametrages::completeBase(){
    completeBaseCompta(pushButton_8);
}
void parametrages::rapportBug(){
    QString errorAllString;
    QFile f(settings()->applicationBundlePath()+"/errorLog.txt");
    qDebug() << __FILE__ << QString::number(__LINE__) << " " << settings()->applicationBundlePath()+"/errorLog";
    if(!f.open(QIODevice::ReadOnly)){
        QMessageBox::warning(0,trUtf8("Error"),trUtf8("Error log file cannot be opened."),QMessageBox::Ok);
        }
    QTextStream s(&f);
    s.setCodec("UTF8");
    while(!s.atEnd()){
        errorAllString += s.readLine()+"\n";
        }
  QString rapport = "rapport de bug";
  mail   *retour  =  new mail;
  if(retour       -> exec() == QDialog::Accepted){
      rapport     = retour->mail::textEdit->toPlainText();

  QString mailing = "mailto:pm.desombre@medsyn.fr?subject=Bug&body="+rapport+"\n"+errorAllString;
  QDesktopServices::openUrl(QUrl(mailing));
  }
  else{
    return;
  }
}

void parametrages::headingsParams(){
   headingParam *h = new headingParam;
   h->show();
}

void parametrages::thesaurusModif(){
   thesaurusModify * th = new thesaurusModify;
   th->show();
}
