/*
	
	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/>.
       
*/

type tipo_ero = {kromajxo : klaso_kromajxo; aktiva : boolean;};


/**
 * @author fenris
 */
abstract class klaso_kliento
{
	
	/**
	 * @author fenris
	 */
	protected kromajxoj : {[identigilo : string] : tipo_ero};
	
	
	/**
	 * @author fenris
	 */
	protected vico_eliga : klaso_vico<klaso_evento_eliga>;
	
	
	/**
	 * @author fenris
	 */
	protected prokrasto : int;
	
	
	/**
	 * @author fenris
	 */
	protected foo : any;
	
	
	/**
	 * @author fenris
	 */
	public constructor(prokrasto : int = null)
	{
		this.kromajxoj = {};
		this.vico_eliga = new klaso_vico<klaso_evento_eliga>();
		this.foo = null;
		this.prokrasto = prokrasto;
	}
	
	
	/**
	 * @author fenris
	 */
	public stirado_arangxi
	(
		permesuloj : Array<string> = []
	)
	{
		let that : klaso_kliento = this;
		this.kromajxo_aldoni
		(
			new klaso_kromajxo_stirado
			(
				permesuloj,
				{
					"anigxi": function (kanalo : string, modo : boolean = true) : void
					{
						that.anigxi_rekte(kanalo, modo);
					},
					"listigi": function (celo : string) : void
					{
						let enhavo : string = Object.keys(that.kromajxoj).map(identigilo => (that.kromajxoj[identigilo].aktiva ? "+" : "-") + identigilo).join(", ");
						that.eligi(new klaso_evento_eliga_mesagxo(celo, null, enhavo));
					},
					"detaloj": function (identigilo : string, celo : string) : void
					{
						if (identigilo in that.kromajxoj)
						{
							let ero : tipo_ero = that.kromajxoj[identigilo];
							let enhavoj : Array<string> =
							[
								"-- nomo: " + ero.kromajxo.nomo_legi(),
								"-- auxtoro: " + ero.kromajxo.auxtoro_legi(),
								"-- priskribo: " + ero.kromajxo.priskribo_legi(),
								"-- uzo: " + ero.kromajxo.uzo_legi(),
								"-- aktiva: " + (ero.aktiva ? "jes" : "ne"),
							];
							enhavoj.forEach(enhavo => that.eligi(new klaso_evento_eliga_mesagxo(celo, null, enhavo)));
						}
					},
					"sxalti": function (identigilo : string, aktiva : boolean = null, celo : string = null) : void
					{
						if (identigilo in that.kromajxoj)
						{
							if (identigilo != "stirado")
							{
								let aktiva_ : boolean = (aktiva != null) ? aktiva : (! that.kromajxoj[identigilo].aktiva);
								that.kromajxoj[identigilo].aktiva = aktiva_;
								if (celo != null) that.eligi(new klaso_evento_eliga_mesagxo(celo, null, "kromajxo '" + identigilo + "' " + (aktiva_ ? "aktivigita" : "malaktivigita") + ""));
							}
							else
							{
								if (celo != null) that.eligi(new klaso_evento_eliga_mesagxo(celo, null, "vi ne vere volas sxalti la stirad-kromajxon ..."));
							}
						}
					},
					"resxargi": function (identigilo : string, celo : string = null) : void
					{
						let ero : tipo_ero = that.kromajxoj[identigilo];
						if (ero.kromajxo instanceof klaso_kromajxo_ekstera)
						{
							let kromajxo : klaso_kromajxo_ekstera = <klaso_kromajxo_ekstera>(ero.kromajxo);
							kromajxo.resxargi
							(
								function (eraro : Error) : void
								{
									if (eraro != null)
									{
										console.warn(eraro);
										if (celo != null) that.eligi(new klaso_evento_eliga_mesagxo(celo, null, "resxargado fiaskis; detaloj trovigxas je la konzolo"));
									}
									else
									{
										if (celo != null) that.eligi(new klaso_evento_eliga_mesagxo(celo, null, "kromajxo '" + identigilo + "' resxargita"));
									}
								}
							);
						}
					},
					"aldoni": function (nomo : string, celo : string= null) : void
					{
						if ((new RegExp("\\w")).test(nomo))
						{
							let vojo : string = "fontoj/kromajxoj/kromajxo_" + nomo + ".js";
							klaso_kromajxo_ekstera.krei
							(
								vojo,
								function (eraro : Error, kromajxo : klaso_kromajxo_ekstera) : void
								{
									if (eraro != null)
									{
										console.warn(eraro);
										if (celo != null) that.eligi(new klaso_evento_eliga_mesagxo(celo, null, "aldoni kromajxon fiaskis"));
									}
									else
									{
										that.kromajxo_aldoni(kromajxo);
										if (celo != null) that.eligi(new klaso_evento_eliga_mesagxo(celo, null, "kromajxo sukcese aldonita"));
									}
								}
							);
						}
					},
					"elmontri_helpon": function (celo : string) : void
					{
						that.eligi(new klaso_evento_eliga_mesagxo(celo, null, "'!kromajxo listigi': listigi la nomojn de cxiuj kromajxoj kaj elmontri cxu ili estas sxaltitaj"));
						that.eligi(new klaso_evento_eliga_mesagxo(celo, null, "'!kromajxo detaloj [nomo]': elmontri detalojn pri la kromajxo kun la nomo [nomo]"));
					},
				}
			)
		);
	}
	
	
	/**
	 * @author fenris
	 */
	public kromajxo_aldoni(kromajxo : klaso_kromajxo, aktiva : boolean = true, anstatauxigi : boolean = false) : void
	{
		let identigilo : string = kromajxo.identigilo_legi();
		if (identigilo in this.kromajxoj)
		{
			if (anstatauxigi && (identigilo != "stirado"))
			{
				console.warn("kromajxo '" + identigilo + "' jam registrita; gxi anstatauxigatos");
				this.kromajxoj[identigilo] = {"kromajxo": kromajxo, "aktiva": aktiva};
			}
			else
			{
				console.warn("kromajxo '" + identigilo + "' jam registrita; gxi NE anstatauxigatos");
			}
		}
		else
		{
			this.kromajxoj[identigilo] = {"kromajxo": kromajxo, "aktiva": aktiva};
		}
	}
	
	
	/**
	 * @author fenris
	 */
	public abstract konekti(parametroj : Object, fino : ()=>void) : void;
	
	
	/**
	 * @author fenris
	 */
	public enigi(enigo : klaso_evento_eniga) : void
	{
		let that : klaso_kliento = this;
		Object.keys(this.kromajxoj).forEach
		(
			function (identigilo : string) : void
			{
				let ero : tipo_ero = that.kromajxoj[identigilo];
				{
					if (ero.aktiva)
					{
						ero.kromajxo.enigi
						(
							enigo,
							function (eligo : klaso_evento_eliga) : void
							{
								try
								{
									if (enigo["fonto"].indexOf("#") < 0)
									{
										eligo["celo"] = enigo["sendinto"];
									}
								}
								catch (escepto)
								{
									console.warn(escepto);
								}
								that.eligi(eligo);
							}
						)
					}
				}
			}
		);
	}
	
	
	/**
	 * @author fenris
	 */
	protected abstract eligi_rekte(eligo : klaso_evento_eliga) : void;
	
	
	/**
	 * @author fenris
	 */
	protected abstract anigxi_rekte(kanalo : string, modo : boolean) : void;
	
	
	/**
	 * @author fenris
	 */
	protected eligi(eligo : klaso_evento_eliga) : void
	{
		let that : klaso_kliento = this;
		
		function fari() : void
		{
			if (that.vico_eliga.malplena())
			{
				clearInterval(that.foo);
				that.foo = null;
			}
			else
			{
				that.eligi_rekte(that.vico_eliga.preni());
			}
		}
		
		if (this.prokrasto == null)
		{
			that.eligi_rekte(eligo);
		}
		else
		{
			this.vico_eliga.doni(eligo);
			if (this.foo == null)
			{
				fari();
				this.foo = setInterval(fari, this.prokrasto);
			}
		}
	}
	
}