import genanki
import csv

my_model = genanki.Model(
  1607340962,
  'Zentriertes Fragen-Modell',
  fields=[
    {'name': 'Frage'},
    {'name': 'Antwort'}
  ],
  templates=[
    {
      'name': 'Card 1',
      'qfmt': '{{Frage}}',
      'afmt': """
        <div style="text-align: center;">
          {{Frage}}<br>
          <input type="text" id="user_input" style="text-align: center;">
          <button onclick="checkAnswer()">Überprüfen</button>
          <div id="feedback"></div>
          <div id="answer" style="display: none;">{{Antwort}}</div>
        </div>
        <script>
          function checkAnswer() {
            var userInput = document.getElementById('user_input').value;
            var correctAnswer = '{{Antwort}}';
            var feedback = document.getElementById('feedback');
            if (userInput === correctAnswer) {
              feedback.textContent = "Richtig!";
            } else {
              feedback.textContent = "Falsch. Die korrekte Antwort ist: " + correctAnswer;
            }
            document.getElementById('answer').style.display = 'block';
          }
        </script>
      """,
    },
  ])

my_deck = genanki.Deck(
  2059400312,
  'Langzeit Abfragen')

with open('LZ.csv', 'r') as csvfile:
  reader = csv.reader(csvfile,delimiter=';')
  for row in reader:
    question, answer = row
    my_note = genanki.Note(
      model=my_model,
      fields=[question, answer])
    my_deck.add_note(my_note)

package = genanki.Package(my_deck)
package.write_to_file('LZ_Eingabe.apkg')

print("Anki-Stapel erfolgreich erstellt!")