<?php

namespace App\Core\Template;

class Output {

    private static $_instance;

    private $view = false;
    protected $values = Array();

    private $_template = '';
    private $_extension = '';
    
    private function __construct() {
        $this->_template = ( defined('TEMPLATE_DEFAULT') ) ? TEMPLATE_DEFAULT : 'DefaultTemplate';
        $this->_extension = ( defined('TEMPLATE_FILETYPE') ) ? TEMPLATE_FILETYPE : '.mustache';
    }

    private static function newObj() {
        if (!isset(self::$_instance)) {
            self::$_instance = new Output();
        }
        return self::$_instance;
    }

    public function getInstance() {
        if (!isset(self::$_instance)) {
            return self::newObj();
        }
        return self::$_instance;
    }

    public function setTemplate($template = 'DefaultTemplate') {
        self::getInstance()->_template = $template;
//        self::getInstance()->_template = '';
        return self::getInstance();
    }

    public static function renderView($name, $values = null, $location = null) {
        if (!$location) {
            $segments = explode('/', debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)[0]['file']);
            array_pop($segments);
            $location = implode('/', $segments) . '/views/';
        }
        
        self::getInstance()->setView($name, $values, $location)->render();
    }

    public static function setView($name = '', $values = Array(), $location = null) {
        if($name == ''){ return self::getInstance(); }
        $instance =  self::getInstance();
        $folder = '';
        if (!$location) {
            $segments = explode('/', debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)[0]['file']);
            array_pop($segments);
            $location = implode('/', $segments) . '/views/';
        }
        $folder = $location;
        $location .= $name;

        if (strpos($instance->_extension, $location) == 0) {
            $location .= $instance->_extension;
        }

        $instance->view = Array();
        $instance->view['location'] = $location;
        $instance->view['folder'] = $folder;
        $instance->view['values'] = $values;
        return self::getInstance();
    }

    public static function addValue($key, $value){
        self::getInstance()->view['values'][$key] = $value;
    }

    public static function render() {
        $instance = self::getInstance();

        if(!is_file(__DIR__."/classes/".strtolower($instance->_template)."/".$instance->_template.'.php') ){
            $instance->_template = 'DefaultTemplate';
        }

        include_once __DIR__."/classes/".strtolower($instance->_template)."/".$instance->_template.'.php';
        
        $class = new $instance->_template;
        $class->setView($instance->view)->render();

    }

    public static function addMenu($route, $name, $icon = '', $attributes = [], $weight = 0){
        global $SIDEBAR;
        $SIDEBAR->add($route, $name, $icon, $attributes, $weight);
    }

    public static function addSubmenu($key, $name, $icon = '', $attributes = [], $weight = 0){
        global $SIDEBAR;
        $SIDEBAR->create_submenu($key, $name, $icon, $attributes , $weight);        
    }
    
    public static function addOnSubmenu($key, $route, $name, $icon = '', $attributes = [], $weight = 0){
        global $SIDEBAR;
        $SIDEBAR->add_on_new($key, $route, $name, $icon, $attributes, $weight);
    }
    
    //========
    
    public static function headAdd($route, $name){
        global $DROPDOWN;
        $DROPDOWN[] = ['route' => $route, 'name' => $name];
    }
    
    public static function addSubHeader($key, $name, $icon = '', $attributes = [], $weight = 0){
        global $DROPDOWN;
        $DROPDOWN->create_submenu($key, $name, $icon, $attributes , $weight);        
    }
    
    public static function addOnSubHeader($key, $route, $name, $icon = '', $attributes = [], $weight = 0){
        global $DROPDOWN;
        $DROPDOWN->add_on_new($key, $route, $name, $icon, $attributes, $weight);
    }
    
    
    public static function icon($icon){
        return "<i class='$icon' aria-hidden='true'></i>";
    }

}
