<?php
/**
 * Plugin NICC Menu Proyectos: Menu circular de navegacion
 *
 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author  SIE Asesorias
 */

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

class syntax_plugin_niccproyectos extends DokuWiki_Syntax_Plugin {

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

    public function getSort() {
        return 155;
    }

    public function connectTo($mode) {
        $this->Lexer->addSpecialPattern('~~MENUPROYECTOS~~', $mode, 'plugin_niccproyectos');
        $this->Lexer->addSpecialPattern('\{\{menuproyectos\}\}', $mode, 'plugin_niccproyectos');
    }

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

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

        $imgBase = DOKU_BASE . 'lib/plugins/niccproyectos/images/';
        $imgPath = DOKU_INC . 'lib/plugins/niccproyectos/images/';

        $circularItems = array(
            array('num' => '1', 'icon' => '01_clientes.png', 'emoji' => '📁', 'text' => 'Control de Clientes', 'link' => 'es:bez:threads'),
            array('num' => '2', 'icon' => '02_tareas.png', 'emoji' => '📅', 'text' => 'Control Tareas', 'link' => 'es:bez:tasks'),
            array('num' => '3', 'icon' => '03_propios.png', 'emoji' => '✅', 'text' => 'Trabajos Propios', 'link' => 'es:bez:projects'),
            array('num' => '4', 'icon' => '04_planeacion.png', 'emoji' => '📈', 'text' => 'Planeacion Proyectos', 'link' => 'es:bez:thread_report:type:issue'),
            array('num' => '5', 'icon' => '05_admtvo.png', 'emoji' => '📝', 'text' => 'Planeacion Admtvo', 'link' => 'es:bez:thread_report:type:project'),
            array('num' => '6', 'icon' => '06_independiente.png', 'emoji' => '✏️', 'text' => 'Tareas Independiente', 'link' => 'es:bez:task_form'),
            array('num' => '7', 'icon' => '07_informe.png', 'emoji' => '📢', 'text' => 'Informe General', 'link' => 'es:bez:report'),
            array('num' => '8', 'icon' => '08_individual.png', 'emoji' => '📄', 'text' => 'Informe Individual', 'link' => 'es:bez:activity_report'),
            array('num' => '9', 'icon' => '09_resumen.png', 'emoji' => '📰', 'text' => 'Resumen Diario', 'link' => 'es:bez:start'),
            array('num' => '10', 'icon' => '10_calendario.png', 'emoji' => '🗓️', 'text' => 'Calendario Tributario', 'link' => '?do=admin&page=beztaxcalendar', 'highlight' => true),
        );

        $bottomButtons = array(
            array('letter' => 'A', 'icon' => 'btn_a_clientes.png', 'emoji' => '👥', 'text' => 'Definicion de Clientes', 'link' => 'es:bez:types'),
            array('letter' => 'B', 'icon' => 'btn_b_procesos.png', 'emoji' => '🔄', 'text' => 'Definicion de Procesos', 'link' => 'es:bez:task_programs'),
            array('letter' => 'C', 'icon' => 'btn_c_semaforo.png', 'emoji' => '🚦', 'text' => 'Semaforizacion Tareas', 'link' => '?do=admin&page=tasksemaphore', 'highlight' => true),
        );

        // Logo: buscar primero en template activo, fallback al plugin
        $logoFile = DOKU_TPLINC . 'images/logo_sie_asesorias.png';
        $logoUrl  = DOKU_TPL . 'images/logo_sie_asesorias.png';
        if (!file_exists($logoFile)) {
            $logoFile = $imgPath . 'logo_sie_asesorias.png';
            $logoUrl  = $imgBase . 'logo_sie_asesorias.png';
        }
        $hasLogo = file_exists($logoFile);

        // Inyectar meta viewport si no existe (necesario para media queries en moviles)
        $html = '<script>if(!document.querySelector("meta[name=viewport]")){var m=document.createElement("meta");m.name="viewport";m.content="width=device-width,initial-scale=1.0";document.head.appendChild(m);}</script>';

        $html .= '<div class="nicc-menu-proyectos">';

        // === VISTA CIRCULAR (Desktop) ===
        $html .= '<div class="circular-menu">';
        $html .= '<div class="decorative-ring"></div>';
        $html .= '<div class="decorative-ring-inner"></div>';
        $html .= '<div class="center-logo">';
        if ($hasLogo) {
            $html .= '<img src="' . $logoUrl . '" alt="SIE Asesorias">';
        } else {
            $html .= '<span class="logo-text">SIE</span>';
        }
        $html .= '</div>';

