import tkinter as tk
from tkinter import messagebox
import os

class Note:
    def __init__(self, title, content):
        self.title = title
        self.content = content

class NoteTaker:
    def __init__(self, root):
        self.root = root
        self.root.title("Note Taker")
        self.notes = []
        self.current_note_index = None
        
        # Create GUI elements
        self.create_widgets()
        
        # Load notes from file
        self.load_notes()
        
    def create_widgets(self):
        # Listbox to display notes
        self.note_listbox = tk.Listbox(self.root, width=50, height=20)
        self.note_listbox.pack(side=tk.LEFT, fill=tk.BOTH, padx=5, pady=5)
        self.note_listbox.bind("<<ListboxSelect>>", self.on_note_select)
        
        # Scrollbar for note listbox
        scrollbar = tk.Scrollbar(self.root)
        scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
        self.note_listbox.config(yscrollcommand=scrollbar.set)
        scrollbar.config(command=self.note_listbox.yview)
        
        # Title and Content Entry
        self.title_label = tk.Label(self.root, text="Title:")
        self.title_label.pack()
        self.title_entry = tk.Entry(self.root, width=50)
        self.title_entry.pack(pady=5)
        self.title_entry.insert(0, "Title")
        
        self.content_label = tk.Label(self.root, text="Content:")
        self.content_label.pack()
        self.content_text = tk.Text(self.root, width=50, height=10)
        self.content_text.pack(pady=5)
        self.content_text.insert(tk.END, "Content")
        
        # Search Entry
        self.search_label = tk.Label(self.root, text="Search:")
        self.search_label.pack()
        self.search_entry = tk.Entry(self.root, width=50)
        self.search_entry.pack(pady=5)
        self.search_entry.bind('<KeyRelease>', self.search_notes)
        # Buttons
        self.save_button = tk.Button(self.root, text="Save", command=self.save_note)
        self.save_button.pack(side=tk.LEFT, padx=5)
        
        self.delete_button = tk.Button(self.root, text="Delete", command=self.delete_note)
        self.delete_button.pack(side=tk.LEFT, padx=5)
        
        
        
        # Bind double-click event to edit note
        self.note_listbox.bind('<Double-Button-1>', self.edit_note)
        
    def load_notes(self):
        if os.path.exists("notes.txt"):
            with open("notes.txt", "r") as f:
                lines = f.readlines()
                for i in range(0, len(lines), 2):
                    title = lines[i].strip()
                    content = lines[i+1].strip()
                    self.notes.append(Note(title, content))
                    self.note_listbox.insert(tk.END, title)
                    
    def save_notes(self):
        with open("notes.txt", "w") as f:
            for note in self.notes:
                f.write(note.title + "\n")
                f.write(note.content + "\n")
                    
    def save_note(self):
        title = self.title_entry.get().strip()
        content = self.content_text.get("1.0", tk.END).strip()
        if title and content:
            if self.current_note_index is not None:
                self.notes[self.current_note_index] = Note(title, content)
                self.note_listbox.delete(self.current_note_index)
                self.note_listbox.insert(self.current_note_index, title)
            else:
                self.notes.append(Note(title, content))
                self.note_listbox.insert(tk.END, title)
            self.save_notes()
            self.clear_fields()
        else:
            messagebox.showwarning("Warning", "Please enter a title and content.")
            
    def delete_note(self):
        if self.current_note_index is not None:
            del self.notes[self.current_note_index]
            self.note_listbox.delete(self.current_note_index)
            self.save_notes()
            self.clear_fields()
            
    def clear_fields(self):
        self.title_entry.delete(0, tk.END)
        self.title_entry.insert(0, "Title")
        self.content_text.delete("1.0", tk.END)
        self.content_text.insert(tk.END, "Content")
        self.current_note_index = None
        
    def on_note_select(self, event):
        if self.note_listbox.curselection():
            index = int(self.note_listbox.curselection()[0])
            self.current_note_index = index
            note = self.notes[index]
            self.title_entry.delete(0, tk.END)
            self.title_entry.insert(0, note.title)
            self.content_text.delete("1.0", tk.END)
            self.content_text.insert(tk.END, note.content)
            
    def edit_note(self, event):
        if self.note_listbox.curselection():
            self.on_note_select(event)
            
    def search_notes(self, event):
        search_term = self.search_entry.get().lower()
        self.note_listbox.delete(0, tk.END)
        for note in self.notes:
            if search_term in note.title.lower() or search_term in note.content.lower():
                self.note_listbox.insert(tk.END, note.title)

if __name__ == "__main__":
    root = tk.Tk()
    app = NoteTaker(root)
    root.mainloop()
