import tkinter as tk
from tkinter import filedialog, messagebox

root = tk.Tk()
root.title("Simple Text Editor")
root.geometry("600x400")

text_area = tk.Text(root, wrap="word", undo=True,font=("Arial", 12))
scrollbar = tk.Scrollbar(root, command=text_area.yview)
text_area.configure(yscrollcommand=scrollbar.set)
scrollbar.pack(side="right", fill="y")
text_area.pack(fill="both", expand=True)

# Functions
current_file = None
dark_mode = False
 
def new_file():
    global current_file
    current_file = None
    text_area.delete(1.0, tk.END)
    root.title("Untitled - Text Editor")

def open_file():
    global current_file
    file_path = filedialog.askopenfilename(
        defaultextension=".txt",
        filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")]
    )
    if file_path:
        current_file = file_path
        with open(file_path, "r", encoding="utf-8") as file:
            text_area.insert(1.0, file.read())
        root.title(f"{file_path} - Text Editor")

def save_file():
    global current_file
    if current_file:
        with open(current_file, "w", encoding="utf-8") as file:
            file.write(text_area.get(1.0, tk.END))
    else:
        save_as_file()

def save_as_file():
    global current_file
    file_path = filedialog.asksaveasfilename(
        defaultextension=".txt",
        filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")]
    )
    if file_path:
        current_file = file_path
        with open(file_path, "w", encoding="utf-8") as file:
            file.write(text_area.get(1.0, tk.END))
        root.title(f"{file_path} - Text Editor")
def exit_app():
    if messagebox.askyesno("Exit", "Do you want to exit?"):
        root.destroy()
# ---------------- Extra Features ----------------     
def find_replace():
    find_window = tk.Toplevel(root)
    find_window.title("Find & Replace")
    find_window.geometry("300x150")
    
    tk.Label(find_window, text="Find:").pack(pady=5)
    find_entry = tk.Entry(find_window, width=25)
    find_entry.pack()
    
    tk.Label(find_window, text="Replace:").pack(pady=5)
    replace_entry = tk.Entry(find_window, width=25)
    replace_entry.pack()
    
    def replace_text():
        find_text = find_entry.get()
        replace_text = replace_entry.get()
        content = text_area.get(1.0, tk.END)
        new_content = content.replace(find_text, replace_text)
        text_area.delete(1.0, tk.END)
        text_area.insert(1.0, new_content)
    
    tk.Button(find_window, text="Replace All", command=replace_text).pack(pady=10)
def toggle_theme():
    global dark_mode
    if dark_mode:
        text_area.config(bg="white", fg="black", insertbackground="black")
        root.config(bg="white")
        dark_mode = False
    else:
        text_area.config(bg="black", fg="white", insertbackground="white")
        root.config(bg="black")
        dark_mode = True
def change_font():
    font_window = tk.Toplevel(root)
    font_window.title("Font Settings")
    font_window.geometry("300x200")
    
    tk.Label(font_window, text="Font Family:").pack(pady=5)
    font_family_var = tk.StringVar(value="Arial")
    font_family_entry = tk.Entry(font_window, textvariable=font_family_var)
    font_family_entry.pack()
    
    tk.Label(font_window, text="Font Size:").pack(pady=5)
    font_size_var = tk.IntVar(value=12)
    font_size_entry = tk.Entry(font_window, textvariable=font_size_var)
    font_size_entry.pack()
    def apply_font():
        family = font_family_var.get()
        size = font_size_var.get()
        
        try:
            text_area.config(font=(family, size))
        except:
            messagebox.showerror("Error", "Invalid font settings!")
    
    
    tk.Button(font_window, text="Apply", command=apply_font).pack(pady=10)
# Menu Bar
menu_bar = tk.Menu(root)
# File Menu
file_menu = tk.Menu(menu_bar, tearoff=0)
file_menu.add_command(label="New", command=new_file)
file_menu.add_command(label="Open", command=open_file)
file_menu.add_command(label="Save", command=save_file)
file_menu.add_command(label="Save As", command=save_as_file)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=exit_app)

menu_bar.add_cascade(label="File", menu=file_menu)
# Edit Menu
edit_menu = tk.Menu(menu_bar, tearoff=0)
edit_menu.add_command(label="Find & Replace", command=find_replace)
menu_bar.add_cascade(label="Edit", menu=edit_menu)
# View Menu
view_menu = tk.Menu(menu_bar, tearoff=0)
view_menu.add_command(label="Toggle Dark/Light Theme", command=toggle_theme)
view_menu.add_command(label="Font Settings", command=change_font)
menu_bar.add_cascade(label="View", menu=view_menu)

root.config(menu=menu_bar)



root.mainloop()