{% extends 'base.html' %}

{% block content %}

<div class="container mx-auto px-4">
    <div class="flex flex-wrap space-y-2 items-center justify-between mb-6 dark:text-white">
        <h1 class="text-2xl font-bold mr-4 dark:text-white ">{{ quiz.name }}</h1> 
     
        <div class=" flex flex-wrap space-x-2   ">
           
            <div class="flex flex-wrap">
            {% if quiz.user_id == request.user %}
            <a href="{% url 'library:edit_quiz' quiz.id %}" class=" text-white px-2 py-2 rounded-md text-sm lg:text-lg hover:scale-110 transition-colors">
                <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-7 text-gray-600 dark:text-white">
                    <path stroke-linecap="round" stroke-linejoin="round" d="m16.862 4.487 1.687-1.688a1.875 1.875 0 1 1 2.652 2.652L10.582 16.07a4.5 4.5 0 0 1-1.897 1.13L6 18l.8-2.685a4.5 4.5 0 0 1 1.13-1.897l8.932-8.931Zm0 0L19.5 7.125M18 14v4.75A2.25 2.25 0 0 1 15.75 21H5.25A2.25 2.25 0 0 1 3 18.75V8.25A2.25 2.25 0 0 1 5.25 6H10" />
                  </svg>
                  
            </a>
            <a href="{% url 'library:delete_quiz' quiz.id %}" class=" text-white px-2 py-2 rounded-md text-sm lg:text-lg hover:scale-110 transition-colors">
                <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-7 text-gray-600 dark:text-white">
                    <path stroke-linecap="round" stroke-linejoin="round" d="m14.74 9-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 0 1-2.244 2.077H8.084a2.25 2.25 0 0 1-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 0 0-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 0 1 3.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 0 0-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 0 0-7.5 0" />
                  </svg>
            </a>
            {% endif %}
        </div>
           
            <div class="flex flex-wrap gap-2">
                <a href="{% url 'library:copy_quiz' quiz.id %}"
                   class="inline-flex items-center gap-2 px-4 py-2 bg-blue-600 text-white text-sm font-medium rounded-lg hover:bg-blue-700 transition-colors duration-200">
                  <svg xmlns="http://www.w3.org/2000/svg" class="w-5 h-5" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
                    <path stroke-linecap="round" stroke-linejoin="round"
                      d="M11.35 3.836c-.065.21-.1.433-.1.664 0 .414.336.75.75.75h4.5a.75.75 0 0 0 .75-.75 2.25 2.25 0 0 0-.1-.664m-5.8 0A2.251 2.251 0 0 1 13.5 2.25H15c1.012 0 1.867.668 2.15 1.586m-5.8 0c-.376.023-.75.05-1.124.08C9.095 4.01 8.25 4.973 8.25 6.108V8.25m8.9-4.414c.376.023.75.05 1.124.08 1.131.094 1.976 1.057 1.976 2.192V16.5A2.25 2.25 0 0 1 18 18.75h-2.25m-7.5-10.5H4.875c-.621 0-1.125.504-1.125 1.125v11.25c0 .621.504 1.125 1.125 1.125h9.75c.621 0 1.125-.504 1.125-1.125V18.75m-7.5-10.5h6.375c.621 0 1.125.504 1.125 1.125v9.375m-8.25-3 1.5 1.5 3-3.75" />
                  </svg>
                  Quiz kopieren
                </a>
            
                {% if quiz.questions.count != 0 %}
                <a href="{% url 'play:create_game' quiz.id %}"
                   class="inline-flex items-center gap-2 px-4 py-2 bg-blue-600 text-white text-sm font-medium rounded-lg hover:bg-blue-700 transition-colors duration-200">
                  <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="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>           
                {% endif %}
              </div>
            </div>
          
            
       
    </div>
  
    {% if quiz.description %}
    <p class="text-gray-600 mb-4 dark:text-gray-400">{{ quiz.description }}</p>
    {% endif %}
    <div class="flex justify-end mb-4">
    {% if detail == True %}
    
