<?php
	namespace Mocha\Oop\MethodImplementations;

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

	class ProcessRelatedUpdatesMethodImplementation extends MethodImplementation
	{

		protected function executeInternal(Oms $oms, OmsContext $context, InstanceReference $method, ?array $parms)
		{
			$classForPRU = $oms->getRelatedInstance($method, KnownRelationshipGuids::Process_Related_Updates_Method__processes_for__Class);
			if ($classForPRU === null)
			{
				$oms->MetadataExceptions[] = new OmsMetadataException("Class for PRU is empty", [
					"Process Related Updates Method" => $method
				]);
				return;
			}

			if ($classForPRU !== null)
			{
			}

			//!HACK!
			$instForPRU = $oms->getNthInstanceOf($classForPRU, 1);
			if ($instForPRU !== null)
			{
				//echo("current work data for '" . $instForPRU->GlobalIdentifier . "' = '" . $context->getWorkData($instForPRU->GlobalIdentifier->__toString()) . "'");
			}

			$instExecutablesForPUMB = $oms->getRelatedInstances($method, KnownRelationshipGuids::Process_Related_Updates_Method__uses__Executable_for_PUMB);
			foreach ($instExecutablesForPUMB as $instExecutableForPUMB)
			{
				if ($instExecutableForPUMB !== null)
				{
					$assignsAttribute = $oms->getRelatedInstance($instExecutableForPUMB, KnownRelationshipGuids::Assign_Attribute_Method__assigns__Attribute);
					$usesExecutableReturningAttribute = $oms->getRelatedInstance($instExecutableForPUMB, KnownRelationshipGuids::Assign_Attribute_Method__uses__Executable_returning_Attribute);

					//$context->setWorkData($assignsAttribute->GlobalIdentifier->__toString(), $usesExecutableReturningAttribute);
					//echo ("assigning attribute '" . $assignsAttribute->GlobalIdentifier . "' the value from executable '" . $usesExecutableReturningAttribute->GlobalIdentifier . "'");

					$value = $context->getWorkData($usesExecutableReturningAttribute->GlobalIdentifier->__toString());
					if ($value instanceof InstanceReference)
					{
						//echo ("the value from work data is ##IS#'" . $value->GlobalIdentifier . "'##");
					}
					else
					{
						$oms->setAttributeValue($instForPRU, $assignsAttribute, $value);
					}
				}
				else
				{
					echo ("executable for PUMB is null");
				}
			}
		}
	}

?>