<?php

class Output {

    private static $_instance;
    private $_doctype = "<!DOCTYPE html>";
    private $_head_items = Array();
    private $_body = "";
    private $_content = "";
    private $_foot_items = Array();
    private $view = false;

    //SINGLETON==============================================
    private function __construct() {
        
    }

    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 static function load_view($name, $values = null, $location = null) {
        if (!$location) {
            $segments = explode('/', debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)[0]['file']);
            array_pop($segments);
            $location = implode('/', $segments) . '/views/';
        }

        if (is_array($values) && count($values) > 0) {
            extract($values, EXTR_PREFIX_SAME, 'data');
        }

        $location .= $name;

        if (strpos('.php', $location) == 0) {
            $location .= '.php';
        }

        if (!file_exists($location)) {
            //return require_once GENERAL_VIEW_FOLDER . 'errors/404.php';
        }
        return require_once( $location );
    }

    private function render_view() {
        if (is_array($this->view['values']) && count($this->view['values']) > 0) {
            extract($this->view['values'], EXTR_PREFIX_SAME, 'data');
        }
        return require_once( $this->view['location'] );
    }

    public function set_view($name, $values = null, $location = null) {
        if (!$location) {
            $segments = explode('/', debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)[0]['file']);
            array_pop($segments);
            $location = implode('/', $segments) . '/views/';
        }

        $location .= $name;

        if (strpos('.php', $location) == 0) {
            $location .= '.php';
        }

        $this->view = Array();
        $this->view['location'] = $location;
        $this->view['values'] = $values;
    }

    public function render() {
        if (is_ajax_request()) {
            echo $this->_content;
            if ($this->view) {
                $this->render_view();
            }
            return;
        }
        global $DROPDOWN, $SIDEBAR;

        $this->_head_items[] = '<script src="/plugins/jquery/jquery.min.js"></script>';

        $this->_head_items[] = '<script src="/plugins/mousetrap/mousetrap.min.js"></script>';

        $this->_head_items[] = '<script src="/plugins/jquery-mask-plugin/jquery.mask.min.js"></script>';

        $this->_head_items[] = '<link href="/plugins/bootstrap/bootstrap.min.css" rel="stylesheet" />';
        $this->_head_items[] = '<script src="/plugins/bootstrap/bootstrap.min.js"></script>';

        $this->_head_items[] = '<link href="/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet" />';

        $this->_head_items[] = '<link href="/plugins/normalize-css/normalize.css" rel="stylesheet">';
        $this->_head_items[] = '<link href="/dist/style.css" rel="stylesheet">';

        $this->_head_items[] = '<link href="/dist/theme/black_and_white.css" rel="stylesheet">';

        $this->_head_items[] = '<link href="/plugins/metisMenu/metisMenu.min.css" rel="stylesheet">';
        $this->_head_items[] = '<script src="/plugins/metisMenu/metisMenu.min.js"></script>';

        $this->_head_items[] = '<link href="/plugins/select2/select2.min.css" rel="stylesheet">';
        $this->_head_items[] = '<script src="/plugins/select2/select2.min.js"></script>';

        $this->_head_items[] = '<link rel="stylesheet" href="/plugins/datatables/dataTables.css">';
        $this->_head_items[] = '<script src="/plugins/datatables/dataTables.js"></script>';

        $this->_foot_items[] = '<script src="/dist/script.js"></script>';

        $head = "<head>";
        foreach ($this->_head_items as $head_item) {
            $head .= $head_item . "\n";
        }
        $head .= "</head>";
        $foot = "";
        foreach ($this->_foot_items as $foot_item) {
            $foot .= $foot_item;
        }

        $header = str_replace("{{usermenu}}", "", file_get_contents(__DIR__ . '/views/header.html'));
        $sidebar = str_replace("{{elements}}", $SIDEBAR->render(), file_get_contents(__DIR__ . '/views/sidebar.html'));

        $body_first = file_get_contents(__DIR__ . "/views/body.html");
        $body_first = str_replace("{{header}}", $header, $body_first);
        $body_first = str_replace("{{sidebar}}", $sidebar, $body_first);
        $body_first .= $this->_content;

        $foot .= '<div id="modal" class="modal fade" role="dialog">  </div>';

        $body_end = file_get_contents(__DIR__ . "/views/body_section.html");
        $body_end = str_replace("{{footer}}", $foot, $body_end);

        echo $this->_doctype . "\n";
        echo "<html> \n";
        echo $head;
        echo $body_first;
        if ($this->view) {
            $this->render_view();
        }
        echo $body_end;
        echo "</html>";
    }

    public function set_content($content) {
        $this->_content = $content;
    }

}
