<?php
/**
 * DokuWiki Plugin comment (Syntax Component)
 *
 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
 * @author  Your Name
 */

if (!defined('DOKU_INC')) die();

class syntax_plugin_comment extends DokuWiki_Syntax_Plugin {
    
    protected static $comments = array();
    protected static $commentCounter = 0;

    public function getType() {
        return 'formatting';
    }

    public function getPType() {
        return 'normal';
    }

    public function getSort() {
        return 195;
    }

    public function connectTo($mode) {
        // Syntax for comment - must capture multiple pipes
        $this->Lexer->addSpecialPattern('\{\{comment>.+?\}\}', $mode, 'plugin_comment');
        // Syntax for list of all comments
        $this->Lexer->addSpecialPattern('\{\{comments-list\}\}', $mode, 'plugin_comment');
    }

    public function handle($match, $state, $pos, Doku_Handler $handler) {
        // Detect syntax type
        if ($match === '{{comments-list}}') {
            return array('type' => 'list');
        }
        
        // Process comment
        $match = substr($match, 10, -2); // Remove {{comment> and }}
        
        // Split into parts by |
        $parts = explode('|', $match);
        
        $text = isset($parts[0]) ? trim($parts[0]) : '';
        $comment = isset($parts[1]) ? trim($parts[1]) : '';
        $status = 'open';
        
        // Check status in third part
        if (isset($parts[2])) {
            $statusPart = trim($parts[2]);
            if (preg_match('/^status=(open|resolved|closed)$/i', $statusPart, $matches)) {
                $status = strtolower($matches[1]);
            }
        }
        
        return array(
            'type' => 'comment',
            'text' => $text,
            'comment' => $comment,
            'status' => $status
        );
    }

    public function render($mode, Doku_Renderer $renderer, $data) {
        if ($mode !== 'xhtml') return false;
        
        // Render comments list
        if ($data['type'] === 'list') {
            $this->renderCommentsList($renderer);
            return true;
        }
        
        // Render individual comment
        self::$commentCounter++;
        $commentNum = self::$commentCounter;
        
        // Save comment for end-of-page list
        self::$comments[] = array(
            'num' => $commentNum,
            'text' => $data['text'],
            'comment' => $data['comment'],
            'status' => $data['status']
        );
        
        // CSS classes by status
        $statusClass = 'comment-' . $data['status'];
        
        // Emoji for status
        $statusEmoji = array(
            'open' => '📬',
            'resolved' => '✅',
            'closed' => '❌'
        );
        $emoji = isset($statusEmoji[$data['status']]) ? $statusEmoji[$data['status']] : '📬';
        
        // Render highlighted text with comment number
        $renderer->doc .= '<span class="comment-highlight ' . $statusClass . '" data-comment="' . $commentNum . '" id="comment-ref-' . $commentNum . '">';
        $renderer->doc .= hsc($data['text']);
        $renderer->doc .= '<sup class="comment-number"><a href="#comment-bubble-' . $commentNum . '" class="comment-link">[💬' . $commentNum . ']</a></sup>';
        $renderer->doc .= '</span>';
        
        // Side bubble with comment
        $renderer->doc .= '<span class="comment-bubble ' . $statusClass . '" data-comment-id="' . $commentNum . '" id="comment-bubble-' . $commentNum . '">';
        $renderer->doc .= '<span class="comment-num"><a href="#comment-ref-' . $commentNum . '" class="comment-backlink">[' . $commentNum . ']</a></span> ';
        $renderer->doc .= '<span class="comment-status">' . $emoji . '</span><br>';
        $renderer->doc .= hsc($data['comment']);
        $renderer->doc .= '</span>';
        
        return true;
    }
    
    /**
     * Render list of all comments on the page
     */
    protected function renderCommentsList($renderer) {
        if (empty(self::$comments)) {
            $renderer->doc .= '<div class="comments-summary"><p><em>There are no comments on this page.</em></p></div>';
            return;
        }
        
        $renderer->doc .= '<div class="comments-summary">';
        $renderer->doc .= '<h3>Comments on this page</h3>';
        
        foreach (self::$comments as $comment) {
            $statusClass = 'comment-' . $comment['status'];
            $renderer->doc .= '<aside><div name="Comment" class="comment-item ' . $statusClass . '">';
            $renderer->doc .= '<div class="comment-header">';
            $renderer->doc .= '<span class="comment-num">[' . $comment['num'] . ']</span> ';
            $renderer->doc .= '<span class="comment-status-badge ' . $statusClass . '">' . hsc($comment['status']) . '</span>';
            $renderer->doc .= '</div>';
            $renderer->doc .= '<div class="comment-text-ref"><strong>Text:</strong> ' . hsc($comment['text']) . '</div>';
            $renderer->doc .= '<div class="comment-content"><strong>Comment:</strong> ' . hsc($comment['comment']) . '</div>';
            $renderer->doc .= '</div></aside> ';
        }
        
        $renderer->doc .= '</div>';
    }
}