
import tkinter as tk  # استيراد مكتبة tkinter لإنشاء واجهة المستخدم الرسومية
from tkinter import messagebox  # استيراد messagebox من tkinter لعرض الرسائل
import json  # استيراد مكتبة json لحفظ وتحميل الاتصالات

class ContactManager:  # تعريف الفئة ContactManager
    def __init__(self, root):  # دالة البناء تأخذ root كمعامل
        self.root = root  # تعيين root كخاصية للكائن
        self.contacts = {}  # إنشاء قاموس فارغ لحفظ الاتصالات
        self.load_contacts()  # تحميل الاتصالات من الملف

        self.listbox = tk.Listbox(root)  # إنشاء Listbox لعرض الاتصالات
        self.listbox.pack()  # إضافة Listbox إلى النافذة

        self.name_var = tk.StringVar()  # إنشاء متغير لحفظ اسم الاتصال
        self.phone_var = tk.StringVar()  # إنشاء متغير لحفظ رقم الاتصال

        tk.Entry(root, textvariable=self.name_var).pack()  # إنشاء حقل إدخال للإسم
        tk.Entry(root, textvariable=self.phone_var).pack()  # إنشاء حقل إدخال للرقم

        tk.Button(root, text="Add Contact", command=self.add_contact).pack()  # زر لإضافة اتصال
        tk.Button(root, text="Edit Contact", command=self.edit_contact).pack()  # زر لتعديل اتصال
        tk.Button(root, text="Delete Contact", command=self.delete_contact).pack()  # زر لحذف اتصال
        tk.Button(root, text="Search Contact", command=self.search_contact).pack()  # زر للبحث عن اتصال

        self.update_listbox()  # تحديث Listbox لعرض الاتصالات الحالية

    def load_contacts(self):  # دالة لتحميل الاتصالات من الملف
        try:
            with open('contacts.json', 'r') as file:  # فتح الملف للقراءة
                self.contacts = json.load(file)  # تحميل الاتصالات من الملف
        except FileNotFoundError:  # إذا لم يتم العثور على الملف
            pass  # تجاهل الخطأ

    def save_contacts(self):  # دالة لحفظ الاتصالات في الملف
        with open('contacts.json', 'w') as file:  # فتح الملف للكتابة
            json.dump(self.contacts, file)  # حفظ الاتصالات في الملف

    def update_listbox(self):  # دالة لتحديث Listbox
        self.listbox.delete(0, tk.END)  # حذف كل العناصر الحالية في Listbox
        for name, phone in self.contacts.items():  # لكل اتصال في القاموس
            self.listbox.insert(tk.END, f"{name}: {phone}")  # إضافة الاتصال إلى Listbox

    def add_contact(self):  # دالة لإضافة اتصال
        name = self.name_var.get()  # الحصول على الاسم من حقل الإدخال
        phone = self.phone_var.get()  # الحصول على الرقم من حقل الإدخال
        self.contacts[name] = phone  # إضافة الاتصال إلى القاموس
        self.save_contacts()  # حفظ الاتصالات في الملف
        self.update_listbox()  # تحديث Listbox

    def edit_contact(self):  # دالة لتعديل اتصال
        name = self.name_var.get()  # الحصول على الاسم من حقل الإدخال
        phone = self.phone_var.get()  # الحصول على الرقم من حقل الإدخال
        if name in self.contacts:  # إذا كان الاتصال موجودا في القاموس
            self.contacts[name] = phone  # تعديل الاتصال في القاموس
            self.save_contacts()  # حفظ الاتصالات في الملف
            self.update_listbox()  # تحديث Listbox
        else:  # إذا لم يكن الاتصال موجودا في القاموس
            messagebox.showinfo("Error", "Contact not found")  # عرض رسالة خطأ

    def delete_contact(self):  # دالة لحذف اتصال
        name = self.name_var.get()  # الحصول على الاسم من حقل الإدخال
        if name in self.contacts:  # إذا كان الاتصال موجودا في القاموس
            del self.contacts[name]  # حذف الاتصال من القاموس
            self.save_contacts()  # حفظ الاتصالات في الملف
            self.update_listbox()  # تحديث Listbox
        else:  # إذا لم يكن الاتصال موجودا في القاموس
            messagebox.showinfo("Error", "Contact not found")  # عرض رسالة خطأ

    def search_contact(self):  # دالة للبحث عن اتصال
        name = self.name_var.get()  # الحصول على الاسم من حقل الإدخال
        if name in self.contacts:  # إذا كان الاتصال موجودا في القاموس
            messagebox.showinfo("Contact Found", f"{name}: {self.contacts[name]}")  # عرض الاتصال
        else:  # إذا لم يكن الاتصال موجودا في القاموس
            messagebox.showinfo("Error", "Contact not found")  # عرض رسالة خطأ

root = tk.Tk()  # إنشاء نافذة جديدة
app = ContactManager(root)  # إنشاء كائن من الفئة ContactManager
root.mainloop()  # بدء الحلقة الرئيسية للنافذة
