<?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\Oms\OmsMetadataExceptionSeverity;
	use Mocha\Oop\MethodImplementation;

	use Phast\UUID;

	class GetReferencedInstanceSetMethodImplementation extends MethodImplementation
	{
		protected function executeInternal(Oms $oms, OmsContext $context, InstanceReference $method, ?array $parms)
		{
			$forClass = $oms->getRelatedInstance($method, KnownRelationshipGuids::Method__for__Class);
			$returnsWorkSet = $oms->getRelatedInstance($method, KnownRelationshipGuids::Get_Referenced_Instance_Set_Method__returns__Work_Set);
			$loopOnInstanceSet = $oms->getRelatedInstance($method, KnownRelationshipGuids::Get_Referenced_Instance_Set_Method__uses_reference__Executable_returning_Instance_Set);
			$relationship = $oms->getRelatedInstance($method, KnownRelationshipGuids::Get_Referenced_Instance_Set_Method__uses_answer__Executable_returning_Instance_Set);
			
			$targetInstance = $context->getWorkData($forClass);
			$mx = array
			(
				"Method Instance" => $method,
				"Returns Work Set" => $returnsWorkSet,
				"Loop on Instance Set" => $loopOnInstanceSet,
				"Get Relationship" => $relationship,
				"For Class" => $forClass,
				"For Class Instantiation" => $targetInstance,
				"Parm Setting" => "For " . $forClass->InstanceKey . " = " . ($targetInstance === null ? "empty" : $targetInstance->InstanceKey)
			);
			if ($targetInstance !== null)
			{
				$singular = $oms->getAttributeValue($returnsWorkSet, KnownAttributeGuids::Singular);
				if ($singular != 1)
				{
					$singular = $oms->getAttributeValue($method, KnownAttributeGuids::Singular);
				}
				if ($singular == 1)
				{
					$value = $oms->getRelatedInstance($targetInstance, $relationship);
				}
				else
				{
					//$oms->MetadataExceptions[] = new OmsMetadataException("GRS: returning nonsingular work set", $mx, $context->CallStack, OmsMetadataExceptionSeverity::Information);
					$value = $oms->getRelatedInstances($targetInstance, $relationship);
				}
				$mx["Got Value"] = $value;
				$context->setWorkData($returnsWorkSet, $value);
			}
			else
			{
				//echo ("exec GRS for class : " . $forClass->InstanceKey);
				$oms->MetadataExceptions[] = new OmsMetadataException("GRS: target instance unspecified", $mx, $context->CallStack);
			}
			//$this->MetadataExceptions[] = new OmsMetadataException("GRS", $mx, null, OmsMetadataExceptionSeverity::Information);
			return $returnsWorkSet;
		}
	}

?>