import tkinter as tk
from tkinter import ttk, messagebox
import json

root = tk.Tk()
root.title("Notes App")
root.geometry("500x500")

root.configure(bg="lightpink4")  # تعيين لون خلفية النافذة الرئيسية

style = ttk.Style()
style.configure("TNotebook", background="lightpink4")  # تعيين لون خلفية التابز (Notebook) إلى اللون الوردي
style.configure("TNotebook.Tab", background="lightpink4", font=("TkDefaultFont", 14, "bold"))  # تعيين لون خلفية علامات التابز إلى اللون الوردي
notebook = ttk.Notebook(root)

notes = {}
try:
    with open("notes.json", "r") as f:
        notes = json.load(f)
except FileNotFoundError:
    pass

notebook.pack(padx=10, pady=10, fill=tk.BOTH, expand=True)

title_entry = None
content_entry = None

def add_note():
    global title_entry, content_entry
    note_frame = ttk.Frame(notebook, padding=10)
    notebook.add(note_frame, text="New Note")
    
    title_label = ttk.Label(note_frame, text="Title:") 
    title_label.grid(row=0, column=0, padx=10, pady=10, sticky="W")
   
    title_entry = ttk.Entry(note_frame, width=40) 
    title_entry.grid(row=0, column=1, padx=10, pady=10)
    
    content_label = ttk.Label(note_frame, text="Content:")
    content_label.grid(row=1, column=0, padx=10, pady=10, sticky="W")
    
    content_entry = tk.Text(note_frame, width=40, height=10)
    content_entry.grid(row=1, column=1, padx=10, pady=10)

    save_button = ttk.Button(note_frame, text="Save", command=save_note)
    save_button.grid(row=2, column=1, padx=10, pady=10)

def save_note():
    global title_entry, content_entry
    title = title_entry.get()
    content = content_entry.get("1.0", tk.END)
    notes[title] = content.strip()
    
    with open("notes.json", "w") as f:
         json.dump(notes, f)
         
    note_content = tk.Text(notebook, width=40, height=10)
    note_content.insert(tk.END, content)
    notebook.forget(notebook.select())
    notebook.add(note_content, text=title)

def load_notes():
    try :
        with open("notes.json", "r") as f:
            notes = json.load(f)
            for title, content in notes.items():
                note_content = tk.Text(notebook, width=40, height=10)
                note_content.insert(tk.END, content)
                notebook.add(note_content, text=title)
    except FileNotFoundError:
        pass

def delete_note():
    current_tab = notebook.index(notebook.select())
    note_title = notebook.tab(notebook.select(), "text")
    confirm = messagebox.askyesno("Delete Note",f"Are you sure you want to delete {note_title}?")
    if confirm:
        notebook.forget(current_tab)
        notes.pop(note_title)
        with open("notes.json", "w") as f:
            json.dump(notes, f)

def search_note():
    query = search_entry.get().strip().lower()
    if query:
        for index in range(notebook.index("end")):
            tab_text = notebook.tab(index, "text").lower()
            tab_content = notebook.nametowidget(notebook.tabs()[index]).get("1.0", "end-1c").lower()
            if query in tab_text or query in tab_content:
                notebook.select(index)
                break
        else:
            messagebox.showinfo("Search", "No matching note found.")
    else:
        messagebox.showwarning("Search", "Please enter a search query.")

new_button = ttk.Button(root, text="New Note")
new_button.pack(side=tk.LEFT, padx=10, pady=10)
new_button.config(command=add_note)

delete_button = ttk.Button(root, text="Delete")
delete_button.pack(side=tk.LEFT, padx=10, pady=10)
delete_button.config(command=delete_note)

search_entry = ttk.Entry(root, width=40)
search_entry.pack(side=tk.LEFT, padx=10, pady=10)
search_entry.config(show="Search")
search_button = ttk.Button(root, text="Search")
search_button.pack(side=tk.LEFT, padx=10, pady=10)
search_button.config(command=search_note)

root.mainloop()