<?php

namespace App\UserAccess;

/**
 * The class works only with the session in order to decide if the current user is authenticated
 * or has acccess to various sections/options.
 * @author Gabriel Grosu
 */
class CurrentUserAccessFast extends CurrentUserAccess
{

	/**
	 * Transform this user to a super-user, allowing access to any section & option
	 * @return \App\UserAccess\CurrentUserAccessFast
	 */
	public function becomeSuperUser()
	{
		if ($this->isSuperUser) {
			throw new \Exception("Trying to become SuperUser when you are already SuperUser is forbidden");
		}
		$this->isSuperUser = true;
		return $this;
	}

	/**
	 * Transform this user back to a normal user, allowing access to standard sections & options
	 * @return \App\UserAccess\CurrentUserAccessFast
	 */
	public function becomeNormalUser()
	{
		if (!$this->isSuperUser) {
			throw new \Exception("Trying to become NormalUser when you are already NormalUser is forbidden");
		}

		$this->isSuperUser = false;
		return $this;
	}

	/**
	 * Checks if currently you are a super user.
	 * @return boolean
	 */
	public function isSuperUser()
	{
		return $this->isSuperUser;
	}


	/**
	 * {@inheritDoc}
	 * @see \App\UserAccess\UserAccess::hasAccessToSection()
	 */
	public function hasAccessToSection($section)
	{
		// if this is a superuser
		if ($this->isSuperUser) { // TODO: [Lucian] why not always return true ?
			$sectionFound = false;
			ReferenceUserAccess::getInstance()->walkTreeSections(ReferenceUserAccess::getInstance()->getAccessTree(), function($k, $v, $o) use (&$sectionFound, $section) {
				if (!$sectionFound && $v['id']==$section) {
					$sectionFound = true;
				}
			});

			return $sectionFound;
		}

		global $S;
		return $S['auth']['usersAccess'][$section]??false;
	}


	/**
	 * {@inheritDoc}
	 * @see \App\UserAccess\UserAccess::hasAccessToOption()
	 */
	public function hasAccessToOption($option)
	{
		if ($this->isSuperUser) {  // TODO: [Lucian] why not always return true ?
			$optionFound = false;
			ReferenceUserAccess::getInstance()->walkTreeSections(ReferenceUserAccess::getInstance()->getAccessTree(), function($k, $v, $o) use (&$optionFound, $option) { // TODO: why so complicated? nu e nevoie de recursivitate
				if (!$optionFound) { // TODO: this is access to option - why need all this?
					foreach ($v['options'] as $opt) {
						if (!$optionFound && $opt['id']==$option) {
							$optionFound = true;
						}
					}
				}
			});

			return $optionFound;
		}

		global $S;
		return (bool)$S['auth']['userOptionsAccess'][$option];
	}

	/**
	 * {@inheritDoc}
	 * @see \App\UserAccess\UserAccessAbstract::getAccessOptionValue()
	 */
	public function getAccessOptionValue($option)
	{

		if ($this->isSuperUser) {  // TODO: [Lucian] why not always return true ?
			$optionData = null;
			ReferenceUserAccess::getInstance()->walkTreeSections(ReferenceUserAccess::getInstance()->getAccessTree(), function($k, $v, $o) use (&$optionData, $option) {
				if (!$optionData) {
					foreach ($v['options'] as $opt) {
						if (!$optionData && $opt['id']==$option) {
							$optionData = $opt;
						}
					}
				}
			});

			if ($optionData) {
				if (isset($optionData['superUserValue'])) {
					return $optionData['superUserValue'];
				}
				elseif($optionData['type']=='checkbox') {
					return true;
				}
			}

			// for any other case, return "access denied"
			return false;
		}

		if ($this->hasAccessToOption($option)) {
			global $S;
			return $S['auth']['userOptionsAccess'][$option];
		}

		return null;
	}


	/**
	 * {@inheritDoc}
	 * @see \App\UserAccess\CurrentUserAccess::isAuthenticated()
	 */
	public function isAuthenticated()
	{
		if ($this->isSuperUser) {
			return true;
		}

		// only checks the session stuff
		return $this->isAuthenticated_checkSession();

	}


	public function getAccessibleSectionsList()
	{
		global $S;
		return array_keys(array_filter((array)$S['auth']['usersAccess'])); // TODO: simplify this? or move somewhere else?
	}

}
