"""
Klassen Widget fuer das Lehrer-Dashboard.

Zeigt eine Uebersicht aller Klassen des Lehrers.
"""


class KlassenWidget:
    widget_id = 'klassen'
    widget_name = 'Meine Klassen'
    widget_icon = '&#128202;'  # Chart
    widget_color = '#8b5cf6'  # Purple
    default_width = 'half'
    has_settings = True

    @staticmethod
    def get_css() -> str:
        return """
/* ===== Klassen Widget Styles ===== */
.widget-klassen {
    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-klassen .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-klassen .widget-title {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 14px;
    font-weight: 600;
    color: var(--bp-text, #e5e7eb);
}

.widget-klassen .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-klassen .klassen-list {
    flex: 1;
    overflow-y: auto;
}

.widget-klassen .klasse-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px;
    margin-bottom: 8px;
    background: var(--bp-bg, #0f172a);
    border: 1px solid var(--bp-border-subtle, rgba(255,255,255,0.1));
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s;
}

.widget-klassen .klasse-item:last-child {
    margin-bottom: 0;
}

.widget-klassen .klasse-item:hover {
    background: var(--bp-surface-elevated, #334155);
    border-color: #8b5cf6;
    transform: translateX(4px);
}

.widget-klassen .klasse-info {
    display: flex;
    align-items: center;
    gap: 12px;
}

.widget-klassen .klasse-badge {
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(139, 92, 246, 0.15);
    color: #8b5cf6;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 700;
}

.widget-klassen .klasse-details {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.widget-klassen .klasse-name {
    font-size: 14px;
    font-weight: 600;
    color: var(--bp-text, #e5e7eb);
}

.widget-klassen .klasse-meta {
    font-size: 12px;
    color: var(--bp-text-muted, #9ca3af);
}

.widget-klassen .klasse-arrow {
    color: var(--bp-text-muted, #9ca3af);
    font-size: 16px;
    transition: transform 0.2s;
}

.widget-klassen .klasse-item:hover .klasse-arrow {
    transform: translateX(4px);
    color: #8b5cf6;
}

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

.widget-klassen .klassen-add-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-klassen .klassen-add-btn:hover {
    border-color: #8b5cf6;
    color: #8b5cf6;
}

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

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

    @staticmethod
    def get_html() -> str:
        return """
<div class="widget-klassen" data-widget-id="klassen">
    <div class="widget-header">
        <div class="widget-title">
            <span class="widget-icon">&#128202;</span>
            <span>Meine Klassen</span>
        </div>
        <button class="widget-settings-btn" onclick="openWidgetSettings('klassen')" title="Einstellungen">&#9881;</button>
    </div>
    <div class="klassen-list" id="klassen-list">
        <!-- Wird dynamisch gefuellt -->
    </div>
    <div class="klassen-footer">
        <button class="klassen-add-btn" onclick="openKlassenManagement()">+ Alle Klassen anzeigen</button>
    </div>
</div>
"""

    @staticmethod
    def get_js() -> str:
        return """
// ===== Klassen Widget JavaScript =====
const KLASSEN_STORAGE_KEY = 'bp-lehrer-klassen';

function getDefaultKlassen() {
    return [
        { id: '10a', name: 'Klasse 10a', schueler: 28, fach: 'Deutsch', klassenlehrer: false },
        { id: '11b', name: 'Klasse 11b', schueler: 26, fach: 'Deutsch', klassenlehrer: true },
        { id: '12c', name: 'Klasse 12c', schueler: 24, fach: 'Deutsch', klassenlehrer: false }
    ];
}

function loadKlassen() {
    const stored = localStorage.getItem(KLASSEN_STORAGE_KEY);
    return stored ? JSON.parse(stored) : getDefaultKlassen();
}

function saveKlassen(klassen) {
    localStorage.setItem(KLASSEN_STORAGE_KEY, JSON.stringify(klassen));
}

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

    const klassen = loadKlassen();

    if (klassen.length === 0) {
        list.innerHTML = `
            <div class="klassen-empty">
                <div class="klassen-empty-icon">&#127979;</div>
                <div>Keine Klassen zugewiesen</div>
            </div>
        `;
        return;
    }

    list.innerHTML = klassen.map(klasse => `
        <div class="klasse-item" onclick="openKlasse('${klasse.id}')">
            <div class="klasse-info">
                <div class="klasse-badge">${klasse.id}</div>
                <div class="klasse-details">
                    <span class="klasse-name">${klasse.name}${klasse.klassenlehrer ? ' &#11088;' : ''}</span>
                    <span class="klasse-meta">${klasse.schueler} Schueler &middot; ${klasse.fach}</span>
                </div>
            </div>
            <span class="klasse-arrow">&#8594;</span>
        </div>
    `).join('');
}

function openKlasse(klasseId) {
    // Navigate to school module with class selected
    if (typeof loadModule === 'function') {
        loadModule('school');
        // Could set a flag to auto-select the class
        console.log('Opening class:', klasseId);
    }
}

function openKlassenManagement() {
    if (typeof loadModule === 'function') {
        loadModule('school');
    }
}

function initKlassenWidget() {
    renderKlassen();
}
"""
