{% extends 'base.html' %}
{% load static %}
{% load humanize %}
{% block extra_css %}
<style>


  .custom-outline {
    -webkit-text-stroke: 0.2px rgb(0, 0, 0);
  }
</style>
{% endblock %}

{% block content %}


{% block head %}
{% include 'library/head.html' %}
{% endblock %}



{% if user.is_authenticated %}

<div class="container mx-auto px-4 sm:px-6 lg:px-8 mt-8 mb-6 dark:text-white">


      <div class="flex justify-end  text-sm mb-2">
      <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-6 text-blue-600">
      <path stroke-linecap="round" stroke-linejoin="round" d="m21 21-5.197-5.197m0 0A7.5 7.5 0 1 0 5.196 5.196a7.5 7.5 0 0 0 10.607 10.607Z" />
      </svg>
      <div class="gap-2 mx-2 ">
      Anzahl aller Ergebnisse:
      <span class="font-bold"> {{ total_all_found }}</span>
      </div>
      (verschiedene: 
      <span class="font-bold ml-1"> {{ total_all }}</span>) </div>

  <div class="flex flex-wrap justify-end gap-4 font-bold text-blue-600 whitespace-nowrap text-sm mb-2 md:mb-0">
    <a href="{{ url_all }}?{{ request.GET.urlencode }}">Alle ({{ total_all }})</a>
    <a href="{{ url_my }}?{{ request.GET.urlencode }}">Meine Quizze ({{ total_my }})</a>
    <p class="font-black underline">Meine Favoriten ({{ total_favorite }})</p>
  </div>

{% endif %}


        <div class="flex items-center gap-3">
    <h2 id="my-quizzes" class="text-2xl font-bold text-gray-900 dark:text-white">Meine Favoriten</h2>
    <div class="h-0.5 flex-grow bg-gradient-to-r from-blue-600 to-transparent rounded-full"></div>
  </div>