<a href="{% url 'library:detail_quiz' quiz.id %}?show_answers=false">
    <div class="flex items-center mr-2 dark:text-white">
    <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-6">
        <path stroke-linecap="round" stroke-linejoin="round" d="M3.98 8.223A10.477 10.477 0 0 0 1.934 12C3.226 16.338 7.244 19.5 12 19.5c.993 0 1.953-.138 2.863-.395M6.228 6.228A10.451 10.451 0 0 1 12 4.5c4.756 0 8.773 3.162 10.065 7.498a10.522 10.522 0 0 1-4.293 5.774M6.228 6.228 3 3m3.228 3.228 3.65 3.65m7.894 7.894L21 21m-3.228-3.228-3.65-3.65m0 0a3 3 0 1 0-4.243-4.243m4.242 4.242L9.88 9.88" />
      </svg>
      &nbsp; Antworten verstecken
    </div>
      
</a>
{% else %}

<a href="{% url 'library:detail_quiz' quiz.id %}?show_answers=true">
    <div class="flex items-center mr-2 dark:text-white">
    <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-6">
    <path stroke-linecap="round" stroke-linejoin="round" d="M2.036 12.322a1.012 1.012 0 0 1 0-.639C3.423 7.51 7.36 4.5 12 4.5c4.638 0 8.573 3.007 9.963 7.178.07.207.07.431 0 .639C20.577 16.49 16.64 19.5 12 19.5c-4.638 0-8.573-3.007-9.963-7.178Z" />
    <path stroke-linecap="round" stroke-linejoin="round" d="M15 12a3 3 0 1 1-6 0 3 3 0 0 1 6 0Z" /> 
  </svg>
  &nbsp; Antworten anzeigen
</div>

</a>

