<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>LiveComponent Event System Demo</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            padding: 2rem;
            background: #f5f5f5;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
        }

        h1 {
            margin-bottom: 2rem;
            color: #333;
        }

        .demo-grid {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 2rem;
            margin-bottom: 2rem;
        }

        .card {
            background: white;
            padding: 1.5rem;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }

        .card h2 {
            margin-bottom: 1rem;
            color: #444;
            font-size: 1.25rem;
        }

        .event-log {
            grid-column: 1 / -1;
        }

        .event-list {
            max-height: 300px;
            overflow-y: auto;
            background: #f9f9f9;
            padding: 1rem;
            border-radius: 4px;
            font-family: 'Monaco', 'Courier New', monospace;
            font-size: 0.875rem;
        }

        .event-item {
            padding: 0.5rem;
            margin-bottom: 0.5rem;
            background: white;
            border-left: 3px solid #4CAF50;
            border-radius: 2px;
        }

        .event-item.milestone {
            border-left-color: #FF9800;
        }

        .event-item.reset {
            border-left-color: #f44336;
        }

        .event-name {
            font-weight: bold;
            color: #333;
        }

        .event-payload {
            color: #666;
            margin-top: 0.25rem;
        }

        .event-time {
            color: #999;
            font-size: 0.75rem;
        }

        button {
            background: #2196F3;
            color: white;
            border: none;
            padding: 0.5rem 1rem;
            border-radius: 4px;
            cursor: pointer;
            font-size: 1rem;
            margin: 0.25rem;
        }

        button:hover {
            background: #1976D2;
        }

        .clear-btn {
            background: #f44336;
        }

        .clear-btn:hover {
            background: #d32f2f;
        }
    </style>
</head>
<body data-module="livecomponent">
    <div class="container">
        <h1>🚀 LiveComponent Event System Demo</h1>

        <div class="demo-grid">
            <div class="card">
                <h2>Counter Component #1</h2>
                {{counter1}}
            </div>

            <div class="card">
                <h2>Counter Component #2</h2>
                {{counter2}}
            </div>

            <div class="card event-log">
                <h2>Event Log (Live Updates)</h2>
                <button class="clear-btn" onclick="clearEventLog()">Clear Log</button>
                <div id="event-log" class="event-list">
                    <p style="color: #999;">Waiting for events...</p>
                </div>
            </div>
        </div>

        <div class="card">
            <h2>How It Works</h2>
            <ul style="line-height: 1.8; color: #666;">
                <li><strong>counter:changed</strong> - Dispatched on every increment/decrement/addAmount</li>
                <li><strong>counter:milestone</strong> - Dispatched when counter reaches multiples of 10</li>
                <li><strong>counter:reset</strong> - Dispatched when counter is reset to 0</li>
                <li>Events are broadcast to all components and logged in real-time</li>
                <li>Components can listen to events and react accordingly</li>
            </ul>
        </div>
    </div>

    <script>
        // Event logging functionality (UI-specific, not LiveComponent core)
        document.addEventListener('livecomponent:counter:changed', (e) => {
            logEvent('counter:changed', e.detail, false);
        });

        document.addEventListener('livecomponent:counter:milestone', (e) => {
            logEvent('counter:milestone', e.detail, true);
        });

        document.addEventListener('livecomponent:counter:reset', (e) => {
            logEvent('counter:reset', e.detail, true);
        });

        function logEvent(name, payload, isSpecial) {
            const log = document.getElementById('event-log');

            // Remove "waiting" message
            const waiting = log.querySelector('p');
            if (waiting) waiting.remove();

            const item = document.createElement('div');
            item.className = 'event-item';

            if (name.includes('milestone')) {
                item.classList.add('milestone');
            } else if (name.includes('reset')) {
                item.classList.add('reset');
            }

            const time = new Date().toLocaleTimeString();

            item.innerHTML = `
                <div class="event-name">${name}</div>
                <div class="event-payload">${JSON.stringify(payload)}</div>
                <div class="event-time">${time}</div>
            `;

            log.insertBefore(item, log.firstChild);

            // Keep only last 20 events
            while (log.children.length > 20) {
                log.removeChild(log.lastChild);
            }
        }

        window.clearEventLog = () => {
            const log = document.getElementById('event-log');
            log.innerHTML = '<p style="color: #999;">Log cleared. Waiting for events...</p>';
        };
    </script>
</body>
</html>
