<?php

include_once("internal/tracker.php");
include_once("internal/log.php");
include_once("trackers/resource_ref.php");

// --------------------------------------------

class ResourceTimer {
	protected $timer = 0.0;
	protected $resource_ref = NULL;

	// --------------------------------------------
	function __construct($t, $resource_ref) {
		$this->timer = $t;
		$this->resource_ref = $resource_ref;
	}

	// --------------------------------------------
	function update($time) {
		$this->timer -= $time;
	}

	// --------------------------------------------
	function has_expired() {
		return $this->timer < 0.0;
	}

	// --------------------------------------------
	function get_resource() {
		return $this->resource_ref;
	}

	// --------------------------------------------
	function get_time() {
		return $this->timer;
	}
};

// --------------------------------------------

class UnlockManager extends Tracker {
	protected $metagame = NULL;
	protected $unlocks = array();
	protected $friendly_id = 0;

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

	// --------------------------------------------
	function init() {
	}

	// --------------------------------------------
	function start() {
	}

	// --------------------------------------------
	public function update($time) {
		// update all timers
		foreach ($this->unlocks as $key => $unlock) {
			$unlock->update($time);
			if ($unlock->has_expired()) {
				// time to drop to unlock
				$r = $unlock->get_resource(); 
				if ($r) {
					$this->drop_resource($r);
					unset($this->unlocks[$key]);
					_log("unlocks: " . count($this->unlocks));
				}
			}
		}
	}

    // ----------------------------------------------------
	// debugging tools
    protected function handle_chat_event($doc) {
		// player_id
		// player_name
		// message
		// global
		$event = $doc->firstChild;

		$message = $event->getAttribute("message");
		// for the most part, chat events aren't commands, so check that first
		if (!starts_with($message, "/")) {
				return;
		}

		$sender = $event->getAttribute("player_name");
		$sender_id = $event->getAttribute("player_id");

		if (!is_admin($sender)) {
			return;
		}

        if (check_command($message, "drop_unlock")) {
            $this->metagame->comms->send("say dropping unlock");
			if (count($this->unlocks) > 0) {
				$this->unlocks[0]->update(60.0 * 60.0);
			}
		}
	}

	// --------------------------------------------
	function has_ended() {
		// always on
		return false;
	}

	// --------------------------------------------
	function has_started() {
		// always on
		return true;
	}

	// --------------------------------------------
	function add($resource) {
		if (!$resource) return;

		$t = 60.0 * 60.0;

		$this->unlocks[] = new ResourceTimer($t, $resource);

		_log("unlocks: " . count($this->unlocks));
	}

	// --------------------------------------------
	function drop_resource($resource) {
		if (!$resource) return;

		_log("* resource unlock expired, removing unlock for " . $resource->key . " (" . $resource->type . ")");

		// disable the resource
		$c = 
			"<command class='faction_resources' faction_id='" . $this->friendly_id . "'>\n" .
			"	<" . $resource->type . " key='" . $resource->key . "' enabled='0' />\n" .
			"</command>";
		$this->metagame->comms->send($c);

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

			$a = array("%resource_name" => $resource->name); 
			$this->metagame->send_faction_message_key($this->friendly_id, "resource out of stock", $a);
		}		
	}

	// --------------------------------------------
	public function apply_unlocks() {
		$command =
			"<command class='faction_resources' faction_id='" . $this->friendly_id . "'>\n";
		// enable all unlocked resources for the faction
		foreach ($this->unlocks as $key => $unlock) {
			$c = "	<" . $unlock->get_resource()->type . " key='" . $unlock->get_resource()->key . "' />\n";
			$command .= $c;
		}
		$command .= 
			"</command>";

		$this->metagame->comms->send($command);
	}

	// --------------------------------------------
	public function save(&$doc, &$root) {
		$parent = $root;
		$subroot = $doc->createElement("unlock_manager");
		foreach ($this->unlocks as $key => $unlock) {
			$e = $doc->createElement("resource");
			$e->setAttribute("time", $unlock->get_time());
			$r = $unlock->get_resource();
			$e->setAttribute("key", $r->key);
			$e->setAttribute("type", $r->type);
			$e->setAttribute("name", $r->name);
			$subroot->appendChild($e);
		}
		$parent->appendChild($subroot);
	}

	// --------------------------------------------
	public function load(&$doc, &$root) {
		_log("loading unlock manager");
		$this->unlocks = array();

		$subroot = $root->getElementsByTagName("unlock_manager")->item(0);
		if ($subroot) {
			_log("unlock_manager found");
			$list = $subroot->getElementsByTagName("resource");
			for ($i = 0; $i < $list->length; $i++) {
				$e = $list->item($i);
				$time = $e->getAttribute("time");
				$key = $e->getAttribute("key");
				$type = $e->getAttribute("type");
				$name = $e->getAttribute("name");
				$r = new ResourceRef($key, $type, $name);

				_log("adding unlock: " . $time . ", key=" . $key . ", type=" . $type . ", name=" . $name);
				$this->unlocks[] = new ResourceTimer($time, $r);
			}
		} else {
			_log("map_rotator not found?");
		}

	}

}

?>