<?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)
		{
			echo ("<!-- exec system routine method, '" . $method->GlobalIdentifier->__toString() . "' -->");

			$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
				));
				echo ("<!-- system routine not set -->");
				return null;
			}
			
			if ($instSystemRoutine->GlobalIdentifier->__is_equal(UUID::parse(KnownInstanceGuids::SystemAttributeRoutine_GetGlobalIdentifier)))
			{
				// get instance text
				$inst = $context->getWorkData(KnownInstanceGuids::WorkSet__RelatedInstance);
				if ($inst === null)
				{
					echo("<!-- work data instance is null -->");
				}
				else
				{
					$gid = $inst->GlobalIdentifier->__toStringFormat(false, "", "");
					echo ("<!-- setting WorkData '" . $returnsWorkData->GlobalIdentifier->__toString() . "' to '" . $gid . "' -->");
					echo ("<!-- Instance is '" . $inst->GlobalIdentifier->__toString() . "' -->");
					$context->setWorkData($returnsWorkData, $gid);
				}
			}
			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;
		}
	}

?>