</div>
{% if request.user.is_authenticated and favorite_quizzes %}
<div class="container mx-auto px-4 sm:px-6 lg:px-8">
  <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-6 auto-rows-fr">
    {% for quiz in favorite_quizzes %}
    <article class="relative min-h-[20rem] rounded-xl shadow-lg overflow-hidden bg-white flex flex-col ">
        <!-- Image Section (Top) -->
        <div class="h-48 w-full relative flex-shrink-0 ">
          {% if quiz.image %}
          <div class="relative w-full h-full inset-0 bg-cover bg-center">
            <!-- Hintergrundbild -->
            <div class="absolute inset-0 bg-cover bg-center"
                style="background-image: url({{ quiz.image.image.url }});">
            </div>
          </div>
         

          {% else %}
          <div class="h-full bg-gradient-to-r from-blue-500 to-blue-600 flex items-center justify-center font-black text-white text-3xl  px-4  overflow-auto break-words hyphens-auto ">{{ quiz.name }}</div>
          {% endif %}
          {% if user.is_authenticated %}
          {% if quiz.favorite == False %}
         
          <div class="absolute top-4 right-4 bg-white bg-opacity-60 text-white px-4 py-2 rounded-lg shadow-lg dark:bg-[#2a2f3a]">
           
            <a class="favorite-link" href="{% url 'library:favorite_quiz' quiz.pk %}">
            <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="text-black size-6 dark:text-white">
              <path stroke-linecap="round" stroke-linejoin="round" d="M11.48 3.499a.562.562 0 0 1 1.04 0l2.125 5.111a.563.563 0 0 0 .475.345l5.518.442c.499.04.701.663.321.988l-4.204 3.602a.563.563 0 0 0-.182.557l1.285 5.385a.562.562 0 0 1-.84.61l-4.725-2.885a.562.562 0 0 0-.586 0L6.982 20.54a.562.562 0 0 1-.84-.61l1.285-5.386a.562.562 0 0 0-.182-.557l-4.204-3.602a.562.562 0 0 1 .321-.988l5.518-.442a.563.563 0 0 0 .475-.345L11.48 3.5Z" />
            </svg>
          </a>
          </div>
          
          {% else %}
          <div class="absolute top-4 right-4 bg-white bg-opacity-60 text-white px-4 py-2 rounded-lg shadow-lg dark:bg-[#2a2f3a]">
          <a class="favorite-link" href="{% url 'library:favorite_quiz' quiz.pk %}">
            <svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="text-yellow-400 size-6">
              <path stroke-linecap="round" stroke-linejoin="round" d="M11.48 3.499a.562.562 0 0 1 1.04 0l2.125 5.111a.563.563 0 0 0 .475.345l5.518.442c.499.04.701.663.321.988l-4.204 3.602a.563.563 0 0 0-.182.557l1.285 5.385a.562.562 0 0 1-.84.61l-4.725-2.885a.562.562 0 0 0-.586 0L6.982 20.54a.562.562 0 0 1-.84-.61l1.285-5.386a.562.562 0 0 0-.182-.557l-4.204-3.602a.562.562 0 0 1 .321-.988l5.518-.442a.563.563 0 0 0 .475-.345L11.48 3.5Z" />
            </svg>
          </a>
          </div>
          {% endif %}
          {% endif %}
                    <div class="dark:bg-[#2a2f3a] max-w-6/10 h-max-12 truncate absolute top-4 left-4 bg-white bg-opacity-60 text-white px-4 py-2 rounded-lg shadow-lg ">
           
            <div  class="flex items-center gap-2">
          <span class="rounded-full w-4 h-4 flex-shrink-0" style="background-color: {{ quiz.category.color }};"></span>
           
              <a class=" text-gray-800 truncate text-sm dark:text-white" title="{{ quiz.category }}" href="{% url 'library:overview_quiz' %}?filter=1&categories={{ quiz.category.id }}">{{ quiz.category }}</a>
            </div>
          </div>




        </div>

        

        <!-- Content Section (Bottom) -->
        <div class="flex-grow bg-white p-4 flex flex-col gap-3 dark:bg-[#2a2f3a] dark:text-white">
          <!-- Title -->
          <h2 class="text-xl font-bold text-gray-900 break-words mb-2"> 
            <a href="{% url 'library:detail_quiz' quiz.id %}" class="hover:text-blue-600 transition-colors dark:text-white">{{ quiz.name }}</a>
          </h2>
          <!-- Description -->
          <p class="text-sm text-gray-700 break-words mb-3 dark:text-white">{{ quiz.description }}</p>

          <!-- Quiz Info Grid -->
          <div class="grid grid-cols-2 gap-x-4 gap-y-3 text-sm">
            <!-- Difficulty -->
            <div class="flex items-center gap-2">
              <svg class="w-4 h-4 text-blue-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 10V3L4 14h7v7l9-11h-7z" />
              </svg>
              <span class="text-gray-800 dark:text-white">{{ quiz.difficulty }}</span>
            </div>
            
            <!-- Questions Count -->
            <div class="flex items-center gap-2">
              <svg class="w-4 h-4 text-green-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8.228 9c.549-1.165 2.03-2 3.772-2 2.21 0 4 1.343 4 3 0 1.4-1.278 2.575-3.006 2.907-.542.104-.994.54-.994 1.093m0 3h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
              </svg>
              <span class="text-gray-800 dark:text-white">{{ quiz.questions.count }} Fragen</span>
            </div>
            
            <!-- Status -->
            <div class="flex items-center gap-2">
              <svg class="w-4 h-4 text-purple-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z" />
              </svg>
              <span class="text-gray-800 dark:text-white">{{ quiz.status }}</span>
            </div>
            
            <!-- Rating -->
            <div class="flex items-center gap-1">
              {% if quiz.rating_count > 0 %}
              {% for i in '12345'|make_list %}
                {% if forloop.counter <= quiz.average_rating|floatformat:0|add:0 %}
                  <span class="text-yellow-500 text-base">★</span>
                {% else %}
                  <span class="text-gray-300 text-base">★</span>
                {% endif %}
              {% endfor %}
                <span class="text-gray-600 text-sm dark:text-white">({{ quiz.rating_count }})</span>
              {% else %}
                <span class="text-gray-600 text-sm dark:text-white">Noch keine Bewertungen</span>
              {% endif %}
            </div>
          </div>

          <!-- Authors -->
           <!--
          {% if quiz.authors.all %}
          <div class="flex items-center gap-2 text-sm mt-2">
            <svg class="w-4 h-4 text-gray-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
              <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z" />
            </svg>
            <span class="text-gray-600">
              {% for author in quiz.authors.all %}
                {{ author.username }}{% if not forloop.last %}, {% endif %}
              {% endfor %}
            </span>
          </div>
          {% endif %}-->

                    <!-- Zeit-->
{% if quiz.published_at != None %}            
             
            <div class="flex items-center gap-2 ">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor"  class="text-green-400 size-4">
  <path stroke-linecap="round" stroke-linejoin="round" d="M12 6v6h4.5m4.5 0a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z" />
