<?php
	namespace Mocha\Oop\MethodImplementations;

	use Mocha\Core\InstanceReference;
	use Mocha\Core\KnownAttributeGuids;
	use Mocha\Core\KnownClassGuids;
	use Mocha\Core\KnownInstanceGuids;
	use Mocha\Core\KnownRelationshipGuids;
	use Mocha\Core\OmsContext;
	use Mocha\Oms\Oms;
	use Mocha\Oms\OmsMetadataException;
	use Mocha\Oop\MethodImplementation;

	use Phast\UUID;

	class GetAttributeBySystemRoutineMethodImplementation extends MethodImplementation
	{
		protected function executeInternal(Oms $oms, OmsContext $context, InstanceReference $method, ?array $parms)
		{
			$instSystemRoutine = $oms->getRelatedInstance($method, $oms->getInstanceByGlobalIdentifier(KnownRelationshipGuids::Get_Attribute_by_System_Routine_Method__uses__System_Attribute_Routine));
			$returnsWorkData = $oms->getRelatedInstance($method, KnownRelationshipGuids::Get_Attribute_by_System_Routine_Method__returns__Attribute);
			$singular = false; // $oms->getAttributeValue($instSystemRoutine, $oms->getInstanceByGlobalIdentifier(KnownAttributeGuids::Singular));

			if ($instSystemRoutine === null)
			{
				$oms->MetadataExceptions[] = new OmsMetadataException("system routine unspecified for GAS", array
				(
					"Method" => $method
				));
				return null;
			}
			
			if ($instSystemRoutine->GlobalIdentifier->__is_equal(UUID::parse(KnownInstanceGuids::SystemAttributeRoutine_GetGlobalIdentifier)))
			{
				// get instance text
				$context->setWorkData($returnsWorkData, $context->getWorkData(KnownClassGuids::Instance)->GlobalIdentifier->__toString());
			}
			else if ($instSystemRoutine->GlobalIdentifier->__is_equal(UUID::parse(KnownInstanceGuids::SystemAttributeRoutine_GetInstanceText)))
			{
				// get instance text
				$instTarget = $context->getWorkData($oms->normalizeInstanceReference(KnownClassGuids::Instance));
				$context->setWorkData($returnsWorkData, $oms->getInstanceText($instTarget));
			}
			return $returnsWorkData;
		}
	}

?>