"""
Matrix Chat Widget fuer das Lehrer-Dashboard.

Zeigt die letzten Chat-Nachrichten aus dem Matrix Messenger.
"""


class MatrixWidget:
    widget_id = 'matrix'
    widget_name = 'Matrix-Chat'
    widget_icon = '&#128172;'  # Speech bubble
    widget_color = '#8b5cf6'  # Purple
    default_width = 'half'
    has_settings = True

    @staticmethod
    def get_css() -> str:
        return """
/* ===== Matrix Widget Styles ===== */
.widget-matrix {
    background: var(--bp-surface, #1e293b);
    border: 1px solid var(--bp-border, #475569);
    border-radius: 12px;
    padding: 16px;
    height: 100%;
    display: flex;
    flex-direction: column;
}

.widget-matrix .widget-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 12px;
    padding-bottom: 12px;
    border-bottom: 1px solid var(--bp-border-subtle, rgba(255,255,255,0.1));
}

.widget-matrix .widget-title {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 14px;
    font-weight: 600;
    color: var(--bp-text, #e5e7eb);
}

.widget-matrix .widget-icon {
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(139, 92, 246, 0.15);
    color: #8b5cf6;
    border-radius: 8px;
    font-size: 14px;
}

.widget-matrix .matrix-list {
    flex: 1;
    overflow-y: auto;
}

.widget-matrix .chat-item {
    display: flex;
    gap: 10px;
    padding: 10px 0;
    border-bottom: 1px solid var(--bp-border-subtle, rgba(255,255,255,0.05));
    cursor: pointer;
    transition: background 0.2s;
}

.widget-matrix .chat-item:last-child {
    border-bottom: none;
}

.widget-matrix .chat-item:hover {
    background: var(--bp-surface-elevated, #334155);
    margin: 0 -12px;
    padding: 10px 12px;
    border-radius: 8px;
}

.widget-matrix .chat-avatar {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(139, 92, 246, 0.15);
    color: #8b5cf6;
    border-radius: 50%;
    font-size: 12px;
    flex-shrink: 0;
}

.widget-matrix .chat-content {
    flex: 1;
    min-width: 0;
}

.widget-matrix .chat-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 2px;
}

.widget-matrix .chat-room {
    font-size: 12px;
    font-weight: 600;
    color: var(--bp-text, #e5e7eb);
}

.widget-matrix .chat-time {
    font-size: 10px;
    color: var(--bp-text-muted, #9ca3af);
}

.widget-matrix .chat-message {
    font-size: 12px;
    color: var(--bp-text-muted, #9ca3af);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.widget-matrix .chat-unread {
    width: 8px;
    height: 8px;
    background: #8b5cf6;
    border-radius: 50%;
    margin-left: 8px;
}

.widget-matrix .matrix-empty {
    text-align: center;
    padding: 24px;
    color: var(--bp-text-muted, #9ca3af);
    font-size: 13px;
}

.widget-matrix .matrix-empty-icon {
    font-size: 32px;
    margin-bottom: 8px;
    opacity: 0.5;
}

.widget-matrix .matrix-footer {
    margin-top: 12px;
    padding-top: 12px;
    border-top: 1px solid var(--bp-border-subtle, rgba(255,255,255,0.1));
}

.widget-matrix .matrix-all-btn {
    width: 100%;
    padding: 10px;
    background: transparent;
    border: 1px dashed var(--bp-border, #475569);
    border-radius: 8px;
    color: var(--bp-text-muted, #9ca3af);
    font-size: 12px;
    cursor: pointer;
    transition: all 0.2s;
}

.widget-matrix .matrix-all-btn:hover {
    border-color: #8b5cf6;
    color: #8b5cf6;
}
"""

    @staticmethod
    def get_html() -> str:
        return """
<div class="widget-matrix" data-widget-id="matrix">
    <div class="widget-header">
        <div class="widget-title">
            <span class="widget-icon">&#128172;</span>
            <span>Matrix-Chat</span>
        </div>
        <button class="widget-settings-btn" onclick="openWidgetSettings('matrix')" title="Einstellungen">&#9881;</button>
    </div>
    <div class="matrix-list" id="matrix-list">
        <!-- Wird dynamisch gefuellt -->
    </div>
    <div class="matrix-footer">
        <button class="matrix-all-btn" onclick="openMessenger()">+ Messenger oeffnen</button>
    </div>
</div>
"""

    @staticmethod
    def get_js() -> str:
        return """
// ===== Matrix Widget JavaScript =====

function getDefaultMatrixChats() {
    const now = Date.now();
    return [
        {
            id: 'room1',
            room: 'Kollegium Deutsch',
            lastMessage: 'Hat jemand das neue Curriculum?',
            sender: 'Fr. Becker',
            time: new Date(now - 30 * 60 * 1000).toISOString(),
            unread: true
        },
        {
            id: 'room2',
            room: 'Klassenfahrt 10a',
            lastMessage: 'Die Anmeldungen sind komplett!',
            sender: 'Hr. Klein',
            time: new Date(now - 3 * 60 * 60 * 1000).toISOString(),
            unread: false
        },
        {
            id: 'room3',
            room: 'Fachschaft',
            lastMessage: 'Termin fuer naechste Sitzung...',
            sender: 'Sie',
            time: new Date(now - 24 * 60 * 60 * 1000).toISOString(),
            unread: false
        }
    ];
}

function formatMatrixTime(timeStr) {
    const time = new Date(timeStr);
    const now = new Date();
    const diffMs = now - time;
    const diffMins = Math.floor(diffMs / (60 * 1000));
    const diffHours = Math.floor(diffMs / (60 * 60 * 1000));

    if (diffMins < 1) return 'jetzt';
    if (diffMins < 60) return `${diffMins}m`;
    if (diffHours < 24) return `${diffHours}h`;
    return time.toLocaleDateString('de-DE', { day: '2-digit', month: '2-digit' });
}

function renderMatrixChats() {
    const list = document.getElementById('matrix-list');
    if (!list) return;

    const chats = getDefaultMatrixChats();

    if (chats.length === 0) {
        list.innerHTML = `
            <div class="matrix-empty">
                <div class="matrix-empty-icon">&#128172;</div>
                <div>Keine Chats verfuegbar</div>
            </div>
        `;
        return;
    }

    list.innerHTML = chats.map(chat => `
        <div class="chat-item" onclick="openMatrixRoom('${chat.id}')">
            <div class="chat-avatar">&#128172;</div>
            <div class="chat-content">
                <div class="chat-header">
                    <span class="chat-room">${chat.room}</span>
                    <span class="chat-time">${formatMatrixTime(chat.time)}</span>
                </div>
                <div class="chat-message">${chat.sender}: ${chat.lastMessage}</div>
            </div>
            ${chat.unread ? '<div class="chat-unread"></div>' : ''}
        </div>
    `).join('');
}

function openMatrixRoom(roomId) {
    if (typeof loadModule === 'function') {
        loadModule('messenger');
        console.log('Opening room:', roomId);
    }
}

function openMessenger() {
    if (typeof loadModule === 'function') {
        loadModule('messenger');
    }
}

function initMatrixWidget() {
    renderMatrixChats();
}
"""