</svg>
<span class="text-gray-800 text-sm dark:text-white "><span class="font-medium dark:text-white">{{ quiz.published_at |naturaltime }}</span> <span class="text-sm">erst. veröffentlicht</span> </span>
</div>
{% endif %}

          <!-- Action Buttons -->
          <div class="flex justify-between items-center gap-2 mt-3 border-t pt-3">
            {% if quiz.questions.count != 0 %}
            <a href="{% url 'play:create_game' quiz.id %}" 
               class="px-3 py-1.5 bg-blue-600 hover:bg-blue-700 text-white text-sm font-medium rounded-lg transition-colors duration-200 flex items-center gap-2">
              <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M14.752 11.168l-3.197-2.132A1 1 0 0010 9.87v4.263a1 1 0 001.555.832l3.197-2.132a1 1 0 000-1.664z" />
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
              </svg>
              Spielen
            </a>
            {% else %}
           Fürs Spielen ist min. 1 Frage nötig!
            {% endif %}

            <div class="flex gap-1">
              <a href="{% url 'library:detail_quiz' quiz.id %}" 
                 class="p-1.5 bg-gray-100 hover:bg-gray-200 text-gray-700 rounded-lg transition-colors duration-200 dark:bg-transparent dark:text-white dark:hover:bg-gray-700">
                <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
                  <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z" />
                </svg>
              </a>
              {% if quiz.user_id == request.user %}
              <a href="{% url 'library:edit_quiz' quiz.id %}" 
                 class="p-1.5 bg-gray-100 hover:bg-gray-200 text-gray-700 rounded-lg transition-colors duration-200 dark:bg-transparent dark:text-white dark:hover:bg-gray-700">
                <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z" />
                </svg>
              </a>
              <a href="{% url 'library:delete_quiz' quiz.id %}" 
                 class="p-1.5 bg-red-100 hover:bg-red-200 text-red-700 rounded-lg transition-colors duration-200 dark:bg-transparent dark:text-white dark:hover:bg-gray-700">
                <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" />
                </svg>
              </a>
              {% endif %}
            </div>
          </div>
          </div>
    </article>
    {% endfor %}
    
  </div>
  
  <!-- Pagination für favorite Quizze -->
  {% if favorite_quizzes.paginator.num_pages > 1 %}
  <div class="flex justify-center space-x-2 mt-6 mb-8 ">
    {% if favorite_quizzes.has_previous %}
    <a href="?favorite_page={{ favorite.previous_page_number }}&{{ querystring_favorite }}" 
       class="px-3 py-1 bg-gray-100 text-gray-700 hover:bg-gray-200 rounded-lg transition-colors">
      <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7" />
      </svg>
    </a>
    {% endif %}
    
    <span class="dark:text-white px-3 py-1 text-gray-600">Seite {{ favorite_quizzes.number }} von {{ favorite_quizzes.paginator.num_pages }}</span>
    
    {% if favorite_quizzes.has_next %}
    <a href="?favorite_page={{ favorite_quizzes.next_page_number }}&{{ querystring_favorite }}" 
       class="px-3 py-1 bg-gray-100 text-gray-700 hover:bg-gray-200 rounded-lg transition-colors">
      <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7" />
      </svg>
    </a>
    {% endif %}
  </div>
  {% endif %}
</div>
{% else %}
<div class="container mx-auto px-4 sm:px-6 lg:px-8 mt-4">
  <p class="text-gray-600 text-center  dark:text-white">Keine favorisierten Quiz gefunden.</p>
</div>
{% endif %}<script>
  document.addEventListener('DOMContentLoaded', function () {
    const filterPanel = document.getElementById('filterPanel');
    const filterButton = document.getElementById('filterButton');

    const urlParams = new URLSearchParams(window.location.search);
    if (urlParams.get('filter') === '1') {
      filterPanel.classList.add('show');

    }

    

    filterButton.addEventListener('click', function () {
      filterPanel.classList.toggle('show');

      const params = new URLSearchParams(window.location.search);
      if (filterPanel.classList.contains('show')) {
        //params.set('filter', '1');
      } else {
        params.delete('filter');
      }

      history.replaceState(null, '', `${location.pathname}?${params}`);
    });
  });


  document.addEventListener("DOMContentLoaded", function () {
  // Scroll-Position beim Laden setzen
  const scrollPos = localStorage.getItem("scroll-position");
  if (scrollPos !== null) {
    window.scrollTo(0, parseInt(scrollPos));
    localStorage.removeItem("scroll-position");
  }

  // Nur Favoriten-Links abfangen
  const favoriteLinks = document.querySelectorAll('a.favorite-link');
  favoriteLinks.forEach(link => {
    link.addEventListener('click', function () {
      localStorage.setItem("scroll-position", window.scrollY);
    });
  });
});

</script>

{% endblock content %}
