import tkinter as tk
from tkinter import messagebox

# Sample contact class to store data
class Contact:
  def __init__(self, name, phone_number):
    self.name = name
    self.phone_number = phone_number

# Contact manager class to handle data and GUI interaction
class ContactManager:
  def __init__(self):
    self.contacts = []  # List to store contacts
    self.load_contacts()  # Load contacts from file on startup

    # Create the main window
    self.window = tk.Tk()
    self.window.title("Simple Contact Manager")

    # Create listbox to display contacts
    self.contact_list = tk.Listbox(self.window, width=50)
    self.contact_list.pack()

    # Create entry fields for name and phone number
    self.name_entry = tk.Entry(self.window)
    self.phone_entry = tk.Entry(self.window)
    self.name_entry.pack()
    self.phone_entry.pack()

    # Create buttons for add, edit, delete, and search functions
    self.add_button = tk.Button(self.window, text="Add Contact", command=self.add_contact)
    self.add_button.pack()
    self.edit_button = tk.Button(self.window, text="Edit Contact", command=self.edit_contact, state=tk.DISABLED)
    self.edit_button.pack()
    self.delete_button = tk.Button(self.window, text="Delete Contact", command=self.delete_contact, state=tk.DISABLED)
    self.delete_button.pack()
    self.search_entry = tk.Entry(self.window)
    self.search_entry.pack()
    self.search_button = tk.Button(self.window, text="Search", command=self.search_contacts)
    self.search_button.pack()

    # Update the contact list on startup
    self.update_contact_list()

    # Run the main event loop
    self.window.mainloop()

  # Function to load contacts from a file (replace with actual file operations)
  def load_contacts(self):
    # Simulate loading contacts from a file (replace with actual reading)
    self.contacts.append(Contact("John Doe", "123-456-7890"))
    self.contacts.append(Contact("Jane Smith", "987-654-3210"))

  # Function to save contacts to a file (replace with actual file operations)
  def save_contacts(self):
    # Simulate saving contacts to a file (replace with actual writing)
    pass

  # Function to add a new contact
  def add_contact(self):
    name = self.name_entry.get()
    phone_number = self.phone_entry.get()

    if name and phone_number:
      new_contact = Contact(name, phone_number)
      self.contacts.append(new_contact)
      self.update_contact_list()
      self.save_contacts()  # Call save function (replace with actual implementation)
      self.name_entry.delete(0, tk.END)
      self.phone_entry.delete(0, tk.END)
      messagebox.showinfo("Success", "Contact added successfully!")

  # Function to edit a selected contact
  def edit_contact(self):
    selected_index = self.contact_list.curselection()
    if selected_index:
      index = selected_index[0]
      selected_contact = self.contacts[index]
      # Open a new window for editing details (not implemented here)

  # Function to delete a selected contact
  def delete_contact(self):
    selected_index = self.contact_list.curselection()
    if selected_index:
      index = selected_index[0]
      confirmation = messagebox.askquestion("Confirm", "Are you sure you want to delete this contact?")
      if confirmation == "yes":
        del self.contacts[index]
        self.update_contact_list()
        self.save_contacts()  # Call save function (replace with actual implementation)
        self.edit_button.config(state=tk.DISABLED)
        self.delete_button
