import tkinter as tk
from tkinter import messagebox

class Note_Taking_app:
    def __init__(self, root):
        self.root = root
        self.root.title("Note Taking App")
        
        self.primary_color = "#333333"  
        self.secondary_color = "#FFC107"
        
        self.notes = []
        self.root.configure(bg=self.primary_color)

        self.title_label = tk.Label(root ,text="Title:",bg="#00F3FF")
        self.title_label.grid(row=0, column=0, padx=10, pady=5,sticky="w")
        self.title_entry = tk.Entry(root,width=40)
        self.title_entry.grid(row=0, column=1, padx=10, pady=5, columnspan=2)

        self.content_label = tk.Label(root, text="Content:",bg="#00F3FF")
        self.content_label.grid(row=1, column=0, padx=10, pady=5, sticky="w")
        self.content_entry = tk.Text(root, width=30, height=10)
        self.content_entry.grid(row=1, column=1, padx=10, pady=5, columnspan=2)

        self.add_button = tk.Button(root, text="Add Note", command=self.add_note)
        self.add_button.grid(row=0, column=3, padx=5, pady=10)

        
        self.edit_button = tk.Button(root, text="Edit Note", command=self.edit_note)
        self.edit_button.grid(row=1, column=3, padx=5, pady=10)

        
        self.search_label = tk.Label(root, text="Search:",bg="#006AFF")
        self.search_label.grid(row=3, column=2, padx=2, pady=2, sticky="e")
        self.search_entry = tk.Entry(root, width=20)
        self.search_entry.grid(row=3, column=3, padx=3, pady=3)

        self.search_button = tk.Button(root, text="Search", command=self.search_note)
        self.search_button.grid(row=3, column=4, padx=5, pady=5)

        self.note_listbox = tk.Listbox(root, width=40, height=10)
        self.note_listbox.grid(row=3, column=0, columnspan=3, padx=10, pady=5)

        self.bold_var = tk.BooleanVar()
        self.italic_var = tk.BooleanVar()
        self.underline_var = tk.BooleanVar()

        self.bold_checkbox = tk.Checkbutton(root, text="Bold", variable=self.bold_var, command=self.apply_formatting,bg="#006AFF")
        self.bold_checkbox.grid(row=2, column=1, padx=4, pady=4)
        
        self.italic_checkbox = tk.Checkbutton(root, text="Italic", variable=self.italic_var, command=self.apply_formatting,bg="#006AFF")
        self.italic_checkbox.grid(row=2, column=2, padx=4, pady=4)
        
        self.underline_checkbox = tk.Checkbutton(root, text="Underline", variable=self.underline_var, command=self.apply_formatting,bg="#006AFF")
        self.underline_checkbox.grid(row=2, column=0, padx=4, pady=4)
        
        self.remove_butt = tk.Button(root, text="Remove Task", command=self.delete_note)
        self.remove_butt.grid(row=2, column=3, padx=5, pady=10, sticky="we")
        
     
        
      
        self.load_notes()

    def apply_formatting(self):
        text = self.content_entry.get("1.0", "end-1c")
        
        tags_to_remove = ["bold", "italic", "underline"]
        for tag in tags_to_remove:
            self.content_entry.tag_remove(tag, "1.0", "end")
        
        if self.bold_var.get():
            self.content_entry.tag_add("bold", "sel.first", "sel.last")
            self.content_entry.tag_configure("bold", font=("TkDefaultFont", 12, "bold"))
        
        if self.italic_var.get():
            self.content_entry.tag_add("italic", "sel.first", "sel.last")
            self.content_entry.tag_configure("italic", font=("TkDefaultFont", 12, "italic"))
            
        if self.underline_var.get():
            self.content_entry.tag_add("underline", "sel.first", "sel.last")
            self.content_entry.tag_configure("underline", underline=True)

    def add_note(self):
        title = self.title_entry.get().strip()
        content = self.content_entry.get("1.0", "end-1c").strip()
        if title and content:
            self.notes.append((title, content))
            self.update_note_listbox()
            self.clear_entries()
            self.save_notes()
        else:
            messagebox.showwarning("Warning", "Please enter both title and content for the note.")

    def edit_note(self):
        selected_index = self.note_listbox.curselection()
        if selected_index:
            selected_note_index = selected_index[0]
            selected_note = self.notes[selected_note_index]
            self.title_entry.delete(0, tk.END)
            self.title_entry.insert(0, selected_note[0])
            self.content_entry.delete("1.0", tk.END)
            self.content_entry.insert("1.0", selected_note[1])
            del self.notes[selected_note_index]
            self.update_note_listbox()
        else:
            messagebox.showwarning("Warning", "Please select a note to edit.")

    def delete_note(self):
        selected_index = self.note_listbox.curselection()
        if selected_index:
            del self.notes[selected_index[0]]
            self.update_note_listbox()
            self.save_notes()
        else:
            messagebox.showwarning("Warning", "Please select a note to delete.")

    def search_note(self):
        query = self.search_entry.get().lower()
        search_results = []
        for note in self.notes:
            if query in note[0].lower() or query in note[1].lower():
                search_results.append(note[0])
        self.display_search_results(search_results)
        
        
   

    def display_search_results(self, results):
        self.note_listbox.delete(0, tk.END)
        for result in results:
            self.note_listbox.insert(tk.END, result)

    def update_note_listbox(self):
        self.note_listbox.delete(0, tk.END)
        for note in self.notes:
            self.note_listbox.insert(tk.END, note[0])

    def clear_entries(self):
        self.title_entry.delete(0, tk.END)
        self.content_entry.delete("1.0", tk.END)

    def save_notes(self):
        with open("notes.txt", "w") as file:
            for note in self.notes:
                file.write(f"{note[0]}\n{note[1]}\n")

    def load_notes(self):
        try:
            with open("notes.txt", "r") as file:
                lines = file.readlines()
                for i in range(0, len(lines), 2):
                    title = lines[i].strip()
                    content = lines[i+1].strip()
                    self.notes.append((title, content))
                self.update_note_listbox()
        except FileNotFoundError:
            pass

def main():
    root = tk.Tk()
    app = Note_Taking_app(root)
    root.mainloop()

if __name__ == "__main__":
    main()