<?php
	class l18n {
		
		public $supportedLanguages;
		public $defaultLanguage;
		public $defaultLocale;
		private $cookie;
		private $config;
		private $session;
		private $memoryCache;
		private $cacheKey;
		private $cookieLng;
		private $user;
		
		function __construct() {
			global $config, $memoryCache, $user, $session, $cookie, $db;
			
			$this->config = $config;
			$this->db = $db;
			$this->memoryCache = $memoryCache;
			$this->defaultLanguage = $this->config['settings']['default-lang'];
			$this->defaultLocale = $this->config['settings']['default-locale'];
			$this->user = $user;
			$this->cookie = $cookie;
			$this->session = $session;
			$this->cacheKey = 'sprtdLngs'; //Supported Languages memory cache key
			$this->cookieLng = 'usrLng';
			
			$this->supportedLanguages = $this->getSupportedLanguages();
									
		}
		
		public function getLanguages() {
			return $this->supportedLanguages;
		}
		
		public function getLanguageStringList() {
			$languages = array();
			
			if (is_array($this->supportedLanguages)) {
				foreach($this->supportedLanguages as $item) {
					$selected = $item['lang_code'] == $this->setCurrentLanguage() ? true : false;
					$languages[] = ['code'=>$item['lang_code'], 'string'=>$item['name_string'], 'selected'=>$selected];
				}
			}
			
			return $languages;
		}
		
		public function getLangNameByCode($langCode='') {
			$languages = $this->getLanguageStringList();
			if (is_array($languages)) {
				foreach($languages as $language) {
					if ($language['code'] == $langCode) return $language['string'];
				}
			}
			
			return $langCode;
		}
		
		public function gerLangStringByArray($array=[]) {
			$tmp = [];
			if (is_array($array) && !empty($array)) {
				foreach($array as $item) {
					$tmp[] = $item['name_string'];
				}
			}
			
			return implode(', ', $tmp);
		}
		
		public function getLocale($langCode) {
			$currentLocale = $this->defaultLocale;
			
			if (is_array($this->supportedLanguages)) {
				foreach($this->supportedLanguages as $values) {
					if ($langCode == $values['lang_code']) return $values['locale_string'];
				}
			}
						
			return $currentLocale;
		}
		
		public function setCurrentLanguage($passedLanguage='') {
			
			if ($this->user->isLogged()) {
				$userLng = $this->user->getLanguage();
				if ($userLng !== false) {
					$this->setLocaleEnvironment($this->getLocale($userLng));
					return $userLng;
				}
			}
						
			if ($passedLanguage == '') {
				
				$cookieLng = $this->cookie->readCookie($this->cookieLng);
				
				if ($cookieLng === false) {
					$language = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
				} else {
					$language = $cookieLng;
				}
				
				$this->setLocaleEnvironment($this->getLocale($language));
				return $language;
				
			} else {
				if ($this->filterValidLanguage($passedLanguage)) {
					$this->cookie->refreshCookie($this->cookieLng, $passedLanguage, strtotime('+10 years'));
					$this->setLocaleEnvironment($this->getLocale($passedLanguage));
					return $passedLanguage;
				} else {
					
					//Delete cookie if it exists and set default values
					$this->cookie->deleteCookie($this->cookieLng);
				}
			}
			
			$this->setLocaleEnvironment($this->getLocale($this->defaultLanguage));
			return $this->defaultLanguage;
		}
		
		public function filterValidLanguage($language) {
			if ($this->checkLanguage($language)) {
				return $language;
			} else {
				return $this->defaultLanguage;
			}
		}
		
		public function checkLanguage($language) {
			
			if (is_array($this->supportedLanguages)) {
				foreach($this->supportedLanguages as $values) {
					if ($language == $values['lang_code']) return true;
				}
			}
			
			return false;
		}
		
		public function getCountries($selectedId=0) {
			$countries = $this->db->orderBy('country_name', 'ASC')->get('countries');
						 
			foreach($countries as $index => $country) {
				$countries[$index]['selected'] = $country['id'] == $selectedId ? true : false;
			}
			
			return $countries;
		}
		
		public function getCountriesByISO2Code($selectedISO2Code='') {
			$countries = $this->db->orderBy('country_name', 'ASC')->get('countries');
						 
			foreach($countries as $index => $country) {
				$countries[$index]['selected'] = $country['country_iso2_code'] == $selectedISO2Code ? true : false;
			}
			
			return $countries;
		}
		
		public function getPhoneCodes($slectedPhoneCode='') {
			$countries = $this->db->orderBy('country_name', 'ASC')->get('countries');
			$phoneCodes = [];
			foreach($countries as $index => $country) {
				$phoneCodes[$index]['phonecode'] = $country['country_phonecode'];
				$phoneCodes[$index]['countryName'] = $country['country_name'];
				$phoneCodes[$index]['selected'] = $country['country_phonecode'] == $slectedPhoneCode ? true : false;
			}
			
			return $phoneCodes;
		}
		
		public function getSupportedLanguages() {
			
			$supportedLanguages = $this->memoryCache->read($this->cacheKey);
			
			if (is_null($supportedLanguages)) {
				$supportedLanguages = $this->db->orderBy('name_string', 'ASC')->get('users_languages');
				$this->memoryCache->write($this->cacheKey, $supportedLanguages, '+1 year');
			}
			
			return $supportedLanguages;
		}
		
		public function setLocaleEnvironment($locale) {
			$domain = 'messages';
			$locale = $locale.'.UTF-8';
			putenv('LC_ALL='.$locale);
			setlocale(LC_ALL, $locale);
			bindtextdomain($domain, LOCALE_DIR);
			textdomain($domain);
			setlocale(LC_TIME, $locale);
			bind_textdomain_codeset($domain, 'UTF-8');
		}

	}