import tkinter as tk
from tkinter import simpledialog
from tkinter import messagebox
import os

class NoteTakingApp:
    def __init__(self, root):
        self.root = root
        self.root.title("Note Taking App")
        
        self.notes = {}
        
        self.create_widgets()
        
        self.load_notes()

    def create_widgets(self):
        self.listbox = tk.Listbox(self.root, width=50, height=20)
        self.listbox.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
        
        self.scrollbar = tk.Scrollbar(self.root, orient="vertical")
        self.scrollbar.config(command=self.listbox.yview)
        self.scrollbar.pack(side=tk.LEFT, fill=tk.Y)
        self.listbox.config(yscrollcommand=self.scrollbar.set)
        
        self.text_area = tk.Text(self.root, width=50, height=20)
        self.text_area.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True)
        
        self.new_button = tk.Button(self.root, text="New Note", command=self.new_note)
        self.new_button.pack(side=tk.TOP, fill=tk.X)
        
        self.save_button = tk.Button(self.root, text="Save Note", command=self.save_note)
        self.save_button.pack(side=tk.TOP, fill=tk.X)
        
        self.delete_button = tk.Button(self.root, text="Delete Note", command=self.delete_note)
        self.delete_button.pack(side=tk.TOP, fill=tk.X)
        
        self.listbox.bind('<<ListboxSelect>>', self.show_note_content)

    def new_note(self):

        title = simpledialog.askstring("Note Title", "Enter the title of the note:")
        if title:
            self.notes[title] = ""
            self.listbox.insert(tk.END, title)
            self.text_area.delete('1.0', tk.END)

    def save_note(self):

        try:
            index = self.listbox.curselection()[0]
            title = self.listbox.get(index)

            self.notes[title] = self.text_area.get('1.0', tk.END).strip()
            self.save_notes_to_file()
        except IndexError:
            messagebox.showerror("Error", "Please select a note to save.")

    def delete_note(self):

        try:
            index = self.listbox.curselection()[0]
            title = self.listbox.get(index)

            del self.notes[title]
            self.listbox.delete(index)
            self.text_area.delete('1.0', tk.END)
            self.save_notes_to_file()
        except IndexError:
            messagebox.showerror("Error", "Please select a note to delete.")

    def show_note_content(self, event):

        try:
            index = self.listbox.curselection()[0]
            title = self.listbox.get(index)
            # Display the note content
            self.text_area.delete('1.0', tk.END)
            self.text_area.insert(tk.END, self.notes[title])
        except IndexError:
            pass

    def load_notes(self):
        if os.path.exists("notes.txt"):
            with open("notes.txt", "r") as file:
                for line in file:
                    title, content = line.strip().split(":", 1)
                    self.notes[title] = content
                    self.listbox.insert(tk.END, title)

    def save_notes_to_file(self):
        with open("notes.txt", "w") as file:
            for title, content in self.notes.items():
                file.write(f"{title}:{content}\n")


root = tk.Tk()
app = NoteTakingApp(root)


root.mainloop()
