/*
	
	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 _irc = require("./node_modules/irc");

	
/**
 * @author fenris
 */
class klaso_kliento_irc extends klaso_kliento
{
	
	/**
	 * @author fenris
	 */
	protected irc : /*_irc.Client*/any;
	
	
	/**
	 * @author fenris
	 */
	public constructor(agordoj : Object, prokrasto : int)
	{ 
		super(prokrasto);
		this.irc = new _irc.Client(agordoj["servilo"], agordoj["kromnomo"], {"channels": agordoj["kanaloj"], "userName": agordoj["uzantnomo"], "password": agordoj["pasvorto"], "autoConnect": false});
	}
	
	
	/**
	 * @author fenris
	 */
	public konekti(parametroj : Object, fino : ()=>void) : void
	{
		let that : klaso_kliento_irc = this;
		this.irc["addListener"]
		(
			"message",
			function (sendinto : string, fonto : string, enhavo : string) : void
			{
				that.enigi(new klaso_evento_eniga_mesagxo(fonto, sendinto, enhavo));
			}
		);
		this.irc.connect
		(
			3,
			function () : void
			{
				fino();
			}
		);
	}
	
	
	/**
	 * @author fenris
	 */
	protected eligi_rekte(eligo : klaso_evento_eliga) : void
	{
		     if (eligo instanceof klaso_evento_eliga_mesagxo)
		{
			let eligo_mesagxo : klaso_evento_eliga_mesagxo = <klaso_evento_eliga_mesagxo>(eligo);
			try
			{
				let cxeno : string = "";
				{
					let alparolato : string = eligo_mesagxo.alparolato_legi();
					if (alparolato != null) cxeno += alparolato + ": ";
				}
				{
					let enhavo : string = eligo_mesagxo.enhavo_legi();
					if (enhavo != null) cxeno += enhavo;
				}
				this.irc["say"](eligo_mesagxo.celo_legi(), cxeno);
			}
			catch (escepto)
			{
				console.warn(escepto);
			}
		}
		else
		{
		}
	}
	
	
	/**
	 * @author fenris
	 */
	protected anigxi_rekte(kanalo : string, modo : boolean = true) : void
	{
		if (modo)
		{
			this.irc["join"](kanalo);
		}
		else
		{
			this.irc["part"](kanalo, "vi kan maka en ander verld");
		}
	}
	
}