#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //Parse file.
    QFile myTextFile("/usr/share/X11/xorg.conf.d/50-touchpad-gui.conf");
    QString mline;
    QStringList mlist;
    int pos;
    QRegExp rx("\"([A-Za-z0-9_\\./\\-\\s]*)\"");
    if (!myTextFile.open(QIODevice::ReadOnly))
    {
        qDebug() << "Error opening file";
    }
    else
    {
        while(!myTextFile.atEnd())
        {
            mline = myTextFile.readLine();
                if ((mline.contains("Option")) && !(mline.contains("#"))) {
                    pos = 0;
                    while ((pos = rx.indexIn(mline, pos)) != -1) {
                        mlist << rx.cap(1);
                        pos += rx.matchedLength();
                    }
                }
        }
        myTextFile.close();
    }

    //set UI values
    ui->accelSpeed->setValue(mlist.takeAt(1).toDouble());
    if (!mlist.takeAt(2).compare("false"))
        ui->disableWhileTyping->setChecked(false);
    else
        ui->disableWhileTyping->setChecked(true);

    if (!mlist.takeAt(3).compare("false"))
        ui->enableHorzScroll->setChecked(false);
    else
        ui->enableHorzScroll->setChecked(true);

    if (!mlist.takeAt(4).compare("false"))
        ui->enableLeftHanded->setChecked(false);
    else
        ui->enableLeftHanded->setChecked(true);

    if (!mlist.takeAt(5).compare("false"))
        ui->enableMiddleButton->setChecked(false);
    else
        ui->enableMiddleButton->setChecked(true);

    if (!mlist.takeAt(6).compare("false"))
        ui->enableNaturalScrolling->setChecked(false);
    else
        ui->enableNaturalScrolling->setChecked(true);

    ui->scrollMethod->setCurrentText(mlist.takeAt(7));

    if (!mlist.takeAt(8).compare("false")) {
        ui->enableTapClick->setChecked(false);
        ui->frame_11->setEnabled(false); // DragLock frame
    }
    else
        ui->enableTapClick->setChecked(true);

    if (!mlist.takeAt(9).compare("false"))
        ui->enableDragLock->setChecked(false);
    else
        ui->enableDragLock->setChecked(true);

    ui->clickPad->setCurrentText(mlist.takeAt(10));

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_saveSettings_clicked()
{
    QString commandLinePre = "/usr/bin/xinput set-prop " + QString::number(getDeviceID()) + " ";
    QString commandLine = commandLinePre + "\"libinput Accel Speed\" " + ui->accelSpeed->text();
    system(commandLine.toStdString().c_str());
    QString outString = "#DO NOT EDIT! Used by touchpad settings app. \n Section \"InputClass\" \n Identifier \"libinput\" \n Driver \"libinput\" \n MatchDevicePath \"/dev/input/event*\" \n     Option \"AccelSpeed\" \"";
    outString += ui->accelSpeed->text() += "\" \n     Option \"DisableWhileTyping\" \"";
    if (ui->disableWhileTyping->isChecked()) {
            outString += "true\"\n";
            commandLine = commandLinePre + "\"libinput Disable While Typing Enabled\" 1";
            system(commandLine.toStdString().c_str());
       } else {
            outString += "false\"\n";
            commandLine = commandLinePre + "\"libinput Disable While Typing Enabled\" 0";
            system(commandLine.toStdString().c_str());
    }

    outString += "     Option \"HorizontalScrolling\" \"";
    if (ui->enableHorzScroll->isChecked()) {
            outString += "true\"\n";
            commandLine = commandLinePre + "\"libinput Horizontal Scroll Enabled\" 1";
            system(commandLine.toStdString().c_str());
       } else {
            outString += "false\"\n";
            commandLine = commandLinePre + "\"libinput Horizontal Scroll Enabled\" 0";
            system(commandLine.toStdString().c_str());
    }

    outString += "     Option \"LeftHanded\" \"";
    if (ui->enableLeftHanded->isChecked()) {
            outString += "true\"\n";
            commandLine = commandLinePre + "\"libinput Left Handed Enabled\" 1";
            system(commandLine.toStdString().c_str());
       } else {
            outString += "false\"\n";
            commandLine = commandLinePre + "\"libinput Left Handed Enabled\" 0";
            system(commandLine.toStdString().c_str());
    }

    outString += "     Option \"MiddleEmulation\" \"";
    if (ui->enableMiddleButton->isChecked()) {
            outString += "true\"\n";
            commandLine = commandLinePre + "\"libinput Middle Emulation Enabled\" 1";
            system(commandLine.toStdString().c_str());
       } else {
            outString += "false\"\n";
            commandLine = commandLinePre + "\"libinput Middle Emulation Enabled\" 0";
            system(commandLine.toStdString().c_str());
    }

    outString += "     Option \"NaturalScrolling\" \"";
    if (ui->enableNaturalScrolling->isChecked()) {
            outString += "true\"\n";
            commandLine = commandLinePre + "\"libinput Natural Scrolling Enabled\" 1";
            system(commandLine.toStdString().c_str());
       } else {
            outString += "false\"\n";
            commandLine = commandLinePre + "\"libinput Natural Scrolling Enabled\" 0";
            system(commandLine.toStdString().c_str());
    }

    outString += "     Option \"ScrollMethod\" \"";
    outString += ui->scrollMethod->currentText() += "\"\n";
    commandLine = commandLinePre + "\"libinput Scroll Method Enabled\" 0 0 0";
    if (!ui->scrollMethod->currentText().compare("twofinger")) {
        commandLine = commandLinePre + "\"libinput Scroll Method Enabled\" 1 0 0";
    }
    if (!ui->scrollMethod->currentText().compare("edge")) {
        commandLine = commandLinePre + "\"libinput Scroll Method Enabled\" 0 1 0";
    }
    if (!ui->scrollMethod->currentText().compare("button")) {
        commandLine = commandLinePre + "\"libinput Scroll Method Enabled\" 0 0 1";
    }
    system(commandLine.toStdString().c_str());


    outString += "     Option \"Tapping\" \"";
    if (ui->enableTapClick->isChecked()) {
            outString += "true\"\n";
            commandLine = commandLinePre + "\"libinput Tapping Enabled\" 1";
            system(commandLine.toStdString().c_str());
       } else {
            outString += "false\"\n";
            commandLine = commandLinePre + "\"libinput Tapping Enabled\" 0";
            system(commandLine.toStdString().c_str());
    }

    outString += "     Option \"TappingDragLock\" \"";
    if (ui->enableDragLock->isChecked()) {
            outString += "true\"\n";
            commandLine = commandLinePre + "\"libinput Tapping Drag Lock Enabled\" 1";
            system(commandLine.toStdString().c_str());
       } else {
            outString += "false\"\n";
            commandLine = commandLinePre + "\"libinput Tapping Drag Lock Enabled\" 0";
            system(commandLine.toStdString().c_str());
    }

    outString += "     Option \"ClickMethod\" \"";
    outString += ui->clickPad->currentText() += "\"\n";
    commandLine = commandLinePre + "\"libinput Click Methods Enabled\" 0 0";
    if (!ui->clickPad->currentText().compare("buttonareas")) {
        commandLine = commandLinePre + "\"libinput Click Methods Enabled\" 1 0";
    }
    if (!ui->clickPad->currentText().compare("clickfinger")) {
        commandLine = commandLinePre + "\"libinput Click Methods Enabled\" 0 1";
    }
    system(commandLine.toStdString().c_str());
    outString += "EndSection";


    QFile wfile("/usr/share/X11/xorg.conf.d/50-touchpad-gui.conf");
    if (wfile.exists())
            wfile.remove();
    if (!wfile.open(QFile::WriteOnly | QFile::Text))
    {
        qDebug() << "Could not open config file for writing.";
    }
    else {
        QTextStream out(&wfile);
        out << outString;
        wfile.close();
    }

  //QMessageBox::information( this, "Touchpad settings", "Restart X or reboot for changes to take effect." );
  this->close();
}
