// Input expected "name (rating) : message". (rating) optional
function autociv_GetNameRatingText(text)
{
	let spliterIndex = text.indexOf(":");
	if (spliterIndex == -1)
		return false;

	let { nick, rating } = splitRatingFromNick(text.slice(0, spliterIndex).trim());
	if (!nick)
		return false;
	return {
		"name": nick,
		"rating": rating,
		"text": text.slice(spliterIndex + 1)
	}
};

var g_HostEnhanced_SharedCommands = {
	"permaban": {
		"description": "Add player to permanent banlist from your hosted games.",
		"handler": (player) =>
		{
			log("debug  ~hostenhancedSharedCommands.js:permaban")
			if (player == "")
				return selfMessage("You need to type a nick to add to banlist.")
			let nick = splitRatingFromNick(player).nick;
			log("debug  ~hostenhancedSharedCommands.js:permaban:nick"+nick);
			botManagerHostEnhanced.get("ban").instance.setValue(nick, nick);
			selfMessage(`You have banned ${nick}.`);
			log("debug  ~hostenhancedSharedCommands.js:permaban:kicking user:"+nick);
			kickPlayer(nick, true);
		}
	},
	"unban": {
		"description": "Removes player from your permanent banlist of hosted games.",
		"handler": (player) =>
		{
			if (player == "")
				return selfMessage("You need to type a nick remove from banlist.")
			let nick = splitRatingFromNick(player).nick;
			botManagerHostEnhanced.get("ban").instance.removeValue(nick);
			selfMessage(`You have unbanned ${nick}.`);			
		}
	},
	"banclear": {
		"description": "Clear list of permanent banned players from your hosted games.",
		"handler": () =>
		{
			botManagerHostEnhanced.get("ban").instance.removeAllValues();
			selfMessage("You have cleared muted list.");
		}
	},
	"banlist": {
		"description": "List of banned players of your hosted games.",
		"handler": () =>
		{
			let list = botManagerHostEnhanced.get("ban").instance.getIds();
			selfMessage(`Banned ${list.length} people`);
			this.counter = 1;
			this.names = "";
			this.words = 3;
			for (let name of list) {															
				if (list.length < 20) {	selfMessage("|" + name ); } else { 
					this.names += "| "+ name +" ";
					if ( this.counter % this.words == 0) {
						selfMessage(this.names)						
						this.names="";
				}
			}
			this.counter++;
			} //for
			selfMessage(this.names);
	}
}
};

function HostEnhanced_InitSharedCommands()
{
	if (!(botManagerHostEnhanced.messageInterface in HostEnhanced_InitSharedCommands.pipe))
		return;

	for (let key in g_HostEnhanced_SharedCommands)
		HostEnhanced_InitSharedCommands.pipe[botManagerHostEnhanced.messageInterface](key);
}

HostEnhanced_InitSharedCommands.pipe = {
	"lobby": key =>
	{
		ChatCommandHandler.prototype.ChatCommands[key] = {
			"description": g_HostEnhanced_SharedCommands[key].description,
			"handler": text =>
			{
				g_HostEnhanced_SharedCommands[key].handler(text)
				return true
			}
		}
	},
	"gamesetup": key =>
	{
		g_NetworkCommands["/" + key] = text =>
		{
			g_HostEnhanced_SharedCommands[key].handler(text)
			return true
		}
	},
	"ingame": key =>
	{
		g_NetworkCommands["/" + key] = text =>
		{
			g_HostEnhanced_SharedCommands[key].handler(text)
			return true
		}
	}
}
