import tkinter as tk
import json

# Load flashcards from JSON file
def load_flashcards():
    with open('flashcards.json', 'r') as f:
        return json.load(f)["cards"]

# Initialize GUI window
window = tk.Tk()
window.title("Customizable Flashcard System")
window.geometry("400x500")

flashcards = load_flashcards()
current_card = 0

# Display first question
question_label = tk.Label(window, text=flashcards[current_card]['question'], font=('Helvetica', 16))
question_label.pack(pady=20)

# Answer entry box
answer_entry = tk.Entry(window, font=('Helvetica', 14))
answer_entry.pack(pady=20)

# Function to check answer
def check_answer():
    answer = answer_entry.get()
    if answer.lower() == flashcards[current_card]['answer'].lower():
        result_label.config(text="Correct!", fg="green")
    else:
        result_label.config(text=f"Wrong! The correct answer is {flashcards[current_card]['answer']}", fg="red")

# Button to check answer
check_button = tk.Button(window, text="Check Answer", command=check_answer)
check_button.pack(pady=10)

# Result display
result_label = tk.Label(window, text="", font=('Helvetica', 14))
result_label.pack(pady=20)

# Function to move to next flashcard
def next_flashcard():
    global current_card
    current_card += 1
    if current_card >= len(flashcards):
        current_card = 0
    question_label.config(text=flashcards[current_card]['question'])
    answer_entry.delete(0, tk.END)
    result_label.config(text="")

# Button to move to next flashcard
next_button = tk.Button(window, text="Next", command=next_flashcard)
next_button.pack(pady=10)

# Function to add new flashcard
def add_flashcard():
    new_question = new_question_entry.get()
    new_answer = new_answer_entry.get()
    if new_question and new_answer:
        flashcards.append({"question": new_question, "answer": new_answer})
        with open('flashcards.json', 'w') as f:
            json.dump({"cards": flashcards}, f, indent=4)
        result_label.config(text="Flashcard Added!", fg="green")

# Input for new question and answer
new_question_label = tk.Label(window, text="New Question:", font=('Helvetica', 12))
new_question_label.pack(pady=10)
new_question_entry = tk.Entry(window, font=('Helvetica', 12))
new_question_entry.pack(pady=5)

new_answer_label = tk.Label(window, text="New Answer:", font=('Helvetica', 12))
new_answer_label.pack(pady=10)
new_answer_entry = tk.Entry(window, font=('Helvetica', 12))
new_answer_entry.pack(pady=5)

# Button to add flashcard
add_button = tk.Button(window, text="Add Flashcard", command=add_flashcard)
add_button.pack(pady=10)

window.mainloop()
