#ifndef AUTOSTART_H
#define AUTOSTART_H

#include <qglobal.h>
#include <QDir>
#include <QCoreApplication>

#define MY_APP_NAME "NumLockManager"
#define MY_APP_PATH QDir::toNativeSeparators(QCoreApplication::applicationFilePath())


#ifdef Q_OS_WIN
#include <QSettings>

class Autostart{
public:
    Autostart (){}
    ~Autostart(){delete settings;}

    bool status(){
        return !settings->value(MY_APP_NAME).toString().isEmpty();
    }

    void set(bool s){
        s ? settings->setValue(MY_APP_NAME, MY_APP_PATH) : settings->remove(MY_APP_NAME);
        settings->sync();
    }

    void switch_(){
        status() ? settings->remove(MY_APP_NAME) : settings->setValue(MY_APP_NAME, MY_APP_PATH);
        settings->sync();
    }
private:
    QSettings * settings = new QSettings("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat);
};

#endif

#ifdef Q_OS_LINUX
#include <QFile>
#include <QStandardPaths>

class Autostart{
public:
    Autostart (){
        file_data = file_data.replace("$", MY_APP_PATH);
    }

    bool status(){
        return QFile::exists(file_path);
    }

    void set(bool s){
        s ? make_file() : (void)QFile::remove(file_path);
    }

    void switch_(){
        status() ? (void)QFile::remove(file_path) : make_file();
    }

private:
    QString file_path = QStandardPaths::standardLocations(QStandardPaths::ConfigLocation)[0] + "/autostart/NumLockManager.desktop";
    QString file_data = QString("[Desktop Entry]\n"
                                "Encoding=UTF-8\n"
                                "Version=1.2\n"
                                "Type=Application\n"
                                "Name=NumLockManager\n"
                                "Comment=\n"
                                "Exec=$\n"
                                "RunHook=0\n"
                                "StartupNotify=false\n"
                                "Terminal=false\n"
                                "Hidden=false\n");



    void make_file(){
        QFile file(file_path);
        file.open(QIODevice::WriteOnly | QIODevice::Text);
        file.write(file_data.toUtf8());
        file.close();
    }
};


#endif

#endif // AUTOSTART_H
