<?php
class Document {
	private $title;
	private $description;
	private $keywords;
	private $links       = array();
	private $propertys   = array();
	private $styles      = array();
    private $scripts     = array();
    private $styles_app  = array();
	private $scripts_app = array();
	private $types = array();
	private $registry;
	private $ftrgeralregistry;
	private $load;

  public function __construct($registry) {
    $this->registry 				= $registry;
		$this->ftrgeralregistry = $registry->get('ftrgeralregistry');
		$this->load 						= $registry->get('load');

		$this->load->helper('ftr_geral');
	}

	public function setTitle($title) {
		$this->title = strip_tags($title);
	}

	public function getTitle() {
		return $this->title;
	}

	public function setDescription($description) {
		$this->description = $description;
	}

	public function getDescription() {
		return $this->description;
	}

	public function setKeywords($keywords) {
		$this->keywords = $keywords;
	}

	public function getKeywords() {
		return $this->keywords;
	}

	public function addLink($href, $rel) {
		$this->links[md5($href)] = array(
			'href' => $href,
			'rel'  => $rel
		);
	}

	public function getLinks() {
		return $this->links;
	}

	public function addProperty($property, $content){
		$this->propertys[md5($property)] = array(
			'property' => $property,
			'content'  => $content
		);
	}

	public function getPropertys(){
		return $this->propertys;
	}

  public function addStylePrioridade($href, $rel = 'stylesheet', $media = 'screen', $cache=true){

		$href = $this->ftrgeralregistry->getScriptManifest($href, $cache);

		if(!in_array($href, array_column($this->styles, 'href'))){
			array_unshift($this->styles, array(
				'href'  => $href,
				'rel'   => $rel,
				'media' => $media
			));
		}

	}

  public function addStylePrioridadeApp($href, $rel = 'stylesheet', $media = 'screen', $cache=true){
		$versionedLink = $this->ftrgeralregistry->getScriptManifest($href, $cache);
    if (!in_array($versionedLink, $this->styles_app)) {
      array_unshift($this->styles_app, $versionedLink);
    }
	}

	public function addStyle($href, $rel = 'stylesheet', $media = 'screen', $cache=true) {
		$this->styles[md5($href)] = array(
			'href'  => $this->ftrgeralregistry->getScriptManifest($href, $cache),
			'rel'   => $rel,
			'media' => $media
		);
	}

  public function addStyleApp($arrayLinkFiles = null) {
    if ($arrayLinkFiles) {
      foreach ($arrayLinkFiles as $linkFile) {
        $versionedLink = $this->ftrgeralregistry->getScriptManifest($linkFile, false);
        if (!in_array($versionedLink, $this->styles_app)) {
          $this->styles_app[] = $versionedLink;
        }
      }
    }
  }

  public function getStyles() {
		return $this->styles;
	}

  public function getStylesApp() {
		return $this->styles_app;
	}

  public function addScriptPrioridade($script, $cache=true){

		$script = $this->ftrgeralregistry->getScriptManifest($script, $cache);

		if(!ftr_geral::in_array_r($script, $this->scripts)){
			array_unshift($this->scripts, $script);
		}

	}

  public function addScriptPrioridadeApp($script, $cache=true){
    $versionedLink = $this->ftrgeralregistry->getScriptManifest($script, $cache);
    if (!in_array($versionedLink, $this->scripts_app)) {
      array_unshift($this->scripts_app, $versionedLink);
    }
	}

	public function addScript($script, $cache=true) {
    if (!preg_match("/\/admin\/$/", DIR_APPLICATION) && $this->ftrgeralregistry->isApp()) {
      return false;
    }

		if(is_array($script)){
			foreach($script as $key => $value){
				$this->scripts[md5($value)] = $this->ftrgeralregistry->getScriptManifest($value, $cache);
			}
		}else{
			$this->scripts[md5($script)] = $this->ftrgeralregistry->getScriptManifest($script, $cache);
		}
	}

  public function addScriptApp($arrayLinkFiles = null) {
    if ($arrayLinkFiles) {
      foreach ($arrayLinkFiles as $linkFile) {
        $versionedLink = $this->ftrgeralregistry->getScriptManifest($linkFile, false);
        if (!in_array($versionedLink, $this->scripts_app)) {
          $this->scripts_app[] = $versionedLink;
        }
      }
    }
  }

  public function getScripts() {
		return $this->scripts;
	}

  public function getScript($pathScript) {
    return $this->scripts[md5($pathScript)];
  }

  public function getScriptsApp() {
		return $this->scripts_app;
  }

  public function addTypesForJs($type) {
		if(is_array($type) && isset($type[0])){
			foreach($type as $key => $value){
				$this->types[] = $value;
			}
		}else{
			$this->types[] = $type;
		}
  }

  public function getTypes() {
		return $this->types;
  }

}
