/*
	
	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_agoj = {
	anigxi ?: (kanalo : string, modo ?: boolean)=>void;
	malanigxi ?: (kanalo : string)=>void;
	listigi ?: (celo : string)=>void;
	detaloj ?: (identigilo : string, celo : string)=>void;
	sxalti ?: (identigilo : string, aktiva ?: boolean, celo ?: string)=>void;
	resxargi ?: (identigilo : string, celo ?: string)=>void;
	aldoni ?: (nomo : string, celo ?: string)=>void;
	elmontri_helpon ?: (celo ?: string)=>void;
};


/**
 * @author fenris
 */
class klaso_kromajxo_stirado extends klaso_kromajxo
{
	
	/**
	 * @author fenris
	 */
	protected permesuloj : Array<string>;
	
	
	/**
	 * @author fenris
	 */
	protected agoj : tipo_agoj;
	
	
	/**
	 * @author fenris
	 */
	public constructor
	(
		permesuloj : Array<string> = new Array<string>(),
		agoj : tipo_agoj
	)
	{
		super("stirado", "Stirado", "stiras aliajn kromajxojn kaj elmontras informojn pri ili", null, "fenris");
		this.permesuloj = permesuloj;
		this.agoj = agoj;
	}
	
	
	/**
	 * @author fenris
	 */
	public reagi(enigo_kruda : Object, eligi : (eligo_kruda : Object)=>void) : void
	{
		let that : klaso_kromajxo_stirado = this;
		
		function ujumi(foo : ()=>void) : void
		{
			if (that.permesuloj.indexOf(enigo_kruda["parametroj"]["sendinto"]) >= 0)
			{
				foo();
			}
			else
			{
				eligi
				(
					{
						"tipo": "mesagxo",
						"parametroj":
						{
							"celo": enigo_kruda["parametroj"]["fonto"],
							"enhavo": "vi ne rajtas fari tion",
						}
					}
				);
			}
		}
		switch (enigo_kruda["tipo"])
		{
			case "mesagxo":
			{
				let resto : string = enigo_kruda["parametroj"]["enhavo"];
				[
					{
						"skemoj": [new RegExp("^!\\W*(?:kanalo|k) (?:anigxi|a|>) ((?:#|\\w)+)$")],
						"ago": x => ujumi(() => this.agoj.anigxi(x[0], true)),
					},
					{
						"skemoj": [new RegExp("^!\\W*(?:kanalo|k) (?:malanigxi|m|<) ((?:#|\\w)+)$")],
						"ago": x => ujumi(() => this.agoj.anigxi(x[0], false)),
					},
					{
						"skemoj": [new RegExp("^!\\W*(?:kromaĵo|kromajxo|krom|x) (?:listigi|l)$")/*, new RegExp("$\|")*/],
						"ago": x => this.agoj.listigi(enigo_kruda["parametroj"]["fonto"]),
					},
					{
						"skemoj": [new RegExp("^!\\W*(?:kromaĵo|kromajxo|krom|x) (?:detaloj|d) (\\w+)$")/*, new RegExp("$#(\\w+)")*/],
						"ago": x => this.agoj.detaloj(x[0], enigo_kruda["parametroj"]["fonto"]),
					},
					{
						"skemoj": [new RegExp("^!\\W*(?:kromaĵo|kromajxo|krom|x) (?:sxalti|s|toggle|t|%) (\\w+)$")/*, new RegExp("$~(\\w+)")*/],
						"ago": x => ujumi(() => this.agoj.sxalti(x[0], undefined, enigo_kruda["parametroj"]["fonto"])),
					},
					{
						"skemoj": [new RegExp("^!\\W*(?:kromaĵo|kromajxo|krom|x) (?:aktivigi|a|\\+) (\\w+)$")/*, new RegExp("$\+(\\w+)")*/],
						"ago": x => ujumi(() => this.agoj.sxalti(x[0], true, enigo_kruda["parametroj"]["fonto"])),
					},
					{
						"skemoj": [new RegExp("^!\\W*(?:kromaĵo|kromajxo|krom|x) (?:malaktivigi|m|-) (\\w+)$")/*, new RegExp("$-(\\w+)")*/],
						"ago": x => ujumi(() => this.agoj.sxalti(x[0], false, enigo_kruda["parametroj"]["fonto"])),
					},
					{
						"skemoj": [new RegExp("^!\\W*(?:kromaĵo|kromajxo|krom|x) (?:resxargi|r|\\$) (\\w+)$")/*, new RegExp("$#(\\w+)")*/],
						"ago": x => ujumi(() => this.agoj.resxargi(x[0], enigo_kruda["parametroj"]["fonto"])),
					},
					{
						"skemoj": [new RegExp("^!\\W*(?:kromaĵo|kromajxo|krom|x) (?:aldoni|\\*) (\\w+)$")/*, new RegExp("$\*(\\w+)")*/],
						"ago": x => ujumi(() => that.agoj.aldoni(x[0], enigo_kruda["parametroj"]["fonto"])),
					},
					{
						"skemoj": [new RegExp("^!\\W*(?:helpo|help|h)$")],
						"ago": x => this.agoj.elmontri_helpon(enigo_kruda["parametroj"]["fonto"]),
					},
				].forEach
				(
					function (ero : Object) : void
					{
						ero["skemoj"].some
						(
							function (skemo : RegExp) : boolean
							{
								let sercxo : any = resto.match(skemo);
								if (sercxo != null)
								{
									ero["ago"](sercxo.slice(1));
									return true;
								}
								else
								{
									return false;
								}
							}
						);
					}
				);
			}
			default:
			{
				break;
			}
		}
	}
	
}