<?php

	namespace Mocha\Core;

	class OmsContext
	{
		private $elementParms;
		public $workData;

		public $CallStack;

		public function __construct()
		{
			$this->elementParms = array();
			$this->workData = array();
			$this->CallStack = array();
		}

		public function setElementParm(InstanceReference $elementContent, string $parmId, $value)
		{
			if (!array_key_exists($elementContent->DatabaseId, $this->elementParms))
			{
				$this->elementParms[$elementContent->DatabaseId] = array();
			}
			$this->elementParms[$elementContent->DatabaseId][$parmId] = $value;
		}

		public function getElementParm(InstanceReference $elementContent, string $parmId, $defaultValue = null)
		{
			if (array_key_exists($elementContent->DatabaseId, $this->elementParms))
			{
				if (array_key_exists($parmId, $this->elementParms[$elementContent->DatabaseId]))
				{
					return $this->elementParms[$elementContent->DatabaseId][$parmId];
				}
			}
			return $defaultValue;
		}
		public function setWorkData(string|InstanceReference $parmId, $value)
		{
			/**
			 * @var \Mocha\Oms\MySQLDatabaseOms
			 */
			$oms = mocha_get_oms();

			$key = $parmId;
			if ($key instanceof InstanceReference)
			{
				$key = $key->GlobalIdentifier->__toString();
				if ($value === null)
				{
					//echo ("[ set wd " . $parmId->InstanceKey->__toString() . " =  null  ]");
				}
				else
				{
					if (is_string($value))
					{
						//echo ("[ set wd " . $parmId->InstanceKey . " = STR'" . $value . "' ]");
					}
					else
					{
						//echo ("[ set wd " . $parmId->InstanceKey . " = IK'" . $value->InstanceKey . "' ]");
					}
				}

				$expectedDataType = "";
				$actualDataType = "";
				if (!$oms->validateWorkDataType($parmId, $value, $expectedDataType, $actualDataType))
				{
					//echo("invalid work data (expected: " . $expectedDataType . ", got: " . $actualDataType . ") for attribute " . $parmId->InstanceKey . " @ " . $parmId->GlobalIdentifier);
				}
			}
			$this->workData[$key] = $value;
		}
		public function getWorkData(string|InstanceReference $parmId, $defaultValue = null)
		{
			$key = $parmId;
			if ($key instanceof InstanceReference)
			{
				$key = $key->GlobalIdentifier->__toString();
			}
			if (array_key_exists($key, $this->workData))
			{
				return $this->workData[$key];
			}
			return $defaultValue;
		}

	}

?>