{% endif %}
</div> 


    <div class="bg-white rounded-lg shadow-md border-blue-600 border-2 rounded-xl shadow-md dark:!bg-transparent dark:text-white">
        <div class="border-b border-gray-200 p-4">
            <div class="flex justify-between items-center flex-wrap">
                <h2 class="text-xl font-semibold">Fragen
                    {% for question in questions %}{% if forloop.last %}({{ forloop.counter }}) {% endif %} {% endfor %}
                </h2>
                <div class="flex space-x-2 flex-wrap">
                    {% if quiz.user_id == request.user %}
                    <a href="{% url 'library:new_question' %}?type=input&quiz_id={{ quiz.id }}" 
                       class=" mt-1 bg-green-100 text-green-800 px-4 py-2 rounded-md text-xs lg:text-lg  hover:border-blue-600 border-2">
                        Eingabe
                    </a>
                    
                    <a href="{% url 'library:new_question' %}?type=multiple_choice&quiz_id={{ quiz.id }}" 
                       class=" mt-1 bg-blue-100 text-blue-800 px-4 py-2 rounded-md text-xs lg:text-lg hover:border-blue-600 border-2">
                        Multiple Choice
                    </a>

                    <a href="{% url 'library:new_question' %}?type=true_false&quiz_id={{ quiz.id }}" 
                       class=" mt-1 bg-purple-100 text-purple-800 px-4 py-2 rounded-md text-xs lg:text-lg  hover:border-blue-600 border-2">
                        Wahr/Falsch
                    </a>
                                      
                    <a href="{% url 'library:new_question' %}?type=order&quiz_id={{ quiz.id }}" 
                       class=" mt-1 bg-yellow-100 text-yellow-800 px-4 py-2 rounded-md text-xs lg:text-lg  hover:border-blue-600 border-2">
                        Reihenfolge
                    </a>

                    <a href="{% url 'library:new_question' %}?type=guess&quiz_id={{ quiz.id }}" 
                       class=" mt-1 bg-pink-100 text-pink-800 px-4 py-2 rounded-md text-xs lg:text-lg  hover:border-blue-600 border-2">
                        Schätzen
                    </a>

                    <a href="{% url 'library:new_question' %}?type=map&quiz_id={{ quiz.id }}" 
                       class=" mt-1 bg-orange-100 text-orange-800 px-4 py-2 rounded-md text-xs lg:text-lg  hover:border-blue-600 border-2">
                        Karte
                    </a>
                    {% endif %}
                </div>
            </div>
        </div>

        {% if questions %}
       
            <div class="divide-y divide-gray-200">
                {% for question in questions %}
                

                
                    <div class="w-full rounded-xl p-4 hover:bg-gray-50 dark:hover:bg-[#2a2f3a] dark:text-white">
                        <p>{{ forloop.counter }}.</p>
                        {% if quiz.user_id == request.user %} <a class="block flex h-full w-full" href="{% url 'library:edit_question' question.id %}" > {% endif %}
                        <div class="flex justify-between items-start">
                            
                            <div class="flex-grow">
                                <p class="font-medium">{{ question.data.question }}</p>
                                <div class="mt-1">
                                    <span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium {% if question.data.type == 'multiple_choice' %}bg-blue-100 text-blue-800 {% elif question.data.type == 'input' %} bg-green-100 text-green-800 {% elif question.data.type == 'map' %}bg-orange-100 text-orange-800 {% elif question.data.type == 'true_false' %}bg-purple-100 text-purple-800{% elif question.data.type == 'order' %}bg-yellow-100 text-yellow-800{% else %}bg-pink-100 text-pink-800 {% endif %}">
                                        {% if question.data.type == 'multiple_choice' %}Multiple Choice
                                        {% elif question.data.type == 'input' %}Eingabe
                                        {% elif question.data.type == 'map' %}Karte
                                        {% elif question.data.type == 'true_false' %}Wahr/Falsch
                                        {% elif question.data.type == 'order' %}Reihenfolge
                                        {% else %}Schätzen
                                        {% endif %}
                                    </span>
                                </div>
                                <div class="mt-2">
                                    {% if detail %}

                                    {% if question.data.type == 'multiple_choice' %}
                                    <div class="w-full gap-4 md:flex justify-between ">
                                    <ul class="space-y-1 ">
                                          {% for option in question.data.options|dictsort:"order" %}
                                      
                                            
                                            <li class="flex items-center text-sm ">
                                               
                                                {% if option.is_correct %}
                                                    <svg class="h-4 w-4 text-green-500 mr-1.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"/>
                                                    </svg>
                                                    <span class="font-medium text-green-700">{{ option.value }}</span>
                                                {% else %}
                                                    <svg class="h-4 w-4 text-gray-400 mr-1.5 dark:text-white " fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M18 12H6"/>
                                                    </svg>
                                                    <span class="text-gray-600 dark:text-white">{{ option.value }}</span>
                                                {% endif %}
                                            </li>
                                        {% endfor %}                           
                                    </ul>
                                       {% if question.image.image.url %}
                                    <img src="{{ question.image.image.url }}" alt="Bild der Frage" class="w-[200px] rounded-lg shadow-md border-blue-600 border-2 rounded-xl shadow-md object-contain mt-4 md:mt-0" />
                                    {% endif %}
                                </div>  
                                
                                {% elif question.data.type == 'map' and detail %}
                                <div class="mt-3">
                                    <div class="text-sm text-gray-600 dark:text-gray-300">
                                        <span class="font-medium">Zielort:</span> 
                                        <span id="target-location-{{ forloop.counter }}">
                                            {% if question.data.target_location %}
                                                {{ question.data.target_location }}
                                            {% else %}
                                                Kein Zielort festgelegt
                                            {% endif %}
                                        </span>
                                    </div>
                                    {% if question.data.target_location %}
                                    <script>
                                        // Format the coordinates to 4 decimal places
                                        document.addEventListener('DOMContentLoaded', function() {
                                            const targetEl = document.getElementById('target-location-{{ forloop.counter }}');
                                            if (targetEl) {
                                                const coords = targetEl.textContent.trim().split(',');
                                                if (coords.length === 2) {
                                                    const lat = parseFloat(coords[0]).toFixed(4);
                                                    const lng = parseFloat(coords[1]).toFixed(4);
                                                    targetEl.textContent = `${lat}, ${lng}`;
                                                }
                                            }
                                        });
                                    </script>
                                    {% endif %}
                                    {% if question.data.tolerance_radius %}
                                    <div class="text-sm text-gray-600 dark:text-gray-300 mt-1">
                                        <span class="font-medium">Toleranzradius:</span> {{ question.data.tolerance_radius }} Meter
                                    </div>
                                    {% endif %}
                                    {% if question.image.image.url %}
                                    <img src="{{ question.image.image.url }}" alt="Bild der Frage" class="w-[200px] rounded-lg shadow-md border-blue-600 border-2 rounded-xl shadow-md object-contain mt-4" />
                                    {% endif %}
                                </div>
                                {% elif question.data.type == 'order' %}
                                <div class="w-full gap-4 md:flex justify-between "> 
                                    <ul class="space-y-1 ">
                                        {% for option in question.data.options|dictsort:"order" %}
                                            <li class="flex items-center text-sm">
                                           {{ forloop.counter }}. &nbsp;
                                                    <span class="font-medium text-green-700 dark:text-white">{{ option.value }}</span>
                                            </li>
                                        {% endfor %}
                                    </ul>
                                    {% if question.image.image.url %}
                                    <img src="{{ question.image.image.url }}" alt="Bild der Frage" class="w-[200px] rounded-lg shadow-md border-blue-600 border-2 rounded-xl shadow-md object-contain mt-4 md:mt-0" />
                                    {% endif %}
                                </div> 
                                
                                    {% elif question.data.type == 'input' %}
                                    <div class="w-full gap-4 md:flex justify-between ">    
                                    <ul class="space-y-1 ">
                                        {% for option in question.data.options %}
                                            <li class="flex items-center text-sm">
                                                  <svg class="h-4 w-4 text-green-500 mr-1.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"/>
                                                    </svg>
                                                    <span class="font-medium text-green-700 dark:text-white">{{ option.value }}</span>
                                            </li>
                                        {% endfor %}
                                    </ul>
                                    {% if question.image.image.url %}
                                    <img src="{{ question.image.image.url }}" alt="Bild der Frage" class="w-[200px] rounded-lg shadow-md border-blue-600 border-2 rounded-xl shadow-md object-contain mt-4 md:mt-0" />
                                    {% endif %}
                                </div> 
                                       {% elif question.data.type == 'guess' %}
                                    <div class="w-full gap-4 md:flex justify-between ">    
                                    <ul class="space-y-1 ">
                                        {% for option in question.data.options %}
                                            <li class="flex items-center text-sm">
                                                  <svg class="h-4 w-4 text-green-500 mr-1.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"/>
                                                    </svg>
                                                    <span class="font-medium text-green-700 dark:text-white">{{ option.value }}</span>
                                            </li>
                                        {% endfor %}
                                    </ul>
                                    {% if question.image.image.url %}
                                    <img src="{{ question.image.image.url }}" alt="Bild der Frage" class="w-[200px] rounded-lg shadow-md border-blue-600 border-2 rounded-xl shadow-md object-contain mt-4 md:mt-0" />
                                    {% endif %}
                                </div> 
                                {% else %}
                                <div class="w-full gap-4 md:flex justify-between ">                  
                                {% for option in question.data.options %}
                                    {% if option.is_correct %}
                                    <p class="text-sm">
                                    <span class="text-gray-500 dark:text-white">Richtige Antwort:</span>
                                    <span class="ml-1 font-medium {% if option.value == "Wahr" %} text-green-700 {% else %}text-red-700{% endif %}">{{ option.value }}</span>
                                </p>
                                    {% endif %}
                            {% endfor %}
                                    {% if question.image.image.url %}
                                    <img src="{{ question.image.image.url }}" alt="Bild der Frage" class="w-[200px] rounded-lg shadow-md border-blue-600 border-2 rounded-xl shadow-md object-contain mt-4 md:mt-0" />
                                    {% endif %}
                                    </div>
                            
                                {% endif %}


                                    {% endif %}

                                </div>
                            </div>
                            
                            <div class="flex h-full space-x-2 ml-4 items-center">

                                <a href=" {% url 'library:delete_question' question.id %}" 
                class="  text-red-700 px-4 py-1 rounded hover:scale-110 ">
                {% if quiz.user_id == request.user %}
                <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="dark:text-white size-7 text-gray-600">
                    <path stroke-linecap="round" stroke-linejoin="round" d="m14.74 9-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 0 1-2.244 2.077H8.084a2.25 2.25 0 0 1-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 0 0-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 0 1 3.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 0 0-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 0 0-7.5 0" />
                  </svg>
                  {% endif %}
                  </a>
                            
                            </div>
                        </div>
                    </div>
                </a>
             
                {% endfor %}
            </div>
      
        {% else %}
   
            <div class="p-8 text-center">
                <svg class="mx-auto h-12 w-12 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"/>
                </svg>
                <p class="mt-2 text-sm text-gray-500">Noch keine Fragen vorhanden. Erstellen Sie eine neue Frage, um loszulegen!</p>
            </div>
        {% endif %}
    </div>

    <div class="flex justify-end">   
        {% if quiz.base_quiz %}
        <p class="text-gray-600 mt-2 dark:text-white">Bearbeitung:<span class="text-blue-600 font-bold"> <a href="{% url 'library:overview_quiz' %}?filter=1&user={{ quiz.user_id }}">{{ quiz.user_id }}</a></span></p>
        {% else %}
        <p class="text-gray-600 mt-2 dark:text-white">Autor:<span class="text-blue-600 font-bold"> <a href="{% url 'library:overview_quiz' %}?filter=1&user={{ quiz.user_id }}">{{ quiz.user_id }}</a></span></p>
        {% endif %}
    </div>

