/*
    If you reading this, it's too late. You have to endure my horribly written code. 
    On a serious note, though. If you do use this code for any of your own projects, plase credit me.
    https://www.gta5-mods.com/users/JustSomeAussie
*/



var app = require('electron').remote; 
        var dialog = app.dialog;
        const fs = require('fs');
        var ipc = require('electron').ipcRenderer;

        let installBtn = document.getElementById("installBtn");
        let removeBtn = document.getElementById('removeBtn');


        document.addEventListener('DOMContentLoaded', () => {
        installBtn.addEventListener('click' , () => {
            install();
        }) 

        removeBtn.addEventListener('click', () => {
            remove();
        })
    })



var app = require('electron').remote; 
var dialog = app.dialog;


function install() {
    // Reading shit from paths.json
    let rawdata = fs.readFileSync('./resources/app/paths.json');
    let paths = JSON.parse(rawdata);
    let modFolderPath = paths.modFolderPath;
    let gtaPath = paths.gtaPath;

    if (gtaPath == null) {
        process.exit();
    }

    // All the files in modFolderPath are set to readData
    let readData = fs.readdirSync(paths.modFolderPath);
    
    for (let i = 0; i < readData.length; i++) {
        let source = modFolderPath + readData[i];
        let destination = gtaPath + readData[i];

        // This doesn't actually really do anything, if readData[i] is a directory, then it basically just continues. 
        // Tbh i can't be bothered explaining this.
        fs.lstat(source, (err, stats) => {
            if (err) console.log('');

            if (stats.isDirectory) {
                console.log('');
            }
        })

        fs.createReadStream(source).pipe(fs.createWriteStream(destination));
    }

    // Writes all the filenames to mods.json
    let data = JSON.stringify(readData, null, 2);  
    fs.writeFileSync('./resources/app/mods.json', data);  
}

function remove() {
    let rawdata = fs.readFileSync('./resources/app/paths.json');
    let paths = JSON.parse(rawdata);
    let rawdata2 = fs.readFileSync('./resources/app/mods.json');
    let mods = JSON.parse(rawdata2);
    let modFolderPath = paths.modFolderPath;
    let gtaPath = paths.gtaPath;

    for (let i = 0; i < mods.length; i++) {
        fs.unlink(gtaPath + mods[i]);
        console.log('Removed: ' + gtaPath + mods[i]);
    }
}