<?php

/**
 * @group plugin_bpmnio
 * @group plugins
 */
class action_plugin_bpmnio_editor_test_form
{
    public array $hiddenFields = [];
    public string $html = '';

    public function setHiddenField($name, $value)
    {
        $this->hiddenFields[$name] = $value;
    }

    public function addHTML($html)
    {
        $this->html .= $html;
    }
}

class action_plugin_bpmnio_editor_test extends DokuWikiTest
{
    protected $pluginsEnabled = array('bpmnio');

    /**
     * Test that the editor plugin can be loaded
     */
    public function test_plugin_load()
    {
        $plugin = plugin_load('action', 'bpmnio_editor');
        $this->assertNotNull($plugin, 'Editor action plugin should be loadable');
    }

    /**
     * Test section edit button for BPMN target
     */
    public function test_section_edit_button_bpmn()
    {
        $plugin = plugin_load('action', 'bpmnio_editor');

        $data = ['target' => 'plugin_bpmnio_bpmn', 'name' => ''];
        $event = new \dokuwiki\Extension\Event('HTML_SECEDIT_BUTTON', $data);

        $plugin->sectionEditButton($event);

        $this->assertNotEmpty($data['name'], 'Button name should be set for BPMN target');
    }

    /**
     * Test section edit button for DMN target
     */
    public function test_section_edit_button_dmn()
    {
        $plugin = plugin_load('action', 'bpmnio_editor');

        $data = ['target' => 'plugin_bpmnio_dmn', 'name' => ''];
        $event = new \dokuwiki\Extension\Event('HTML_SECEDIT_BUTTON', $data);

        $plugin->sectionEditButton($event);

        $this->assertNotEmpty($data['name'], 'Button name should be set for DMN target');
    }

    /**
     * Test section edit button ignores non-bpmnio targets
     */
    public function test_section_edit_button_ignores_other()
    {
        $plugin = plugin_load('action', 'bpmnio_editor');

        $data = ['target' => 'section', 'name' => ''];
        $event = new \dokuwiki\Extension\Event('HTML_SECEDIT_BUTTON', $data);

        $plugin->sectionEditButton($event);

        $this->assertEmpty($data['name'], 'Button name should not be set for non-bpmnio targets');
    }

    /**
     * Test handlePost does nothing when plugin data not in POST
     */
    public function test_handle_post_noop_without_data()
    {
        $plugin = plugin_load('action', 'bpmnio_editor');

        global $TEXT;
        $TEXT = 'original';
        global $INPUT;

        $data = 'edit';
        $event = new \dokuwiki\Extension\Event('ACTION_ACT_PREPROCESS', $data);

        $plugin->handlePost($event);

        $this->assertEquals('original', $TEXT, '$TEXT should not change when plugin data is not posted');
    }

    public function test_handle_form_generates_balanced_editor_markup()
    {
        $plugin = plugin_load('action', 'bpmnio_editor');

        global $TEXT;
        global $RANGE;

        $TEXT = '<xml />';
        $RANGE = '1-2';

        $form = new action_plugin_bpmnio_editor_test_form();
        $data = [
            'target' => 'plugin_bpmnio_bpmn',
            'form' => $form,
        ];
        $event = new \dokuwiki\Extension\Event('EDIT_FORM_ADDTEXTAREA', $data);

        $plugin->handleForm($event);

        $this->assertArrayHasKey('plugin_bpmnio_data', $form->hiddenFields);
        $this->assertStringContainsString('id="plugin_bpmnio__bpmn_editor"', $form->html);
        $this->assertStringContainsString('<div class="bpmn_js_canvas">', $form->html);
        $this->assertSame(4, substr_count($form->html, '<div'));
        $this->assertSame(4, substr_count($form->html, '</div>'));
    }
}