<div class="flex justify-end">   
{% if quiz.base_quiz %}
<p class="text-gray-600 dark:text-white ">Kopie von: <a href="{% url 'library:detail_quiz' quiz.base_quiz.id %}"><span class="text-blue-600 font-bold">{{ quiz.base_quiz}}</span></a></p>
{% endif %}
</div>
<div class="flex justify-end">  
    {% if quiz.base_quiz %}
    <p class="text-gray-600 dark:text-white">Autor:<span class="text-blue-600 font-bold"> <a href="{% url 'library:overview_quiz' %}?filter=1&user={{ quiz.base_quiz.user_id }}">{{ quiz.base_quiz.user_id }}</a></span></p>
    {% endif %}
</div>

<!-- Links müssen wieder ergänzt werden!
 {% if questions %}
<div class="flex flex-col sm:flex-row gap-4 items-start sm:items-center my-6">

    

  <a href="" 
     target="_blank"
     class=" inline-flex items-center gap-2 px-2 py-2 rounded-lg bg-blue-600 hover:bg-blue-700 text-white shadow-md transition">
    <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none"
         viewBox="0 0 24 24" stroke="currentColor">
      <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
            d="M12 4v16m8-8H4" />
    </svg>
    als Aufgabenblatt herunterladen (PDF)
  </a>
 
 
  <a href=""
     target="_blank"
     class="inline-flex items-center gap-2 px-2 py-2 rounded-lg bg-green-600 hover:bg-green-700 text-white  shadow-md transition">
    <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none"
         viewBox="0 0 24 24" stroke="currentColor">
      <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
            d="M12 4v16m8-8H4" />
    </svg>
    Lösungen herunterladen (PDF)
  </a>

 