        foreach ($circularItems as $item) {
            $url = (strpos($item['link'], '?') === 0) ? DOKU_BASE . 'doku.php' . $item['link'] : wl($item['link']);
            $hlClass = isset($item['highlight']) && $item['highlight'] ? ' highlight' : '';
            $iconFile = $imgPath . $item['icon'];
            $useImage = file_exists($iconFile);

            $html .= '<a href="' . $url . '" class="menu-item pos-' . $item['num'] . $hlClass . '">';
            $html .= '<span class="item-icon">';
            if ($useImage) {
                $html .= '<img src="' . $imgBase . $item['icon'] . '" alt="' . hsc($item['text']) . '">';
            } else {
                $html .= '<span class="emoji-fallback">' . $item['emoji'] . '</span>';
            }
            $html .= '</span>';
            $html .= '<span class="item-number">' . $item['num'] . '</span>';
            $html .= '<span class="item-text">' . hsc($item['text']) . '</span>';
            $html .= '</a>';
        }
        $html .= '</div>';

        // === VISTA GRID (Movil — oculto en desktop) ===
        $html .= '<div class="grid-fallback">';
        foreach ($circularItems as $item) {
            $url = (strpos($item['link'], '?') === 0) ? DOKU_BASE . 'doku.php' . $item['link'] : wl($item['link']);
            $hlClass = isset($item['highlight']) && $item['highlight'] ? ' highlight' : '';
            $iconFile = $imgPath . $item['icon'];
            $useImage = file_exists($iconFile);

            $html .= '<a href="' . $url . '" class="grid-item' . $hlClass . '">';
            $html .= '<span class="item-icon">';
            if ($useImage) {
                $html .= '<img src="' . $imgBase . $item['icon'] . '" alt="' . hsc($item['text']) . '">';
            } else {
                $html .= '<span class="emoji-fallback">' . $item['emoji'] . '</span>';
            }
            $html .= '</span>';
            $html .= '<span class="item-number">' . $item['num'] . '</span>';
            $html .= '<span class="item-text">' . hsc($item['text']) . '</span>';
            $html .= '</a>';
        }
        $html .= '</div>';

        // === BOTONES INFERIORES ===
        $html .= '<div class="bottom-buttons">';
        foreach ($bottomButtons as $btn) {
            $url = (strpos($btn['link'], '?') === 0) ? DOKU_BASE . 'doku.php' . $btn['link'] : wl($btn['link']);
            $hlClass = isset($btn['highlight']) && $btn['highlight'] ? ' highlight-btn' : '';
            $iconFile = $imgPath . $btn['icon'];
            $useImage = file_exists($iconFile);

            $html .= '<a href="' . $url . '" class="bottom-btn' . $hlClass . '">';
            $html .= '<span class="btn-icon">';
            if ($useImage) {
                $html .= '<img src="' . $imgBase . $btn['icon'] . '" alt="' . hsc($btn['text']) . '">';
            } else {
                $html .= '<span class="emoji-fallback">' . $btn['emoji'] . '</span>';
            }
            $html .= '</span>';
            $html .= '<span>' . hsc($btn['text']) . '</span>';
            $html .= '</a>';
        }
        $html .= '</div>';

        // JS respaldo: usar window.innerWidth (no offsetWidth que es afectado por sidebar)
        $html .= '<script>(function(){var c=document.querySelector(".nicc-menu-proyectos");if(!c)return;function chk(){var w=window.innerWidth;var cm=c.querySelector(".circular-menu");var gf=c.querySelector(".grid-fallback");if(w<769){if(cm)cm.style.cssText="display:none!important";if(gf){gf.style.cssText="display:block!important;overflow:hidden;width:100%";var gi=gf.querySelectorAll(".grid-item");for(var i=0;i<gi.length;i++){gi[i].style.cssText="display:block!important;float:left;width:50%;padding:11px 14px;background:#fff;border:1px solid #e8ecf4;text-decoration:none;box-sizing:border-box;overflow:hidden";}}}else{if(cm)cm.style.cssText="";if(gf){gf.style.cssText="";var gi=gf.querySelectorAll(".grid-item");for(var i=0;i<gi.length;i++){gi[i].style.cssText="";}}}}chk();window.addEventListener("resize",chk);window.addEventListener("load",chk);})();</script>';

        $html .= '</div>';

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