#ifndef CONFIGDIALOG_H
#define CONFIGDIALOG_H

#include <QtGui>
#include "ui_configdialog.h"
#include "consoleoutmodel.h"

//NB Not Currently Connected!
#define _CONSOLE_ERROR( x ) emit sendConsoleOut(QString() + x , ConsoleOut::Error);
#define _CONSOLE_INFO( x ) emit sendConsoleOut(QString() + x , ConsoleOut::Information);
#define _CONSOLE_VERBOSE( x ) emit sendConsoleOut(QString() + x , ConsoleOut::Verbose);
#define _CONSOLE_FATAL( x ) emit sendConsoleOut(QString() + x , ConsoleOut::Fatal);

namespace Ui{
    class ConfigDialog;
}

class ConfigDialog : public QDialog{
    Q_OBJECT
public:
    ConfigDialog(QString *scriptDir, QString *scriptEdit, QString *midiPlayer,
                 QWidget *parent=0 ):QDialog(parent), ui(new Ui::ConfigDialog),
                scriptDir(scriptDir), scriptEdit(scriptEdit),
                midiPlayer(midiPlayer){
        ui->setupUi(this);
        ui->scriptsDirE->setText(*scriptDir);
        ui->scriptsEditE->setText(*scriptEdit);
        ui->extMidiE->setText(*midiPlayer);
    }

    //Theres no error checking in this (!)
    bool readConfig(){
        QFile f(".config");
        if(!f.exists()){
            return false;
        }
        if (!f.open(QIODevice::ReadOnly)){
            return false;
        }
        QDataStream in(&f);
        in.setVersion(QDataStream::Qt_4_6);
        in >> *this->scriptDir;
        in >> *this->scriptEdit;
        in >> *this->midiPlayer;
        ui->scriptsDirE->setText(*scriptDir);
        ui->scriptsEditE->setText(*scriptEdit);
        ui->extMidiE->setText(*midiPlayer);
        f.close();
        return true;
    }

    //No Error Checking!
    void writeConfig(){
        QFile f(".config");
        if(!f.open(QIODevice::WriteOnly | QIODevice::Truncate)){
            QMessageBox::information(this, "Oh Noes", "Oh Noes");
        }

        QDataStream out(&f);
        out.setVersion(QDataStream::Qt_4_6);
        *scriptDir = ui->scriptsDirE->text();
        out <<  *scriptDir;
        *scriptEdit = ui->scriptsEditE->text();
        out << *scriptEdit;
        *midiPlayer = ui->extMidiE->text();
        out << *midiPlayer;
    }

private:
    Ui::ConfigDialog *ui;
    QString *scriptDir, *scriptEdit, *midiPlayer;

signals:
    void sendConsoleOut(QString text, ConsoleOut::OutputType);


private slots:
    void on_buttonBox_accepted(){
        writeConfig();
    }

private slots:
    void on_extMidiBrowse_clicked(){
        QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
                      QDesktopServices::storageLocation(QDesktopServices::ApplicationsLocation),
                      tr("(*.*)"));
        if(!fileName.isEmpty()){
            ui->extMidiE->setText(fileName);
        }
    }
    void on_scriptsEditBrowse_clicked(){
        QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
                      QDesktopServices::storageLocation(QDesktopServices::ApplicationsLocation),
                      tr("(*.*)"));
        if(!fileName.isEmpty()){
            ui->scriptsEditE->setText(fileName);
            }
    }
    void on_scriptsDirBrowse_clicked(){
        QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
                      QDesktopServices::storageLocation(QDesktopServices::HomeLocation),
                      QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
        if(!dir.isEmpty()){
            ui->scriptsDirE->setText(dir);
            }
    }
};

#endif // CONFIGDIALOG_H