</div>
   {% endif %}
   -->
<div class="flex flex-col max-w-full">
     <p class="text-gray-600 mt-2 dark:text-white ">Credits: </p>
    {% if quiz.credits %}
        <p class="text-blue-500 mb-2 w-full break-words dark:text-gray-400  ">
            <span class="break-words italic">{{ quiz.credits }}</span>
            <span class="font-normal break-words text-gray-600 dark:text-white">(Credits für das Quiz)</span>
        </p>
        {% else %}
        <p class="text-gray-500 dark:text-gray-400 ">Keine Credits für das Quizformular vorhanden!</p>
    {% endif %}
    
</div>



<div class="flex flex-col max-w-full">
    <ul class="space-y-2">
        {% regroup questions by credits as grouped_credits %}
        {% if grouped_credits|length == 1 and not grouped_credits.0.grouper %}
            <li class="text-gray-500 dark:text-gray-400 ">Keine Credits für die Fragen vorhanden!</li>
        {% else %}
            {% for question in questions %}
                {% if question.credits %}
                    <li class="text-blue-500 mb-2 w-full break-words  break-words dark:text-gray-400 ">
                        <span class="italic">{{ question.credits }}</span>
                        <span class="font-normal break-words text-gray-600 dark:text-white">(Frage: {{ forloop.counter }})</span>
                    </li>
                {% endif %}
            {% endfor %}
        {% endif %}
    </ul>
</div>

</div>

{% endblock %}
