import tkinter as tk
from tkinter import messagebox, simpledialog

class NoteTakingApp:
    def __init__(self, root):
        self.root = root
        self.root.title("Note Taking App")
        self.notes = {}
        self.load_notes()

        self.note_listbox = tk.Listbox(root, width=50)
        self.note_listbox.pack(pady=10)

        self.new_button = tk.Button(root, text="New Note", command=self.create_note)
        self.new_button.pack()

        self.edit_button = tk.Button(root, text="Edit Note", command=self.edit_note)
        self.edit_button.pack()

        self.delete_button = tk.Button(root, text="Delete Note", command=self.delete_note)
        self.delete_button.pack()

        self.search_button = tk.Button(root, text="Search Notes", command=self.search_notes)
        self.search_button.pack()

        self.save_notes()

    def load_notes(self):
        try:
            with open("notes.txt", "r") as file:
                for line in file:
                    title, content = line.strip().split(":")
                    self.notes[title] = content
        except FileNotFoundError:
            pass

    def save_notes(self):
        with open("notes.txt", "w") as file:
            for title, content in self.notes.items():
                file.write(f"{title}:{content}\n")

    def create_note(self):
        title = simpledialog.askstring("New Note", "Enter note title:")
        if title:
            content = simpledialog.askstring("New Note", "Enter note content:")
            if content:
                self.notes[title] = content
                self.update_note_list()

    def edit_note(self):
        selected_index = self.note_listbox.curselection()
        if selected_index:
            title = self.note_listbox.get(selected_index)
            content = simpledialog.askstring("Edit Note", f"Edit content for '{title}':", initialvalue=self.notes[title])
            if content:
                self.notes[title] = content
                self.update_note_list()

    def delete_note(self):
        selected_index = self.note_listbox.curselection()
        if selected_index:
            title = self.note_listbox.get(selected_index)
            del self.notes[title]
            self.update_note_list()

    def search_notes(self):
        search_term = simpledialog.askstring("Search Notes", "Enter search term:")
        if search_term:
            matching_notes = [title for title, content in self.notes.items() if search_term.lower() in content.lower()]
            if matching_notes:
                messagebox.showinfo("Search Results", f"Matching notes: {', '.join(matching_notes)}")
            else:
                messagebox.showinfo("Search Results", "No matching notes found.")

    def update_note_list(self):
        self.note_listbox.delete(0, tk.END)
        for title in self.notes.keys():
            self.note_listbox.insert(tk.END, title)
        self.save_notes()

def main():
    root = tk.Tk()
    app = NoteTakingApp(root)
    root.mainloop()

if __name__ == "__main__":
    main()