#include "LecAdm.h"

// Loesung ist absichtlich uebersichtlich gehalten. Besser waere es natuerlich,
// immer vor dem Einfuegen/Löschen zu prüfen ob der/die Student/Vorlesung/hoert-Beziehung
// bereits bzw. noch nicht existiert.

void LecAdm::AddLecture(string lecName)
{
    Lecture* newLec = new Lecture;
    newLec->name = lecName;
    lectures.append(newLec);
}

void LecAdm::RemoveLecture(string lecName)
{
    while (!lectures.isAtEnd() && lectures.read()->name != lecName) {
        lectures.next();
    }
    if (lectures.read()->name == lecName) {
        lectures.deleteCurrent();
    }
}

void LecAdm::AddStudent(int matNr, string name)
{
    Student* newStud = new Student;
    newStud->matNr = matNr;
    newStud->name = name;
    students.append(newStud);
}

void LecAdm::RemoveStudent(int matNr)
{
    students.toStart();
    while (!students.isAtEnd() && students.read()->matNr == matNr) {
        students.next();
    }
    if (students.read()->matNr == matNr) {
        students.deleteCurrent();
    }
}

bool LecAdm::AddStudentToLecture(int matNr, string lecName)
{
    lectures.toStart();
    while (!lectures.isAtEnd() && lectures.read()->name != lecName) {
        lectures.next();
    }
    if (lectures.read()->name == lecName) {
        lectures.read()->listeners.append(matNr);
        return true;
    } else {
        return false;
    }
}

bool LecAdm::IsStudentInLecture(int matNr, string lecName) 
{
    lectures.toStart();
    while (!lectures.isAtEnd() && lectures.read()->name != lecName) {
        lectures.next();
    }
    if (lectures.read()->name == lecName) {
        GenDLList<int> & lecListeners = lectures.read()->listeners;
        lecListeners.toStart();
        while (!lecListeners.isAtEnd() && lecListeners.read() != matNr) {
            lecListeners.next();
        }
        if (lecListeners.read() == matNr) {
            return true;
		} else {
			return false;
		}
    } else {
        return false;
    }
}