import tkinter as tk
from tkinter import messagebox
from tkinter import ttk  # Import ttk for themed widgets
import os

class Task:
    def __init__(self, text):
        self.text = text
        self.completed = False


tasks = []  # Initialize list to store tasks


def add_task():
    task_text = textbox.get().strip()
    if not task_text:
        messagebox.showerror("Error", "Please enter a task")
        return

    # Create a task object
    task = Task(task_text)
    tasks.append(task)

    # Create frame for checkbox and label
    frame = tk.Frame(listbox)
    frame.pack(fill=tk.X)

    # Create and pack checkbox
    checkbox = ttk.Checkbutton(frame, variable=tk.IntVar(), command=lambda index=len(tasks) - 1: handle_checkbox_change(index))
    checkbox.pack(side=tk.LEFT)

    # Create and pack label
    label = tk.Label(frame, text=task_text)
    label.pack(side=tk.LEFT)
    
    # Clear the textbox only
    textbox.delete(0, tk.END)


def handle_checkbox_change(index):
    try:
        task = tasks[index]
        task.completed = checkbox_vars[index].get()
        
        
        
        # Update font based on checkbox state
        if task.completed:
            task.label = "Strikethrough \u0336python\u0336"
        else:
            task.label = ""
            
    except IndexError:
        # Handle the error gracefully, e.g., print a message or ignore the error
        pass
        
    
def clear_all_tasks():
    # Clear tasks list, listbox, and checkbox variables
    global tasks, checkbox_vars
    tasks.clear()
    listbox.delete(0, tk.END)
    checkbox_vars.clear()

    save_tasks()  # Save an empty tasks list to file


def save_tasks():
    with open("tasks.txt", "w") as file:
        for task in tasks:
            file.write(f"{task.text},{int(task.completed)}\n")


def load_tasks():
    global tasks

    if os.path.exists("tasks.txt"):
        try:
            with open("tasks.txt", "r") as file:
                for line in file:
                    task_text, completed = line.strip().split(",")
                    task = Task(task_text)
                    task.completed = bool(int(completed))
                    tasks.append(task)
        except (FileNotFoundError, ValueError):
            pass


def display_tasks():
    listbox.delete(0, tk.END)  # Clear the listbox
    checkbox_vars.clear()

    for index, task in enumerate(tasks):
        # Create frame for checkbox and label
        frame = tk.Frame(listbox)
        frame.pack(fill=tk.X)

        # Create and pack checkbox
        checkbox = ttk.Checkbutton(frame, variable=tk.IntVar(), command=lambda index=index: handle_checkbox_change(index))
        checkbox.pack(side=tk.LEFT)

        # Create and pack label
        label = tk.Label(frame, text=task.text)
        label.pack(side=tk.LEFT)

        task.label = label  # Store reference to the label

        # Update checkbox state and font based on task object
        checkbox_var = tk.IntVar()
        checkbox_var.set(task.completed)
        checkbox_vars.append(checkbox_var)
        checkbox.config(variable=checkbox_var)

        if task.completed:
            label.config(font=("Arial", 12, "overstrike"), fg="gray")
        else:
            label.config(font=("Arial", 12), fg="black")
    

    
    
    
# Create the main window
root = tk.Tk()

# Create widgets
textbox = tk.Entry(root)
listbox = tk.Listbox(root)
checkbox_vars = []  # List to store checkbox variables
button = tk.Button(root, text="Add", command=add_task)
button_clear = tk.Button(root, text="Clear", command=clear_all_tasks)


# Pack widgets
textbox.pack()
button.pack()
listbox.pack()
button_clear.pack(side=tk.LEFT)


root.mainloop()
