{% extends "base.html" %}

{% block title %}Meine Tutorials{% endblock %}

{% block head %}
{{ super() }}
<style>
    .tutorials-header {
        margin-bottom: 2rem;
        padding: 1.5rem;
        background: linear-gradient(135deg, #296f78 0%, #1a5057 100%);
        border-radius: 14px;
        color: white;
    }

    .tutorials-header h1 {
        color: white;
        margin-bottom: 0.5rem;
    }

    .tutorials-header p {
        color: rgba(255, 255, 255, 0.9);
        font-size: 1.05rem;
    }

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

    .tutorial-player-section {
        background: white;
        border: 1px solid #d0d7d0;
        border-radius: 14px;
        overflow: hidden;
        box-shadow: 0 10px 30px rgba(18, 40, 44, 0.08);
    }

    .video-player-wrapper {
        position: relative;
        width: 100%;
        padding-bottom: 56.25%; /* 16:9 aspect ratio */
        height: 0;
        overflow: hidden;
        background: #000;
    }

    .video-player-wrapper iframe {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        border: none;
    }

    .video-player-wrapper video {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
    }

    .video-details {
        padding: 2rem;
    }

    .video-title {
        font-size: 1.8rem;
        color: var(--text-main);
        margin-bottom: 0.5rem;
        font-family: Arial, Helvetica, sans-serif;
    }

    .video-meta {
        display: flex;
        gap: 1.5rem;
        margin-bottom: 1rem;
        flex-wrap: wrap;
    }

    .video-meta-item {
        display: flex;
        align-items: center;
        gap: 0.5rem;
        font-size: 0.95rem;
        color: var(--text-soft);
    }

    .video-meta-badge {
        display: inline-flex;
        align-items: center;
        padding: 0.4rem 0.8rem;
        background: #dbe8e5;
        color: #1f5158;
        border-radius: 999px;
        font-size: 0.85rem;
        font-weight: 600;
    }

    .video-description {
        color: var(--text-soft);
        line-height: 1.6;
        margin-bottom: 1.5rem;
    }

    .tutorials-grid {
        display: grid;
        grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
        gap: 1.5rem;
        margin-top: 2rem;
    }

    .tutorial-card {
        background: white;
        border: 1px solid #d0d7d0;
        border-radius: 14px;
        overflow: hidden;
        transition: all 0.3s ease;
        cursor: pointer;
        box-shadow: 0 2px 8px rgba(18, 40, 44, 0.04);
    }

    .tutorial-card:hover {
        transform: translateY(-4px);
        box-shadow: 0 10px 30px rgba(18, 40, 44, 0.12);
        border-color: #296f78;
    }

    .tutorial-thumbnail {
        position: relative;
        width: 100%;
        padding-bottom: 56.25%;
        height: 0;
        overflow: hidden;
        background: linear-gradient(135deg, #296f78 0%, #1a5057 100%);
    }

    .tutorial-thumbnail img {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        object-fit: cover;
    }

    .play-icon {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        width: 60px;
        height: 60px;
        background: rgba(255, 255, 255, 0.9);
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        transition: all 0.3s ease;
    }

    .tutorial-card:hover .play-icon {
        background: rgba(255, 255, 255, 1);
        transform: translate(-50%, -50%) scale(1.1);
    }

    .play-icon::after {
        content: "";
        width: 0;
        height: 0;
        border-left: 20px solid #296f78;
        border-top: 12px solid transparent;
        border-bottom: 12px solid transparent;
        margin-left: 4px;
    }

    .tutorial-info {
        padding: 1.2rem;
    }

    .tutorial-card-title {
        font-size: 1.1rem;
        color: var(--text-main);
        margin-bottom: 0.5rem;
        font-weight: 600;
        line-height: 1.4;
        font-family: Arial, Helvetica, sans-serif;
    }

    .tutorial-card-meta {
        display: flex;
        justify-content: space-between;
        align-items: center;
        font-size: 0.85rem;
        color: var(--text-soft);
    }

    .tutorial-duration {
        display: inline-flex;
        align-items: center;
        gap: 0.3rem;
    }

    .tutorial-category {
        font-size: 0.75rem;
        background: #f0f3f0;
        color: #1f5158;
        padding: 0.3rem 0.6rem;
        border-radius: 6px;
        font-weight: 600;
    }

    .empty-state {
        text-align: center;
        padding: 3rem 2rem;
        background: #f9faf8;
        border: 1px dashed #d0d7d0;
        border-radius: 14px;
        margin: 2rem 0;
    }

    .empty-state-icon {
        font-size: 3rem;
        margin-bottom: 1rem;
        opacity: 0.5;
    }

    .empty-state h3 {
        color: var(--text-main);
        margin-bottom: 0.5rem;
    }

    .empty-state p {
        color: var(--text-soft);
        max-width: 60ch;
        margin: 0 auto;
    }

    .controls-row {
        display: flex;
        gap: 1rem;
        margin-top: 1.5rem;
        flex-wrap: wrap;
    }

    .btn-secondary {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        padding: 0.7rem 1.4rem;
        background: white;
        color: #296f78;
        border: 2px solid #296f78;
        border-radius: 999px;
        font-weight: 600;
        font-size: 0.95rem;
        cursor: pointer;
        transition: all 0.3s ease;
        text-decoration: none;
    }

    .btn-secondary:hover {
        background: #296f78;
        color: white;
        transform: translateY(-2px);
    }

    @media (max-width: 768px) {
        .tutorials-grid {
            grid-template-columns: 1fr;
        }

        .video-title {
            font-size: 1.4rem;
        }

        .video-meta {
            flex-direction: column;
            gap: 0.8rem;
        }

        .video-details {
            padding: 1.5rem;
        }
    }
</style>
{% endblock %}

{% block content %}
<section class="tutorials-header">
    <h1>Tutorial-Videos</h1>
    <p>Lernen Sie mit unseren umfassenden Tutorials, wie Sie Invario optimal nutzen können.</p>
</section>

{% if tutorials %}
    <section class="tutorial-player-section">
        <div class="video-player-wrapper" id="mainVideoPlayer">
            <iframe id="videoFrame" 
                    width="100%" 
                    height="100%" 
                    src="{{ tutorials[0].video_url }}" 
                    title="{{ tutorials[0].title }}"
                    frameborder="0" 
                    allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" 
                    referrerpolicy="strict-origin-when-cross-origin"
                    allowfullscreen>
            </iframe>
        </div>
        <div class="video-details">
            <h2 class="video-title" id="videoTitle">{{ tutorials[0].title }}</h2>
            <div class="video-meta">
                <div class="video-meta-item">
                    <span class="video-meta-badge">
                        {{ tutorials[0].category or 'Allgemein' }}
                    </span>
                </div>
                <div class="video-meta-item">
                    <span class="video-meta-badge">
                        {{ tutorials[0].duration or 'Unbekannt' }}
                    </span>
                </div>
            </div>
            <p class="video-description" id="videoDescription">
                {{ tutorials[0].description }}
            </p>
            <div class="controls-row">
                <button class="btn-secondary" onclick="shareTutorial()">
                    🔗 Teilen
                </button>
                <button class="btn-secondary" onclick="reportIssue()">
                    ⚠️ Problem melden
                </button>
            </div>
        </div>
    </section>

    <section>
        <h3 style="margin: 2rem 0 1.5rem; font-size: 1.5rem; color: var(--text-main);">
            Alle Tutorials
        </h3>
        <div class="tutorials-grid">
            {% for tutorial in tutorials %}
            <div
                class="tutorial-card"
                data-video-url="{{ tutorial.video_url|e }}"
                data-title="{{ tutorial.title|e }}"
                data-description="{{ tutorial.description|default('', true)|e }}"
                data-category="{{ tutorial.category|default('Allgemein', true)|e }}"
                data-duration="{{ tutorial.duration|default('', true)|e }}"
            >
                <div class="tutorial-thumbnail">
                    {% if tutorial.thumbnail_url %}
                        <img src="{{ tutorial.thumbnail_url }}" alt="{{ tutorial.title }}" />
                    {% endif %}
                    <div class="play-icon"></div>
                </div>
                <div class="tutorial-info">
                    <h4 class="tutorial-card-title">{{ tutorial.title }}</h4>
                    <div class="tutorial-card-meta">
                        {% if tutorial.duration %}
                        <span class="tutorial-duration">⏱{{ tutorial.duration }}</span>
                        {% endif %}
                        <span class="tutorial-category">{{ tutorial.category|default('Allgemein') }}</span>
                    </div>
                </div>
            </div>
            {% endfor %}
        </div>
    </section>

    <script>
        function loadVideo(videoUrl, title, description, category, duration) {
            // Update main video player
            const videoFrame = document.getElementById('videoFrame');
            const videoTitle = document.getElementById('videoTitle');
            const videoDescription = document.getElementById('videoDescription');

            if (!videoUrl) {
                alert('Dieses Video kann aktuell nicht geladen werden.');
                return;
            }
            
            videoFrame.src = videoUrl;
            videoTitle.textContent = title;
            videoDescription.textContent = description;
            
            // Scroll to player
            document.querySelector('.tutorial-player-section').scrollIntoView({ behavior: 'smooth' });
        }

        function shareTutorial() {
            const url = window.location.href;
            if (navigator.share) {
                navigator.share({
                    title: document.getElementById('videoTitle').textContent,
                    text: document.getElementById('videoDescription').textContent,
                    url: url
                });
            } else {
                const text = `${document.getElementById('videoTitle').textContent}\n${url}`;
                navigator.clipboard.writeText(text);
                alert('Link kopiert!');
            }
        }

        function reportIssue() {
            const title = document.getElementById('videoTitle').textContent;
            window.location.href = `{{ url_for('user_tickets') }}?subject=Tutorial-Feedback: ${encodeURIComponent(title)}`;
        }

        document.querySelectorAll('.tutorial-card').forEach((card) => {
            card.addEventListener('click', () => {
                loadVideo(
                    card.dataset.videoUrl || '',
                    card.dataset.title || '',
                    card.dataset.description || '',
                    card.dataset.category || 'Allgemein',
                    card.dataset.duration || ''
                );
            });
        });
    </script>

{% else %}
    <div class="empty-state">
        <div class="empty-state-icon">🎬</div>
        <h3>Keine Tutorials verfügbar</h3>
        <p>Es sind derzeit keine Tutorial-Videos verfügbar. Bitte versuchen Sie es später erneut oder kontaktieren Sie den Support.</p>
    </div>
{% endif %}

{% endblock %}
