<?php 
//namespace core;

/**
* Language - simple language handler
**/
class Language{

	/** This one holds direct language "dictionary" */
	public static $_array=array();
	public static $_lang=LANGUAGE_CODE;

	/**
	* Define current language to use on calls
	*/
	public static function set($_lang = LANGUAGE_CODE){
		self::$_lang = $_lang;
	}

	/**
	* Load language definitions, all default starts and contains inside the folders as initials
	* First tries to load the foreign language if no use systems defined language
	* @param {string} $name of language to load
	*/
	public static function load($name = LANGUAGE_CODE){
		$file = LANGUAGE_PATH.self::$_lang."/".self::$_lang.".php";
		/* check if is readable */
		if(is_readable($file)){
			/* same as push_array but i like it more because it's not creating multidimensional and the [0] or else need */
			self::$_array += include($file);
			// array_push(self::$_array , include($file));
		}else{
			/* first try to get once again default sites language Version */
			$file = LANGUAGE_PATH.LANGUAGE_CODE."/".LANGUAGE_CODE.".php";
			if(is_readable($file)){
				self::$_array += include($file);
			}else{
				/* Fuck not working... display error */
				echo Error::display("Could not load language default file :$name nor original");
				die;
			}
		}
	}

	/**
	* Add language definitions to existing array
	* @param {string} $name of the addition "dictionary" file to load
	*/
	public static function add($name = LANGUAGE_CODE){
		$file = LANGUAGE_PATH.self::$_lang."/$name.php";
		if(is_readable($file)){
			self::$_array += include($file);
		}else{
			/* first try to get once again default sites language Version */
			$file = LANGUAGE_PATH.LANGUAGE_CODE."/$name.php";
			if(is_readable($file)){
				self::$_array += include($file);
			}else{
				echo Error::display("Could not add language file :$name nor original to existing array");
				die;
			}
		}
	}

	/**
	* Gets/Returns current language
	* @return {string}
	*/
	public static function get(){
		if(!empty(self::$_lang))
			return self::$_lang;
		else
			error_log("Could not return language string because it's empty");
	}

	/**
	* Gets/Returns current language array
	* @return {array}
	*/
	public static function getArray(){
		if(!empty(self::$_array))
			return self::$_array;
		else
			error_log("Could not return language array because it's empty");
	}

	/**
	* Get element from language array by key if such exists else will return the called value to prevent any errors
	* @param {string} $value
	* @return {string} if exists or the the requested value string
	*/
	public static function getValue($value){
		if(!empty(self::$_array[$value])){
			return self::$_array[$value];
		} else {
			return $value;
		}
	}
}