<?php
	namespace Mocha\Core;

	use Mocha\Core\Oms;

	use Mocha\Oop\Method;
	use Mocha\Oop\MethodBinding;

	use Phast\UUID;

	class InstanceReference
	{
		//public Oms $OMS;
		public ?int $DatabaseId;
		public InstanceKey|null $InstanceKey;
		public UUID|null $GlobalIdentifier;

		public static function toInstanceKeys(array $instanceReferences) : array
		{
			$keys = [ ];
			foreach ($instanceReferences as $instanceReference)
			{
				$keys[] = $instanceReference->InstanceKey;
			}
			return $keys;
		}

		public function __construct(?int $databaseId, InstanceKey|null $instanceKey, UUID|string|null $globalIdentifier)
		{
			//$this->OMS = $oms;
			$this->DatabaseId = $databaseId;
			$this->InstanceKey = $instanceKey;

			if (is_string($globalIdentifier))
			{
				$globalIdentifier = UUID::parse($globalIdentifier);
			}
			$this->GlobalIdentifier = $globalIdentifier;
		}

		/*
		public function getRelatedInstances(InstanceReference $relationship)
		{
			$instances = [];
			return $instances;
		}
		public function getAttributeValue($attributeInstance, $defaultValue = null, $effectiveDate = null) : ?string
		{
			return $this->OMS->getAttributeValue($this, $attributeInstance, $defaultValue, $effectiveDate);
		}

		*/

		public function asMethod()
		{
			return new Method($this);
		}
		public function asMethodBinding($oms)
		{
			return new MethodBinding($oms, $this);
		}

		public function __is_equal(InstanceReference $other)
		{
			return $this->GlobalIdentifier->__is_equal($other->GlobalIdentifier);
		}
		public function __is_not_equal(InstanceReference $other)
		{
			return !$this->__is_equal($other);
		}

		public function __toString()
		{
			if ($this === null)
			{
				// can we do this?
				return "(null)";
			}
			return $this->GlobalIdentifier->__toString() . " [" . $this->InstanceKey . "]";
		}
	}
?>