# L-24 MCS 260 Monday 9 March 2015 : class People
"""
The class People represents patrons and the librarian.
"""

from classperson import Person

class People(object):
    """
    The class People collects information of
    all librarians and patrons of the library.
    """
    def __init__(self):
        "Creates a root user."
        root = Person(name='root', PIN=0, status=True)
        self.whoswho = [root]
        self.current = ''

    def search(self, name):
        """
        Returns None if name not in self.whoswho,
        else the person object is returned.
        """
        for person in self.whoswho:
            if person.name == name:
                return person
        return None

    def logon(self):
        """
        Prompts for name and PIN, and returns
        -1 if access is not granted;
         0 if the user is a patron;
        +1 if the user is a librarian.
        """
        name = input('Give your name : ')
        member = self.search(name)
        if member == None:
            print('you are not a member')
            return -1
        else:
            rawpin = input('Give your PIN : ')
            intpin = int(rawpin)
            if intpin == member.pin:
                print('welcome ' + member.name)
                self.current = member
                if member.librarian:
                    return +1
                else:
                    return 0
            else:
                print('wrong PIN')
                return -1

    def who(self):
        "Shows who is currently logged in."
        if self.current == '':
            print('nobody is logged in')
        else:
            print(self.current.name + ' is logged in')

    def logoff(self):
        "The current user is logged off."
        if self.current != '':
            print('bye bye, ' + self.current.name)
            self.current = ''

    def add(self, *person):
        "Adds a new person to the collection."
        if len(person) > 0:
            toadd = person[0]
        else:
            toadd = Person()
        self.whoswho.append(toadd)

    def delete(self):
        "Prompts for a name and then deletes."
        name = input('Give name : ')
        for k in range(0, len(self.whoswho)):
            person = self.whoswho[k]
            if person.name == name:
                popped = self.whoswho.pop(k)
                print(popped, 'has been deleted')
                break
