<?php

/*
 * *************************************************
 * Copyright (C) 2020 Earworks.                    *
 * Codeigniter Project Template Modification       *
 * Please contact our email for more information : *
 * contact@earworks.co.id                          *
 * *************************************************
 */

/**
 *
 * @author andhy
 */
trait Ci_view_trait {

    protected static $container = 'layouts';
    protected static $view_container = 'main';
    public static $title = '';
    protected static $back_url = '';
    protected static $meta = [];
    protected static $css_plugin = [];
    protected static $css = [];
    protected static $js = [];
    protected static $js_plugin = [];
    protected static $maps = [];

    public static function view(string $method, array $params = []) {
        $CI = ancillary_object();
        $params['ver'] = config_item('asset_version');
        $content = self::partials($method, $params);
        $params['title'] = isset($params['title']) ? $params['title'] : self::$title;
        if (!isset($params['location'])) {
            $params['location'] = 'general';
        }
        $CI->load->view(self::$container . '/' . self::$view_container, array_merge($params, array(
            'render_css' => "\n" . implode("\n", self::$css) . "\n",
            'render_css_plugin' => "\n" . implode("\n", self::$css_plugin) . "\n",
            'render_js' => "\n" . implode("\n", self::$js) . "\n",
            'render_js_plugin' => "\n" . implode("\n", self::$js_plugin) . "\n",
            'render_meta' => "\n" . implode("\n", self::$meta) . "\n",
            'render_maps' => self::$maps,
            'back_url' => self::$back_url,
            '_session' => $CI->session->userdata(getenv('TOKEN_SESSION')),
            'content' => $content
        )));
    }

    public static function partials(string $method, array $params = []) {
        $CI = ancillary_object();
        $string_view = $CI->load->view(target_view($method), $params, true);
        return $string_view;
    }

    public static function set_title(string $title) {
        self::$title = $title;
    }

    public static function set_layout(string $layout_folder) {
        self::$container = $layout_folder;
    }

    public static function set_layout_file(string $layout_file) {
        self::$view_container = $layout_file;
    }

    public static function set_back_url(string $url) {
        self::$back_url = $url;
    }

    public static function set_script($script, $type = 'import', $defer = false) {
        $success = true;
        $js = null;
        $js_plugin = null;

        switch ($type) {
            case 'import':
                if (strpos($script, 'http') === false) {
                    $filepath = base_url() . $script;
                } else {
                    $filepath = $script;
                }
                $js = "\t\t<script type=\"text/javascript\" src=\"" . $filepath . "\"";
                if ($defer) {
                    $js .= ' defer="defer"';
                }
                $js .= "></script>";
                break;

            case 'embed':
                $js = "\t\t<script type=\"text/javascript\"";
                if ($defer) {
                    $js .= ' defer="defer"';
                }
                $js .= ">\r\n";
                $js .= $script;
                $js .= "\r\n\t\t</script>";
                break;
            case 'plugin':
                if (strpos($script, 'http') === false) {
                    $filepath = base_url() . $script;
                } else {
                    $filepath = $script;
                }
                $js_plugin = "\t\t<script type=\"text/javascript\" src=\"" . $filepath . "\"";
                if ($defer) {
                    $js_plugin .= ' defer="defer"';
                }
                $js_plugin .= "></script>";
                break;
            case 'plugin_embed' :
                $js_plugin = "\t\t<script type=\"text/javascript\"";
                if ($defer) {
                    $js_plugin .= ' defer="defer"';
                }
                $js_plugin .= ">\r\n";
                $js_plugin .= $script;
                $js_plugin .= "\r\n\t\t</script>";
                break;
            default:
                $success = FALSE;
                break;
        }

        // Add to js array if it doesn't already exist
        if ($js != NULL && !in_array($js, self::$js)) {
            self::$js[] = $js;
        }
        if ($js_plugin != null && !in_array($js_plugin, self::$js_plugin)) {
            self::$js_plugin[] = $js_plugin;
        }

        return $success;
    }

    public static function set_style($style, $type = 'link', $media = false) {
        $success = true;
        $css = null;
        $css_plugin = null;
        if (strpos($style, 'http') === false) {
            $filepath = base_url() . $style;
        } else {
            $filepath = $style;
        }


        switch ($type) {
            case 'link':
                $css = "\t\t<link type=\"text/css\" rel=\"stylesheet\" href=\"" . $filepath . "\"";
                if ($media) {
                    $css .= ' media="' . $media . '"';
                }
                $css .= ' />';
                break;

            case 'import':
                $css = "\t\t<style type=\"text/css\">@import url(" . $filepath . ");</style>";
                break;

            case 'embed':
                $css = "\t\t<style type=\"text/css\">\r\n";
                $css .= $style;
                $css .= "\r\n\t\t</style>";
                break;
            case 'plugin':
                $css_plugin = "\t\t<link type=\"text/css\" rel=\"stylesheet\" href=\"" . $filepath . "\"";
                if ($media) {
                    $css_plugin .= ' media="' . $media . '"';
                }
                $css_plugin .= ' />';
                break;
            case 'plugin_embed' :
                $css_plugin = "\t\t<style type=\"text/css\">\r\n";
                $css_plugin .= $style;
                $css_plugin .= "\r\n\t\t</style>";
                break;
            default:
                $success = false;
                break;
        }

        // Add to js array if it doesn't already exist
        if ($css != null && !in_array($css, self::$css)) {
            self::$css[] = $css;
        }
        if ($css_plugin != null && !in_array($css_plugin, self::$css_plugin)) {
            self::$css_plugin[] = $css_plugin;
        }
        return $success;
    }

    public static function set_metatag($key, $val) {
        $success = FALSE;
        $meta = "\t\t<meta name=\"" . $key . '" content="' . $val . '" />';

        if (!in_array($meta, self::$meta)) {
            self::$meta[] = $meta;
            $success = TRUE;
        }

        return $success;
    }

    public static function set_reload($time = 5) {
        $success = FALSE;
        $meta = "\t\t<meta http-equiv=\"refresh\" content=\"{$time}\">";

        if (!in_array($meta, self::$meta)) {
            self::$meta[] = $meta;
            $success = TRUE;
        }

        return $success;
    }

    /**
     * Dynamically include breadcrumbs map in the template
     *
     * @param string $title title to be display
     * @param string $url url to previews page
     * @return bool
     */
    public static function set_map($title, $url = null) {
        static::$maps[] = array(
            'title' => $title,
            'url' => $url
        );
        return true;
    }

}
