<?php

class Product extends Model {

	protected $_classes = [
		"parent" => "Product"
	];

	protected $_computed = [
		"documents" => "get_documents",
		"children" => "get_children",
	];

	protected $_triggers = [
		"parent" => "update_path",
		"title" => "update_path",
	];

	private $_children = null;
	public function get_children() {
		$c = $this->cache_get("children");
		if ($c) return $c;

		if ($this->_children == null) {
			$this->_children = Product::find([["parent", "=", $this->id]])->orderBy("title")->all();
		}
		$this->cache_set("children", $this->_children);
		return $this->_children;
	}

	public function get_full_title() {
		$tree = $this->get_tree();

		$n = [];
		foreach ($tree as $t) {
			$n[] = $t->title;
		}

		$out = implode(" / ", $n);
		return $out;
	}

	public function update_path($ppath = null) {
		if ($ppath == null) {
			$this->full_path = $this->get_full_title();
		} else {
			$this->full_path = $ppath . " / " . $this->title;
		}
		$this->full_path = str_replace("/ / ", "/ ", $this->full_path);
		$this->save();
		foreach ($this->get_children() as $child) {
			$child->update_path($this->fill_path);
		}
	}

	public function get_tree() {
		$out = [];
		if ($this->load("parent")) {
			$p = $this->parent;
			$out = $p->get_tree();
		}
		array_push($out, $this);

		return $out;
	}

	public function overview_md() {
		return \Michelf\Markdown::defaultTransform($this->overview);
	}

    public function on_delete() {
        DB::getInstance()->query("delete from docproduct where product=:id", ["id" => $this->id]);
    }

	private $_documents = null;

	public function get_documents() {

		$d = $this->cache_get("documents");
		if ($d) return $d;

		if ($this->_documents == null) {
			//$dpl = DocProduct::find([["product", "=", $this->id]])->limit(100)->all();
			$dpl = DocProduct::find([["product", "=", $this->id]])->all();
			$this->_documents = new Collection;
			foreach ($dpl as $dp) {
				if ($dp->load("document")) {
					$this->_documents->push($dp->document);
				}
			}

			$this->_documents->sort("subsubtitle", true);
			$this->_documents->sort("subtitle", true);
			$this->_documents->sort("title", true);
		}
		$this->cache_set("documents", $this->_documents);
		return $this->_documents;
	}

	public function add_document($doc) {
		$dp = new DocProduct;
		$dp->product = $this->id;
		$dp->document = $doc->id;
		$dp->save();
	}

	public function meta() {
		if ($this->metadata == null) {
			return [];
		}
		return explode(",", $this->metadata);
	}

	public function add_meta($id, $save = true) {
		$m = $this->meta();
		if (!in_array($id, $m)) {
			$m[] = $id;
		}
		$this->metadata = implode(",", $m);
		if ($save) $this->save();
	}

	public function del_meta($id, $save = true) {
		$m = $this->meta();
		$o = [];
		foreach ($m as $v) {
			if ($v != $id) {
				$o[] = $v;
			}
		}
		$this->metadaya = implode(",", $o);
		if ($save) $this->save();
	}

	public function documents_sorted_by_meta() {

		if ($this->metadata == null) {
			$meta = [];
		} else {
			$meta = explode(",", $this->metadata);
		}

		$docs = $this->get_documents();


		while (count($meta) > 0) {
			$mid = array_pop($meta);
			$docs->sort_with_function( function($a, $b) use ($mid) {
				$va = $a->get_metadata_by_id($mid);
				$vb = $b->get_metadata_by_id($mid);

				if ($va > $vb) return 1;
				if ($va < $vb) return -1;
				return 0;
				});
		}


		return $docs;

	}

}
