<?php
namespace Szurubooru\Controllers\ViewProxies;
use Szurubooru\Helpers\EnumHelper;
use Szurubooru\Privilege;
use Szurubooru\Services\PrivilegeService;

class UserViewProxy extends AbstractViewProxy
{
	private $privilegeService;

	public function __construct(PrivilegeService $privilegeService)
	{
		$this->privilegeService = $privilegeService;
	}

	public function fromEntity($user, $config = [])
	{
		$result = new \StdClass;
		if ($user)
		{
			$result->id = $user->getId();
			$result->name = $user->getName();
			$result->accessRank = EnumHelper::accessRankToString($user->getAccessRank());
			$result->registrationTime = $user->getRegistrationTime();
			$result->lastLoginTime = $user->getLastLoginTime();
			$result->avatarStyle = EnumHelper::avatarStyleToString($user->getAvatarStyle());
			$result->banned = $user->isBanned();

			if ($this->privilegeService->isLoggedIn($user))
			{
				$result->browsingSettings = $user->getBrowsingSettings();
			}

			if ($this->privilegeService->hasPrivilege(Privilege::VIEW_ALL_EMAIL_ADDRESSES) or
				$this->privilegeService->isLoggedIn($user))
			{
				$result->email = $user->getEmail();
				$result->emailUnconfirmed = $user->getEmailUnconfirmed();
			}

			$result->confirmed = $user->isAccountConfirmed();
		}
		return $result;
	}
}
