<?php
/*
 * Copyright (c) 2014-2020 Mark C. Prins <mprins@users.sf.net>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * @phpcs:disable Squiz.Classes.ValidClassName.NotCamelCaps
 */

/**
 * Add a Mapillary layer to your map.
 */
class syntax_plugin_openlayersmapoverlays_mapillarylayer extends DokuWiki_Syntax_Plugin {
    private $dflt = array(
        'id'      => 'olmap',
        'visible' => false,
        'skey'    => ''
    );

    /**
     *
     *
     * @see DokuWiki_Syntax_Plugin::getPType()
     */
    public function getPType(): string {
        return 'block';
    }

    /**
     *
     *
     * @see DokuWiki_Syntax_Plugin::getType()
     */
    public function getType(): string {
        // return 'FIXME: container|baseonly|formatting|substition|protected|disabled|paragraphs';
        return 'baseonly';
    }

    /**
     *
     *
     * @see Doku_Parser_Mode::getSort()
     */
    public function getSort(): int {
        return 903;
    }

    /**
     * Connect to our special pattern.
     *
     * @see Doku_Parser_Mode::connectTo()
     */
    public function connectTo($mode): void {
        // look for: <olmap_mapillarylayer id="olmap" visible="false"></olmap_mapillarylayer>
        $this->Lexer->addSpecialPattern(
            '<olmap_mapillarylayer ?[^>\n]*>.*?</olmap_mapillarylayer>', $mode,
            'plugin_openlayersmapoverlays_mapillarylayer'
        );
    }

    /**
     * (non-PHPdoc)
     *
     * @see DokuWiki_Syntax_Plugin::handle()
     */
    public function handle($match, $state, $pos, Doku_Handler $handler): array {
        $param = array();
        $data  = $this->dflt;

        preg_match_all('/(\w*)="(.*?)"/us', $match, $param, PREG_SET_ORDER);

        foreach($param as $kvpair) {
            list ($matched, $key, $val) = $kvpair;
            $key = strtolower($key);
            if(isset ($data [$key])) {
                $data [$key] = hsc($val);
            }
        }
        return $data;
    }

    /**
     * (non-PHPdoc)
     *
     * @see DokuWiki_Syntax_Plugin::render()
     */
    public function render($format, Doku_Renderer $renderer, $data): bool {
        if($format !== 'xhtml') {
            return false;
        }

        static $loadedOLlib = false;
        if(!$loadedOLlib) {
            $renderer->doc .= DOKU_LF . '<script defer="defer" src="' . DOKU_BASE
                . 'lib/plugins/openlayersmapoverlays/lib/layers.js' . '"></script>';
            $loadedOLlib   = true;
        }
        // incremented for each olmap_osmlayer tag in the page source
        static $overlaynumber = 0;

        list ($id, $url, $name, $visible) = $data;
        $renderer->doc .= DOKU_LF . '<script defer="defer" src="data:text/javascript;base64,';
        $str           = '{';
        foreach($data as $key => $val) {
            $str .= "'" . $key . "':'" . $val . "',";
        }
        $str           .= "'type':'mapillary'}";
        $renderer->doc .= base64_encode("olMapOverlays['mapillary" . $overlaynumber . "'] = " . $str . ";")
            . '"></script>';
        $overlaynumber++;
        return true;
    }
}