# Arup Guha
# 6/3/2026
# Used to show use of multiple Contact objects in another class.
# This is a HAS-A relationship.

# Since this is in a different file.
from Contact import Contact

class AddressBook:

    # Builds an empty address book.
    def __init__(self):
        self.adrBook = []
        self.numContacts = 0

    # Returns the index with the contact with the name name or -1 if no
    # such contact exists in this AddressBook.
    def contactIndex(self, name):

        # Look in each spot for the contact.
        for i in range(self.numContacts):
            if name == self.adrBook[i].name:
                return i

        # Never found it.
        return -1

    # Tries to add contact to the address book. If no one has the same
    # name then the contact is added and true is returned. Otherwise,
    # no action is taken and false is returned.
    def addContact(self, contact):

        # Get the index where this name is.
        idx = self.contactIndex(contact.name)

        # Already in the book.
        if idx != -1:
            return False

        # Add and update.
        self.adrBook.append(contact)
        self.numContacts+=1
        return True

    # Deletes a contact with name if it exists an returns True. If not, does nothing
    # and returns false.
    def deleteContact(self, name):

        # Find it.
        idx = self.contactIndex(name)

        # Never found.
        if idx == -1:
            return False

        # Remove item.
        self.adrBook.pop(idx)
        self.numContacts -=1
        return True

    # Returns a string representation of the object.
    def __str__(self):
        mystr = "["
        for item in self.adrBook:
            mystr += (str(item)+", ")
        return mystr + "]"

    # Returns a list of the names of each person with the birthday month/day.
    def getBirthdays(self, month, day):

        # Store the answer here.
        res = []

        # Go through each contact.
        for contact in self.adrBook:

            # We have match, so add it to the result.
            if contact.bday == month*100 + day:
                res.append(contact.getName())

        # Return the answers.
        return res

# Runs some tests. 
def testAddressBook():

    # Creates an empty address book.
    myBlackBook = AddressBook()

    # Add Lionel.
    myBlackBook.addContact(Contact("LionelMessi", 36, 9999999999, 6, 24))
    print(myBlackBook)

    # And Ronaldo.
    myBlackBook.addContact(Contact("Ronaldo", 40, 123, 2, 5))
    print(myBlackBook)

    # Me
    myBlackBook.addContact(Contact("Arup", 40, 123, 9, 14))

    # Ian
    myBlackBook.addContact(Contact("Ian", 40, 123, 2, 5))
    print(myBlackBook)

    # Comment this back in to test delete.
    #if myBlackBook.deleteContact("Arup"):
    #    print("Arup has been deleted.")

    # Testing our new birthday method!
    list1 = myBlackBook.getBirthdays(2, 5)
    list2 = myBlackBook.getBirthdays(9, 14)
    list3 = myBlackBook.getBirthdays(6, 24)
    list4 = myBlackBook.getBirthdays(1, 1)
    print(list1)
    print(list2)
    print(list3)
    print(list4)

testAddressBook()
