<?php

class Specific
{

  private $language;
  private $load;
  private $request;
  private $db;
  private $registry;
  private $url;
  private $urlloja;

  public function __construct($registry)
  {
    $this->registry = $registry;
    $this->language = $registry->get('language');
    $this->load = $registry->get('load');
    $this->db = $registry->get('db');
    $this->request = $registry->get('request');
    $this->prefixname = defined('PREFIX_NAME');
    $this->url = $registry->get('url');
    $this->urlloja = $registry->get('urlloja');
  }

  public function __get($name)
  {
    return $this->registry->get($name);
  }

  public function loadFiles(&$module, &$payment, &$shipping, &$total)
  {
    if ($this->prefixname) {
      $module = array_merge($module, glob(DIR_APPLICATION . 'controller/specific/' . PREFIX_NAME . '/module/*.php'));
      $payment = array_merge($payment, glob(DIR_APPLICATION . 'controller/specific/' . PREFIX_NAME . '/payment/*.php'));
      $shipping = array_merge($shipping, glob(DIR_APPLICATION . 'controller/specific/' . PREFIX_NAME . '/shipping/*.php'));
      $total = array_merge($total, glob(DIR_APPLICATION . 'controller/specific/' . PREFIX_NAME . '/total/*.php'));
    }
  }

  public function getIgnoreFiles()
  {
    if ($this->prefixname) {
      return array(
        'material' => false
      );
    } else {
      return array();
    }
  }


  public function getPathController($type, $extension)
  {

    if ($this->prefixname && file_exists(DIR_APPLICATION . 'controller/specific/' . PREFIX_NAME . '/' . $type . '/' . $extension . '.php')) {
      require_once(DIR_APPLICATION . 'controller/specific/' . PREFIX_NAME . '/' . $type . '/' . $extension . '.php');
      return 'ControllerSpecific' . PREFIX_NAME . ucfirst($type) . str_replace('_', '', $extension);
    } else {
      require_once(DIR_APPLICATION . 'controller/' . $type . '/' . $extension . '.php');
      return 'Controller' . ucfirst($type) . str_replace('_', '', $extension);
    }

  }

  public function getLink($route, $token, $tipo)
  {

    if ($this->prefixname && file_exists(DIR_APPLICATION . 'controller/specific/' . PREFIX_NAME . '/' . $route . '.php')) {
      return $this->url->link('specific/' . PREFIX_NAME . '/' . $route, 'token=' . $token . '&type=' . $tipo, 'SSL');
    } else {
      return $this->url->link($route, 'token=' . $token . '&type=' . $tipo, 'SSL');
    }

  }

}
