/*
	
	IRC-Bot "Kvasir"
    Copyright (C) 2016	Fenris Wolf (fenris@folksprak.org)
	
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
	
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
	
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
       
*/

var _fs = require("fs");

	
/**
 * @author fenris
 */
class klaso_kromajxo_ekstera extends klaso_kromajxo
{
	
	/**
	 * @author fenris
	 */
	protected vojo : string;
	
	
	/**
	 * @author fenris
	 */
	protected memoro : Object;
	
	
	/**
	 * @author fenris
	 */
	protected reagi_ : (enigo_kruda : Object, eligi : (eligo_kruda : Object)=>void, memoro ?: Object)=>void;
	
	
	/**
	 * @author fenris
	 */
	public constructor
	(
		identigilo : string = null,
		nomo : string = null,
		priskribo : string = null,
		uzo : string = null,
		auxtoro : string = null,
		reagi_ : (enigo_kruda : Object, eligi : (eligo_kruda : Object)=>void)=>void
	)
	{
		super(identigilo, nomo, priskribo, uzo, auxtoro);
		this.reagi_ = reagi_;
		this.vojo = null;
		this.memoro = {};
	}
	
	
	/**
	 * @author fenris
	 */
	public resxargi(fino : (eraro : Error)=>void = function (eraro : Error) : void {}) : void
	{
		let that : klaso_kromajxo_ekstera = this;
		klaso_kromajxo_ekstera.sxargi
		(
			this.vojo,
			function (eraro : Error, datumoj : Object) : void
			{
				if (eraro != null)
				{
					fino(eraro);
				}
				else
				{
					that.nomo = datumoj["nomo"];
					that.priskribo = datumoj["priskribo"];
					that.uzo = datumoj["uzo"];
					that.auxtoro = datumoj["auxtoro"];
					that.reagi_ = datumoj["reagi"];
					fino(null);
				}
			}
		);
	}
	
	
	/**
	 * @author fenris
	 */
	protected reagi(enigo_kruda : Object, eligi : (eligo_kruda : Object)=>void) : void
	{
		this.reagi_(enigo_kruda, eligi, this.memoro);
	}
	

	/**
	 * @author fenris
	 */
	protected static sxargi(vojo : string, uzado : (eraro : Error, datumoj : Object)=>void) : void
	{
		_fs.readFile
		(
			vojo,
			{"encoding": "utf8", "flag": "r"},
			function (eraro : Error, enhavo : string) : void
			{
				if (eraro != null)
				{
					uzado(eraro, null);
				}
				else
				{
					try
					{
						let datumoj : Object = eval("(function () {return (" + enhavo + ");})()");
						uzado(null, datumoj);
					}
					catch (escepto)
					{
						uzado(new Error(String(escepto)), null);
					}
				}
			}
		);
	}
	
	
	/**
	 * @author fenris
	 */
	public static krei(vojo : string, uzado : (eraro : Error, kromajxo : klaso_kromajxo_ekstera)=>void) : void
	{
		klaso_kromajxo_ekstera.sxargi
		(
			vojo,
			function (eraro : Error, datumoj : Object) : void
			{
				if (eraro != null)
				{
					uzado(eraro, null);
				}
				else
				{
					let kromajxo : klaso_kromajxo_ekstera = new klaso_kromajxo_ekstera
					(
						datumoj["identigilo"],
						datumoj["nomo"],
						datumoj["priskribo"],
						datumoj["uzo"],
						datumoj["auxtoro"],
						datumoj["reagi"]
					);
					kromajxo.vojo = vojo;
					uzado(null, kromajxo);
				}
			}
		);
	}
	
}