import pickle

from time import sleep
from tkinter import *
from tkinter import ttk
from contacts_package.contact import Contact

ACTION_ADD_CONTACT = 1
ACTION_REMOVE_CONTACT = 2
ACTION_FIND_CONTACT = 3
ACTION_EXPORT_CONTACT = 4
ACTION_EXIT = 5

SAVE_FILE_NAME = "contactsDB.save"

MENU_OPTIONS = [ACTION_ADD_CONTACT, ACTION_REMOVE_CONTACT, ACTION_FIND_CONTACT, ACTION_EXPORT_CONTACT, ACTION_EXIT]

def add_contact(contacts, name, phone, email):
    contact = Contact(name, phone, email)
    contacts.append(contact)
    return contact


def add_contact_tk(contacts, name, phone, email, frame_contact_list):
    contact = add_contact(contacts, name, phone, email)
    cols, row = frame_contact_list.grid_size()
    ttk.Label(frame_contact_list, text=contact.name).grid(column=1, row=row)
    ttk.Label(frame_contact_list, text=contact.email).grid(column=2, row=row)
    ttk.Label(frame_contact_list, text=contact.phone).grid(column=3, row=row)


def ask_new_contact(contacts):
    print("\n\nAñadir contacto\n")
    contact = add_contact(contacts, input("Nombre: "), input("Teléfono: "), input("Email: "))
    print("Se ha añadido el contacto {} correctamente\n".format(contact.name))
    sleep(2)


def clear_contacts(contacts, frame_contact_list):
    contacts[:] = []
    cols, row = frame_contact_list.grid_size()
    ttk.Label(frame_contact_list, text="Se han borrado ").grid(column=1, row=row)
    ttk.Label(frame_contact_list, text="todos los ").grid(column=2, row=row)
    ttk.Label(frame_contact_list, text="contactos").grid(column=3, row=row)


def export_contacts():
    pass


def load_contacts():
    try:
        return pickle.load(open(SAVE_FILE_NAME, "rb"))
    except FileNotFoundError:
        return []
    except EOFError:
        return []


def save_contacts(contacts):
    with open(SAVE_FILE_NAME, "wb") as save_file:
        pickle.dump(contacts, save_file)
    print("Datos guardados correctamente.")


def contacts_ui_main():
    contacts = load_contacts()

    root = Tk()
    frame_add_contact = ttk.Frame(root, padding="30 30 30 15")
    frame_add_contact.grid()

    frame_contact_list = ttk.Frame(root, padding="30 30 30 15")
    frame_contact_list.grid()

    name = StringVar()
    email = StringVar()
    phone = StringVar()

    ttk.Label(frame_add_contact, text="Nombre").grid(column=1, row=1, sticky=W)
    ttk.Label(frame_add_contact, text="Email").grid(column=2, row=1, sticky=W)
    ttk.Label(frame_add_contact, text="Telefono").grid(column=3, row=1, sticky=W)

    ttk.Entry(frame_add_contact, width=7, textvariable=name).grid(column=1, row=2, sticky=W)
    ttk.Entry(frame_add_contact, width=7, textvariable=email).grid(column=2, row=2, sticky=W)
    ttk.Entry(frame_add_contact, width=7, textvariable=phone).grid(column=3, row=2, sticky=W)

    ttk.Label(frame_contact_list, text="Nombre").grid(column=1, row=1, sticky=W)
    ttk.Label(frame_contact_list, text="Email").grid(column=2, row=1, sticky=W)
    ttk.Label(frame_contact_list, text="Telefono").grid(column=3, row=1, sticky=W)

    for contact in contacts:
        cols, row = frame_contact_list.grid_size()
        ttk.Label(frame_contact_list, text=contact.name).grid(column=1, row=row, sticky=W)
        ttk.Label(frame_contact_list, text=contact.email).grid(column=2, row=row, sticky=W)
        ttk.Label(frame_contact_list, text=contact.phone).grid(column=3, row=row, sticky=W)

    ttk.Button(frame_add_contact,
               text="Añadir",
               command=lambda: add_contact_tk(contacts, name.get(), phone.get(), email.get(), frame_contact_list)
               ).grid(column=3, row=3)
    ttk.Button(frame_add_contact,
               text="Borrar Todos",
               command=lambda: clear_contacts(contacts, frame_contact_list)).grid(column=3, row=4)
    root.mainloop()
    save_contacts(contacts)


if __name__ == "__main__":
    contacts_ui_main()
