# -*- coding: utf-8 -*-
"""Hard Project (2nd)-Khalid AlHarbi

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1bm701Ug-wWfEI6kA_dECB8bMsjWJ5emU
"""

import tkinter as tk
from tkinter import messagebox
import json

class ContactManager:
    def __init__(self, root):
        self.root = root
        self.root.title("Simple Contact Manager")
        self.contacts = self.load_contacts()

        # Create GUI components
        self.create_widgets()

    def create_widgets(self):
        # Listbox to display contacts
        self.listbox = tk.Listbox(self.root)
        self.listbox.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)

        # Entry fields for name and phone number
        self.name_entry = tk.Entry(self.root)
        self.name_entry.pack()
        self.phone_entry = tk.Entry(self.root)
        self.phone_entry.pack()

        # Buttons for adding, editing, and deleting contacts
        tk.Button(self.root, text="Add Contact", command=self.add_contact).pack()
        tk.Button(self.root, text="Edit Contact", command=self.edit_contact).pack()
        tk.Button(self.root, text="Delete Contact", command=self.delete_contact).pack()

        # Search entry
        self.search_entry = tk.Entry(self.root)
        self.search_entry.pack()
        tk.Button(self.root, text="Search", command=self.search_contact).pack()

        # Display contacts in the listbox
        self.update_listbox()

    def load_contacts(self):
        try:
            with open("contacts.json", "r") as file:
                return json.load(file)
        except FileNotFoundError:
            return {}

    def save_contacts(self):
        with open("contacts.json", "w") as file:
            json.dump(self.contacts, file)

    def add_contact(self):
        name = self.name_entry.get()
        phone = self.phone_entry.get()
        if name and phone:
            self.contacts[name] = phone
            self.save_contacts()
            self.update_listbox()
        else:
            messagebox.showerror("Error", "Name and phone number cannot be empty")

    def edit_contact(self):
        # Implement contact editing logic
        pass

    def delete_contact(self):
        contact = self.listbox.get(tk.ACTIVE)
        if contact:
            self.contacts.pop(contact, None)
            self.save_contacts()
            self.update_listbox()

    def search_contact(self):
        search_term = self.search_entry.get()
        if search_term:
            matches = {name: num for name, num in self.contacts.items() if search_term.lower() in name.lower()}
            self.update_listbox(matches)
        else:
            self.update_listbox()

    def update_listbox(self, contacts=None):
        if contacts is None:
            contacts = self.contacts
        self.listbox.delete(0, tk.END)
        for name, phone in contacts.items():
            self.listbox.insert(tk.END, f"{name}: {phone}")

if __name__ == "__main__":
    root = tk.Tk()
    app = ContactManager(root)
    root.mainloop()