# coding=utf-8

from typing import List, Callable
from adbook.orm.entity import Entity
from adbook.addressbook import AddressBook


def convert_person_list(persons: List[Entity]) -> List[str]:
    return [str(person) for person in persons]

if __name__ == '__main__':
    """
    * Find person by email address (can supply either the exact string or a prefix string,
    ie. both "alexander@company.com" and "alex" should work).
    """

    with AddressBook() as ab:
        p1 = ab.persons.create(first_name="Franc", last_name="Kafka", email="kafka@absurd.org")
        p2 = ab.persons.create(first_name="Sergey", last_name="Esenin")

        p2.emails.append("esenin1@gmail.com")
        p2.emails.append("esenin2@gmail.com")

        ab.persons.add(p1, p2)

        lookingfor_email_1 = "esenin1"
        found1 = ab.persons.find_by_email(lookingfor_email_1)

        print("Find email {}: {}".format(lookingfor_email_1, convert_person_list(found1)))

        lookingfor_email_2 = "kafka@absurd.org"
        found2 = ab.persons.find_by_email(lookingfor_email_2)

        print("Find email {}: {}".format(lookingfor_email_2, convert_person_list(found2)))
