<?php
/**
 * Plugin NICC Hipervinculos: Menu visual de accesos rapidos
 *
 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author  SIE Asesorias
 */

if (!defined('DOKU_INC')) die();

class syntax_plugin_nicchipervinculos extends DokuWiki_Syntax_Plugin {

    public function getType() {
        return 'substition';
    }

    public function getSort() {
        return 155;
    }

    public function connectTo($mode) {
        $this->Lexer->addSpecialPattern('~~HIPERVINCULOS~~', $mode, 'plugin_nicchipervinculos');
        $this->Lexer->addSpecialPattern('\{\{hipervinculos\}\}', $mode, 'plugin_nicchipervinculos');
    }

    public function handle($match, $state, $pos, Doku_Handler $handler) {
        return array();
    }

    public function render($mode, Doku_Renderer $renderer, $data) {
        if ($mode !== 'xhtml') return false;

        // Ruta base de imagenes
        $imgBase = DOKU_BASE . 'lib/plugins/nicchipervinculos/images/';
        $imgPath = DOKU_INC . 'lib/plugins/nicchipervinculos/images/';

        // Definir los items del menu con iconos PNG y emojis fallback
        $items = array(
            array(
                'icon' => 'info_general.png',
                'emoji' => 'ℹ️',
                'text' => 'Informacion General',
                'link' => 'varios:generalidades',
                'full' => false
            ),
            array(
                'icon' => 'evaluacion.png',
                'emoji' => '✅',
                'text' => 'Evaluacion SGC',
                'link' => 'es:bez:admin_threads_filtro',
                'full' => false
            ),
            array(
                'icon' => 'documentos.png',
                'emoji' => '📚',
                'text' => 'Documentos Externos',
                'link' => 'varios:lexternos',
                'full' => false
            ),
            array(
                'icon' => 'tutoriales.png',
                'emoji' => '💻',
                'text' => 'Tutoriales y Capacitacion',
                'link' => 'varios:capacitacion',
                'full' => false
            ),
            array(
                'icon' => 'confidencial.png',
                'emoji' => '🔒',
                'text' => 'Acuerdo de Confidencialidad',
                'link' => 'varios:confidencialidad',
                'full' => true
            )
        );

        // Generar HTML
        $html = '<div class="nicc-menu-hipervinculos">';
        $html .= '<div class="menu-title">Hipervinculos</div>';
        $html .= '<div class="menu-grid">';

        foreach ($items as $item) {
            $url = wl($item['link']);
            $fullClass = $item['full'] ? ' full-width' : '';
            $iconFile = $imgPath . $item['icon'];
            $useImage = file_exists($iconFile);

            $html .= '<a href="' . $url . '" class="menu-item' . $fullClass . '">';
            $html .= '<div class="item-icon">';

            if ($useImage) {
                $iconUrl = $imgBase . $item['icon'];
                $html .= '<img src="' . $iconUrl . '" alt="' . hsc($item['text']) . '">';
            } else {
                $html .= '<span class="emoji-icon">' . $item['emoji'] . '</span>';
            }

            $html .= '</div>';
            $html .= '<span class="item-text">' . hsc($item['text']) . '</span>';
            $html .= '</a>';
        }

        $html .= '</div>'; // .menu-grid
        $html .= '</div>'; // .nicc-menu-hipervinculos

        $renderer->doc .= $html;
        return true;
    }
}
