<?php
/**
 * DokuWiki Plugin CountdownV2 (Syntax Component)
 *
 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
 * @author  Frank Kosanke <digger@error-404.de>
 */
class syntax_plugin_countdownv2 extends \dokuwiki\Extension\SyntaxPlugin
{
    /** @inheritDoc */
    public function getType()
    {
        return 'substition';
    }

    /** @inheritDoc */
    public function getPType()
    {
        return 'normal';
    }

    /** @inheritDoc */
    public function getSort()
    {
        return 999;
    }

    /** @inheritDoc */
    public function connectTo($mode)
    {
        $this->Lexer->addSpecialPattern('<countdown=.+?>', $mode, 'plugin_countdownv2');
        // $this->Lexer->addEntryPattern('<FIXME>', $mode, 'plugin_countdown');
    }

    // /** @inheritDoc */
    // public function postConnect()
    // {
    //     $this->Lexer->addExitPattern('</FIXME>', 'plugin_countdownv2');
    // }

    /** @inheritDoc */
    public function handle($match, $state, $pos, Doku_Handler $handler)
    {
        return array(substr($match, 11, -1));
    }

    /** @inheritDoc */
    public function render($mode, Doku_Renderer $renderer, $data)
    {
        // if ($mode !== 'xhtml') {
        //    return false;
        // }
        // return true;
        $color = $this->getConf('colordefault'); // Standardfarbe
        $unit = "";                              // Einheit
        $result = "";                            // Zeitstring Ergebnis 
        $comment = "";                           // Kommentarstring
        $commentstr = "";                        // Kommentarstring ohne Datum und Zeit

        // String zerlegen
        $parts = explode("|", $data[0]);
        if (isset($parts[1])) {
            $commentstr = trim($parts[1]);               // Kommentarstring
        }
        // Type ermitteln und Zeitstring ermitteln
        $typestr = substr(trim($parts[0]),0,1);          // Zeichen für den Typ
        $timestr = substr(trim($parts[0]),2);            // Zeitstring

        $starttime = time();
        $endtime = strtotime($timestr);
        $difftime = $endtime - $starttime;

        // verbleibende Zeit ermitteln und formatieren
        switch ($typestr) {
        case "Y":
            $result = ceil($difftime / (60*60*24*365));
            $unit = $this->getLang('year');
            if (($result != 1) && ($result != -1)) { $unit = $this->getLang('years'); }
            if ($difftime <= $this->getConf('yearwarning')) { $color = $this->getConf('colorwarning'); }
            break;
        case "M":
            $result = ceil($difftime / (60*60*24*31));
            $unit = $this->getLang('month');
            if (($result != 1) && ($result != -1)) { $unit = $this->getLang('months'); }
            if ($difftime <= $this->getConf('monthwarning')) { $color = $this->getConf('colorwarning'); }
            break;
        case "W":
            $result = ceil($difftime / (60*60*24*7));
            $unit = $this->getLang('week');
            if (($result != 1) && ($result != -1)) { $unit = $this->getLang('weeks'); }
            if ($difftime <= $this->getConf('weekwarning')) { $color = $this->getConf('colorwarning'); }
            break;
        case "D":
            $result = ceil($difftime / (60*60*24));
            $unit = $this->getLang('day');
            if (($result != 1) && ($result != -1)) { $unit = $this->getLang('days'); }
            if ($difftime <= $this->getConf('daywarning')) { $color = $this->getConf('colorwarning'); }
            break;
        case "h":
            $result = ceil($difftime / (60*60));
            $unit = $this->getLang('hour');
            if (($result != 1) && ($result != -1)) { $unit = $this->getLang('hours'); }
            if ($difftime <= $this->getConf('hourwarning')) { $color = $this->getConf('colorwarning'); }
            break;
        case "m":
            $result = ceil($difftime / 60);
            $unit = $this->getLang('minute');
            if (($result != 1) && ($result != -1)) { $unit = $this->getLang('minutes'); }
            if ($difftime <= $this->getConf('minutewarning')) { $color = $this->getConf('colorwarning'); }
            break;
        }

        // Kommentar hinzufügen
        if (strlen($commentstr) > 0) {
            // Datum einfügen
            $comment = str_replace("##DATE##", strftime($this->getConf('dateformat'), $endtime), $commentstr);
            // Zeit einfügen
            $comment = str_replace("##TIME##", strftime($this->getConf('timeformat'), $endtime), $comment);
            // Datum und Zeit einfügen
            $comment = str_replace("##DATETIME##", strftime($this->getConf('datetimeformat'), $endtime), $comment);
            // Leerzeichen einfügen
        }

        // Wenn Zeit abgelaufen dann Farbe auf kritisch setzen
        if ($endtime < $starttime) {
            $color = $this->getConf('colorcritical');
        }

        // Ausgabe erstellen
        $renderer->doc .= "<span style=\"color:" . $color . ";\">" . $result . "&nbsp;" . $unit . "</span>&nbsp;" . $comment;
        return true;
    }
}

