<?php
namespace common\components;

use \Yii;

class RoleDefinition{
	
	
	
	public static function roleLabels(){
		return [
				'reception' => Yii::t('common/role' ,'Reception'),
				'admin' => Yii::t('common/role' ,'Administrator'),
				'employee' => Yii::t('common/role' ,'Employee'),
		];
	}
	
	public static function getRoleLabel($role){
		$result = null;
		$roleLabels = self::roleLabels();
		if ( array_key_exists($role, $roleLabels)){
			$result = $roleLabels[$role];
		}
		return $result;
	}
	
	
	public static function roleDefinitions(){
		return [
				'employee' =>  [
						'canAllow' => [  'employee'],
				],
				'admin' =>  [
						'canAllow' => ['admin','reception','employee'],
				],
				'reception' =>  [
						'canAllow' => [ ],
				],
		];
	}
	
	
	public static function getRoleDefinition($role){
		$defs = self::roleDefinitions();
		$result = null;
		if ( array_key_exists($role, $defs)){
			$result = $defs[$role];
		}
		$result = $defs[$role];
		return $result;
	}
	
	public static function getRolesCanAllow($role){
		$result = [];
		$def = self::getRoleDefinition($role);
		if ( isset($def)){
			$result = $def['canAllow'];
		}
	
		return $result;
	}

	public static function can($role){
		$result = false;
		if ( !Yii::$app->user->isGuest ){
			if ( isset(  $role)){
				if ( is_array($role)){
					foreach ($role as $r){
						$result |= Yii::$app->user->can($r);
					}
				}else if ( is_string($role)){
					$result = Yii::$app->user->can($role);
				}
			}
		}
		return $result;
	}
	
	public static function isAdmin(){
		return self::can('admin');
	}
	
	public static function isReception(){
		return self::can('reception');
	}

	public static function isEmployee(){
		return self::can('employee');
	}
	
	/*
	 * [
	 *  	'role1' => 'template1',
	 *      'role2' => 'template2,
	 *      ]
	 * */
	public static function getRoleTemplate($templates){
		$result = "";
		foreach ($templates as $role => $template ){
			if ( Yii::$app->user->can($role)){
				$result = $template;
				break;
			}
		}
		return $result;
	}
	
}