import tkinter as tk
import json

class TaskListApp:
    def __init__(self, root):
        self.root = root
        self.root.title("Task List App")

       
        self.tasks = []

        
        self.load_tasks()

        
        self.task_entry = tk.Entry(root)
        self.task_entry.pack()

        self.add_button = tk.Button(root, text="Add Task", command=self.add_task)
        self.add_button.pack()

        self.task_listbox = tk.Listbox(root)
        self.task_listbox.pack()

        self.complete_button = tk.Button(root, text="Mark as Completed", command=self.mark_completed)
        self.complete_button.pack()

    
        self.update_task_listbox()

    def add_task(self):
        task = self.task_entry.get()
        if task:
            self.tasks.append({"task": task, "completed": False})
            self.task_entry.delete(0, tk.END)
            self.update_task_listbox()
            self.save_tasks()

    def mark_completed(self):
        selected_index = self.task_listbox.curselection()
        if selected_index:
            index = selected_index[0]
            self.tasks[index]["completed"] = True
            self.update_task_listbox()
            self.save_tasks()

    def update_task_listbox(self):
        self.task_listbox.delete(0, tk.END)
        for task in self.tasks:
            status = "✓" if task["completed"] else " "
            self.task_listbox.insert(tk.END, f"{status} {task['task']}")

    def save_tasks(self):
        with open("tasks.json", "w") as f:
            json.dump(self.tasks, f)

    def load_tasks(self):
        try:
            with open("tasks.json", "r") as f:
                self.tasks = json.load(f)
        except FileNotFoundError:
            pass

if __name__ == "__main__":
    root = tk.Tk()
    app = TaskListApp(root)
    root.mainloop()

#-------------------------------------------------------------------------------
    # هذا الجزء لملف tasks.json 
    #اتمنى وضعه في ملف اخر لان الارفاق لا يسمح لي الا بارفاق ملف واحد فقط وشكرا


    #      [{"task": "clean", "completed": true}]