<?php

use dokuwiki\Extension\SyntaxPlugin;

/**
 * @license    See LICENSE file
 */
// See help: https://www.dokuwiki.org/devel:syntax_plugins
// The HTML structure generated by this syntax plugin is:
//
// <div class="plugin-bpmnio" id="__(bpmn|dmn)_js_<hash>">
//   <div class="bpmn_js_data">
//      ... base64 encoded xml
//   </div>
//   <div class="bpmn_js_canvas {$class}">
//     <div class="bpmn_js_container">... rendered herein</div>
//   </div>
// </div>
class syntax_plugin_bpmnio_bpmnio extends SyntaxPlugin
{
    public string $type = ''; // 'bpmn' or 'dmn'

    public function getPType()
    {
        return 'block';
    }

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

    public function getSort()
    {
        return 0;
    }

    public function connectTo($mode)
    {
        $this->Lexer->addEntryPattern('<bpmnio.*?>(?=.*?</bpmnio>)', $mode, 'plugin_bpmnio_bpmnio');
    }

    public function postConnect()
    {
        $this->Lexer->addExitPattern('</bpmnio>', 'plugin_bpmnio_bpmnio');
    }

    public function handle($match, $state, $pos, Doku_Handler $handler)
    {
        switch ($state) {
            case DOKU_LEXER_ENTER:
                $matched = '';
                preg_match('/<bpmnio type="(\w+)">/', $match, $matched);
                $this->type = $matched[1] ?? 'bpmn';
                return [$state, $this->type, '', $pos, ''];

            case DOKU_LEXER_UNMATCHED:
                $posStart = $pos;
                $posEnd = $pos + strlen($match);
                $match = base64_encode($match);
                return [$state, $this->type, $match, $posStart, $posEnd];

            case DOKU_LEXER_EXIT:
                $this->type = '';
                return [$state, '', '', '', ''];
        }
        return [];
    }

    public function render($mode, Doku_Renderer $renderer, $data)
    {
        [$state, $type, $match, $posStart, $posEnd] = $data;

        if (is_a($renderer, 'renderer_plugin_dw2pdf')) {
            if ($state == DOKU_LEXER_EXIT) {
                $renderer->doc .= <<<HTML
                    <div class="plugin-bpmnio">
                        <a href="https://github.com/Color-Of-Code/dokuwiki-plugin-bpmnio/issues/4">
                            DW2PDF support missing: Help wanted
                        </a>
                    </div>
                    HTML;
            }
            return true;
        }

        if ($mode == 'xhtml' || $mode == 'odt') {
            switch ($state) {
                case DOKU_LEXER_ENTER:
                    $bpmnid = "__{$type}_js_{$posStart}";
                    $renderer->doc .= <<<HTML
                        <div class="plugin-bpmnio" id="{$bpmnid}">
                        HTML;
                    break;

                case DOKU_LEXER_UNMATCHED:
                    $renderer->doc .= <<<HTML
                        <div class="{$type}_js_data">
                            {$match}
                        </div>
                        HTML;

                    $target = "plugin_bpmnio_{$type}";
                    $sectionEditData = ['target' => $target];
                    $class = $renderer->startSectionEdit($posStart, $sectionEditData);
                    $renderer->doc .= <<<HTML
                        <div class="{$type}_js_canvas {$class}">
                            <div class="{$type}_js_container"></div>
                        </div>
                        HTML;
                    $renderer->finishSectionEdit($posEnd);
                    break;

                case DOKU_LEXER_EXIT:
                    $renderer->doc .= <<<HTML
                        </div>
                        HTML;
                    break;
            }
            return true;
        }
        return false;
    }
}
