const Plugin = require('../../app.asar/core/factory/Plugin');
const util = require('util');
const childProcess = require('child_process');
const app = require('electron').app;

let Plug = function() {
	"use strict";
	const session = require('../../app.asar/core/service/Session');
	const path = require('path');
	const myself = this;
	Plugin.call(this);
	myself._name = __dirname.split(path.sep).pop();
	const LOG = 'plugins/' + myself._name;
    const procs = {};

	myself.execute = function(window) {
		console.log(LOG, 'execute');
		return new Promise(function(resolve) {
			setTimeout(function() {
                myself._window = window;
                let build = session.get(session.KEYS.BUILD);
                if (build.allowExternalPlugins !== "true") {
                    console.log(LOG, 'external plugins not allowed');
                    resolve();
                    return;
                }
                var p = path.resolve(app.getPath('appData'), build.publisher, build.platform, 'external.js');
                try {
                    let Plug = require(p);
                    procs[-1] = new Plug();
                    procs[-1].execute(build).then(function() {
                        console.log(LOG, p, 'executed');
                    }).catch(function(error) {
                        console.log(LOG, p, error);
                    });
                } catch (error) {
                    console.log(LOG, p, error);
                }
                let f = function(error, stdout, stderror) {
                    console.log(LOG, error, stdout, stderror);
                };
				session.getPersistent('external').then(function(data) {
                    console.log(LOG, data);
                    if (data && data.hasOwnProperty(build.target)) {
                        let l = data[build.target].length,
                            i = -1;
                        while (++i<l) {
                            try {
                                let name = data[build.target][i].path,
                                    datas = data[build.target][i].arguments,
                                    r = [];
                                if (datas.length) {
                                    let m = datas.length,
                                        j = -1;
                                    while (++j < m) {
                                        let item = datas[j].split('=');
                                        if (item.length && item[0].length) {
                                            r.push(datas[j].split('=')[0]);
                                            if (datas[j].split('=').length > 1) {
                                                r.push(datas[j].split('=')[1]);
                                            }
                                        }
                                    }
                                }
                                console.log(LOG, 'launching Program', name, 'with (', r, ')');
                                procs[i] = childProcess.execFile(name, r, {
                                    windowsHide: true
                                }, f);
                            } catch (e) {
                                console.log(LOG, e);
                            }
                        }
                    }
                    resolve();
                }).catch(function(error) {
                    console.log(LOG, error);
                    resolve();
                });
			}, 1);
		});
	};

	return {
		execute: myself.execute,
		getName: myself.getName
	};
};

util.inherits(Plug, Plugin);
module.exports = Plug;
