<?php

namespace Drupal\restaurant\Controller;

use Drupal\Core\Controller\ControllerBase;
//use Drupal\restaurant\Helper;
use Drupal\restaurant\View\View;
use Drupal\restaurant\Call\Call;
use Drupal\restaurant\Registry\Registry;
use Drupal\restaurant\App\Base;
//use Drupal\restaurant\Service\Calendar;
//use Drupal\restaurant\App\Misc\Misc;
use Drupal\restaurant\App\Misc;


abstract class AppController extends ControllerBase implements Base
{


  /** path to drupal installation
  * srting FILE
  */
  const FILE = __FILE__;

  /**
  *  Drupal database object
  */
  protected $db;

  /**
  *  path to the Html scripts folder
  */
  protected $htmlPath;


  /**
  * path to RestaurantHQ modules
  *
  */
  protected $modulePath;

  /**
  * view object, not to be confused with drupal views
  */
  protected $view;

  /**
  * service Object
  */
  protected $service;
  /**
  * Drupal database object
  */
  protected $conn;

  /**
  * setup a database connection and set the modulePath variable
  */
  public function __construct()
  {
    $this->service = new Misc();

    //$this->service = \Drupal::service('restaurant.misc');
    //$this->calendar = \Drupal::service('restaurant.calendar');
    //$misc = \Drupal::getContainer()->get(Misc::misc);
    //die ('Hello world');



    $paths =  explode("modules", dirname(self::FILE));

    $this->modulePath = $paths[0] . 'modules' . DIRECTORY_SEPARATOR . 'RestaurantHQ' . DIRECTORY_SEPARATOR;

    $this->view = new View();

    require_once $this->modulePath . 'restaurant' . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'Call' . DIRECTORY_SEPARATOR . 'Call.php'; //todo - get  this class to autoload

    $registry = Registry::getInstance();
    $registry->modulePath = $this->modulePath;

    $this->conn = \Drupal::database();

  }

  /**
  * call html Helpers
  * @param $func name of method passed into __call
  * @param $args arguments passed into __call
  */
  public function __call($func, $args)
  {
    $call = new Call();
    return $call->get($func, $args);
  }

  /**
  * set path to $htmlPath
  */
  protected function setPath(string $path)
  {
    $path = trim($path, DIRECTORY_SEPARATOR);

    if (strpos($path, 'src') !== false) { //src folder found in $path
      $this->htmlPath = $this->modulePath . $path . DIRECTORY_SEPARATOR;
    } else {
      $this->htmlPath = $this->modulePath;
      $parts = explode(DIRECTORY_SEPARATOR, $path);
      $this->htmlPath = $this->modulePath . $parts[0] . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . $parts[1] . DIRECTORY_SEPARATOR;
    }
  }

  final public function getDbConnection()
  {
    $this->db = new Db();
    return $this->db;
  }

  /**
  * shorthand for Drupal::routeMatch()->getParameter()
  */
  final public function getParam($name)
  {
    return $this->service->getParam($name);
  }

  /**
  * Check if parameter exits
  * @param $name name of the parameter to check
  */
  final public function hasParam($name)
  {
    return $this->service->hasParam($name);
  }

  /**
  * easier to use than the Drupal method
  */
  /*final public function redirect($path)
  {
    return $this->service->redirect($path);
  }*/

  /**
  * add a drupal error message to the page
  * @param $message message to display
  * @param $type message type, defaults to "message"
  */
  public function addMessage($message, $type = 'message')
  {
    return $this->service->addMessage($message, $type);
  }

  /**
  * return true if admin page is in the path
  */
  public function inAdminPath()
  {
    return $this->service->inAdminPath();
  }

  /**
  * set a variable to be passed to the html scripts
  */
  protected function set($key, $val)
  {
    $this->view->{$key} = $val;
  }

  /**
  * return output to Controller
  */
  protected function render($htmlscript, bool $ajax = false)
  {
   // mydump ($htmlscript);
    ob_start();
    require_once $this->htmlPath . $htmlscript;
    if ($ajax) {
      return new \Symfony\Component\HttpFoundation\Response(ob_get_clean());
      exit;
    }
    return array("#markup" => $this->t(ob_get_clean()));
  }

  /* Redirect to another url in the drupal sites
  */
  public function http_redirect($path)
  {
    $path = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['SERVER_NAME'] . DIRECTORY_SEPARATOR . $path;
    header("Location: $path");
    exit;
  }

  /** return Drupal session Object
  */
  public function getSession()
  {
    return $this->service->getSession();
  }

  public function setSetting($name, $value)
  {
    return $this->service->setSetting($name, $value);
  }

  public function getSetting($name)
  {
    return $this->service->getSetting($name);
  }

  public function image($image)
  {
    $path = ltrim(get_restaurant_path(false), '\\');
    $path = '/' . ltrim($path, '/');
    $path = str_replace('restaurant', '', $path);
    return '<img alt="" src="' . $path . $image . '">';
  }

  /*
   * Output html without themeing For use in ajax requests
   */
  protected function output($html)
  {
      print $this->t($html);
      exit;
  }
}
