import os

class Student():
    def __init__(self, aNumber, aMentor):
        self._mentor = None
        self._number = None
        self._number = aNumber
        didAddMentor = self.setMentor(aMentor)
        if not didAddMentor :
            raise RuntimeError ("Unable to create student due to mentor. See http://manual.umple.org?RE002ViolationofAssociationMultiplicity.html")

    def setNumber(self, aNumber):
        wasSet = False
        self._number = aNumber
        wasSet = True
        return wasSet

    def getNumber(self):
        return self._number

    def getMentor(self):
        return self._mentor

    def setMentor(self, aMentor):
        from Mentor import Mentor
        wasSet = False
        if aMentor is None :
            return wasSet
        if aMentor.numberOfStudents() >= Mentor.maximumNumberOfStudents() :
            return wasSet
        existingMentor = self._mentor
        self._mentor = aMentor
        if not (existingMentor is None) and not existingMentor == aMentor :
            didRemove = existingMentor.removeStudent(self)
            if not didRemove :
                self._mentor = existingMentor
                return wasSet
        self._mentor.addStudent(self)
        wasSet = True
        return wasSet

    def delete(self):
        placeholderMentor = self._mentor
        self._mentor = None
        if not (placeholderMentor is None) :
            placeholderMentor.removeStudent(self)

    def __str__(self):
        return str(super().__str__()) + "[" + "number" + ":" + str(self.getNumber()) + "]" + str(os.linesep) + "  " + "mentor = " + ((format(id(self.getMentor()), "x")) if not (self.getMentor() is None) else "null")


class Mentor():
    def __init__(self, aName):
        self._students = None
        self._name = None
        self._name = aName
        self._students = []

    def setName(self, aName):
        wasSet = False
        self._name = aName
        wasSet = True
        return wasSet

    def getName(self):
        return self._name

    def getStudent(self, index):
        aStudent = self._students[index]
        return aStudent

    def getStudents(self):
        newStudents = tuple(self._students)
        return newStudents

    def numberOfStudents(self):
        number = len(self._students)
        return number

    def hasStudents(self):
        has = len(self._students) > 0
        return has

    def indexOfStudent(self, aStudent):
        index = (-1 if not aStudent in self._students else self._students.index(aStudent))
        return index

    @staticmethod
    def minimumNumberOfStudents():
        return 0

    @staticmethod
    def maximumNumberOfStudents():
        return 6

    def addStudent1(self, aNumber):
        from Student import Student
        if self.numberOfStudents() >= Mentor.maximumNumberOfStudents() :
            return None
        else :
            return Student(aNumber, self)

    def addStudent2(self, aStudent):
        wasAdded = False
        if (aStudent) in self._students :
            return False
        if self.numberOfStudents() >= Mentor.maximumNumberOfStudents() :
            return wasAdded
        existingMentor = aStudent.getMentor()
        isNewMentor = not (existingMentor is None) and not self == existingMentor
        if isNewMentor :
            aStudent.setMentor(self)
        else :
            self._students.append(aStudent)
        wasAdded = True
        return wasAdded

    def removeStudent(self, aStudent):
        wasRemoved = False
        if not self == aStudent.getMentor() :
            self._students.remove(aStudent)
            wasRemoved = True
        return wasRemoved

    def addStudentAt(self, aStudent, index):
        wasAdded = False
        if self.addStudent(aStudent) :
            if index < 0 :
                index = 0
            if index > self.numberOfStudents() :
                index = self.numberOfStudents() - 1
            self._students.remove(aStudent)
            self._students.insert(index, aStudent)
            wasAdded = True
        return wasAdded

    def addOrMoveStudentAt(self, aStudent, index):
        wasAdded = False
        if (aStudent) in self._students :
            if index < 0 :
                index = 0
            if index > self.numberOfStudents() :
                index = self.numberOfStudents() - 1
            self._students.remove(aStudent)
            self._students.insert(index, aStudent)
            wasAdded = True
        else :
            wasAdded = self.addStudentAt(aStudent, index)
        return wasAdded

    def delete(self):
        i = len(self._students)
        while i > 0 :
            aStudent = self._students[i - 1]
            aStudent.delete()
            i -= 1

    def __str__(self):
        return str(super().__str__()) + "[" + "name" + ":" + str(self.getName()) + "]"

    def addStudent(self, *argv):
        from Student import Student
        if len(argv) == 1 and isinstance(argv[0], str) :
            return self.addStudent1(argv[0])
        if len(argv) == 1 and isinstance(argv[0], Student) :
            return self.addStudent2(argv[0])
        raise TypeError("No method matches provided parameters")