/*
	PostNuke Wasteland Languagelibrary
	Author: NeoPhoenix
	Date: 	30.12.2011
*/

#if defined lib_lang_included
	#endinput
#else
	#define lib_lang_included
#endif

#include "n_lib/nfile"//nFile-System required
#define LANG_STARTUP		"/lang/startup.ini"
#define LANG_PATH			"/lang"
#define DEFAULT_LANGUAGE	"de"//German
#define MAX_LANGUAGES		(5)

forward bool:l_ValidLang(l_scut[]);
stock bool:l_ValidLang(l_scut[])
{
	new langs[MAX_LANGUAGES][3];
	nfile_GetArray("languages",langs,LANG_STARTUP);
	for(new l; l<MAX_LANGUAGES; l++)
	{
		if(!langs[l][0]) continue;
		if(!strcmp(langs[l],l_scut))
		{
			new filename[MAX_PATH_LEN];
			format(filename,MAX_PATH_LEN,"%s/%s/%s_info.ini",LANG_PATH,l_scut,l_scut);	
			if(nfile_GetBool("validate",filename) == true) return true;
		}
	}
	return false;
}

stock l_GetLangByID(lid)
{
	new langss[MAX_LANGUAGES][3],count_lang=0;
	nfile_GetArray("languages",langss,LANG_STARTUP);
	for(new l; l<MAX_LANGUAGES; l++)
	{
	    if(!langss[l][0]) continue;
		if(count_lang == lid) return langss[l];
	    count_lang++;
	}
	return langss[0];
}

stock l_GetPlayerLang(playerid)
{
	new lstring[3];
	GetPVarString(playerid,"l_scut",lstring,3);
	if(!lstring[0] || !l_ValidLang(lstring)) lstring = DEFAULT_LANGUAGE;
	return lstring;
}

stock l_SetPlayerLang(playerid,scut[])
{
	new lstring[3];
	if(!scut[0]) lstring = DEFAULT_LANGUAGE;
	else format(lstring,sizeof(lstring),"%s",scut);
	SetPVarString(playerid,"l_scut",lstring);
	return ;
}

#define l_GetText(%0,%1,%2,%3) l_LibText(%1,%2,%3,l_GetPlayerLang(%0))
stock l_LibText(library[],folder[],languagevar[],l_scut[])
{
	new filename[MAX_PATH_LEN],result[MAX_GET];
	format(filename,MAX_PATH_LEN,"%s/%s/%s/%s.txt",LANG_PATH,l_scut,library,folder);
	if(!nfile_Exists(filename)) return result;
	strmid(result,nfile_GetValue(languagevar,filename),0,MAX_GET,255);
	return result;
}

#include "misc/badwords"
has_badwords(text[])
{
	new blacklisted = 0;
	for(new i; i<sizeof(blacklist); i++)
	{
	    new found = strfind(text,blacklist[i]);
	    while(found != -1)
		{
		    blacklisted++;
			for(new c=found; c<(found+strlen(blacklist[i])); c++)
			{
			    text[c] = '*';
			}
			found = strfind(text,blacklist[i]);
		}
	}
	return blacklisted;
}