<?php
// --------------------------------------------

include_once("trackers/resource_ref.php");

// ----------------------------------------------------
class ResourceUnlocker {
	protected $metagame = NULL;
	protected $friendly_id = 0;
	protected $unlock_list = array();

	// ----------------------------------------------------
	function __construct($metagame, $unlock_list) {
		$this->metagame = $metagame;
		$this->unlock_list = $unlock_list;
	}

	// ----------------------------------------------------
	function handle_item_delivery_completed($item) {
		_log("handle_item_delivery_completed");

		// link delivered item to a resource unlock:
		$unlock = $this->get_unlock($item);

		if ($unlock != NULL) {
			_log("* item delivery completed, picking unlock for " . $item->key . " (" . $item->type . "): " . $unlock->key . " (" . $unlock->type . ")");
			// enable a resource for the faction
			$c = 
				"<command class='faction_resources' faction_id='" . $this->friendly_id . "'>\n" .
				"	<" . $unlock->type . " key='" . $unlock->key . "' />\n" .
				"</command>";
			$this->metagame->comms->send($c);

			if ($unlock->name != "") {
				// comment about it?
				//$text = "objective completed, " . $this->item_name . " unlocked";

				$a = array("%resource_name" => $unlock->name); 
				$this->metagame->send_faction_message_key($this->friendly_id, "resource added in stock", $a);

				// unlocks are lost over time, but they must carry over map changes
				// announce to metagame that we've unlocked something now, it'll handle that part
				$this->metagame->item_unlocked($unlock);
			}
		} else {
			_log("* failed to find an unlock");

			// comment about it?
			//$text = "objective completed, " . $this->item_name . " unlocked";

			// TODO:
			// if all unlocks have been used:
			// - don't care for now?
		}
	}

	// ----------------------------------------------------
	function rule_out_owned_items(&$master_list, $type, $type_plural) {
		$own = $this->metagame->get_faction_deliverables($this->friendly_id, $type, $type_plural);
		if (!$own) return;

		// go through the list and match with master list, removing those already there, from the master list
		for ($i = 0; $i < $own->length; ++$i) {
			$node = $own->item($i);
			$key = $node->getAttribute("key");
			// check if we have this key
			$erase_index = -1;
			foreach ($master_list as $j => $item) {
				if ($key !== $item->key) {
					// no match, continue searching
				} else {
					// matches, rule it out
					$erase_index = $j;
					break;
				}
			}
			if ($erase_index >= 0) {
				//unset($master_list[$erase_index]);
				// unset just erases the item, we also have to remove the key as
				// we want to have a continuous index based array here
				array_splice($master_list, $erase_index, 1);
			}
		}
	}

	// ----------------------------------------------------
	function pick_reward_item($master_list) {
		// remove already owned items from input list

		$this->rule_out_owned_items($master_list, "weapon", "weapons");
		$this->rule_out_owned_items($master_list, "carry_item", "carry_items");
		//rule_out_owned_items($master_list, "grenade", "grenades");
		//rule_out_owned_items($master_list, "call", "calls");
		//rule_out_owned_items($master_list, "vehicle", "vehicles");

		// any item randomly from the list?

		if (count($master_list) > 0) {
			_log("possible reward items " . count($master_list));
			$item_index = rand(0, count($master_list) - 1);
			$item = $master_list[$item_index];

			return $item;
		}

		return NULL;
	}


	// ----------------------------------------------------
	function get_unlock($item) {
		_log("get_unlock");
		$result = NULL;
		if (array_key_exists($item->key, $this->unlock_list)) {
			// unlock list is an associative container of
			// "delivery target item key" -> "container potential reward resourcerefs"
			$result = $this->pick_reward_item($this->unlock_list[$item->key]);
		} else {
			_log("key " . $item->key . " doesn't exist in unlock_list");
		}
		return $result;
	}

}
?>