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

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;

	myself.execute = function(window) {
		console.log(LOG, 'execute');
		return new Promise(function(resolve) {
			setTimeout(function() {
				if (process.platform === "win32") {
					myself._window = window;
					let token = session.get(session.KEYS.KERBEROS_TOKEN);
					if (!token || !token.length) {
						console.log(LOG, 'ignored SSO, invalid kerberos token');
						resolve('');
					} else {
						try {
							let instance = require(path.resolve(session.get(session.KEYS.DIRECTORY_ASARS), 'plugin_' + myself._name + '.asar', myself._name));
							instance.execute(myself._window).then(resolve).catch(resolve);
						} catch (e) {
							resolve();
						}
					}
				} else {
					console.log(LOG, 'unsupported', process.platform);
					resolve('unsupported' + process.platform);
				}
			}, 1);
		});
	};

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

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