#include "connectortoserver.h"

QByteArray ConnectorToServer::getListTaskFimArray()
{
    QMutexLocker locker(&mtxAccess);
    return listTaskFimArray;
}

QByteArray ConnectorToServer::getListTaskAmmArray()
{
    QMutexLocker locker(&mtxAccess);
    return listTaskAmmArray;
}

QList<Instructor> ConnectorToServer::getListInstructors()
{
    QMutexLocker locker(&mtxAccess);
    return listInstructors;
}

QList<Trainee> ConnectorToServer::getListTrainees()
{
    QMutexLocker locker(&mtxAccess);
    return listTrainees;
}

QList<Group> ConnectorToServer::getListGroups()
{
    QMutexLocker locker(&mtxAccess);
    return listGroups;
}

QList<Computer> ConnectorToServer::getListComputers()
{
    QMutexLocker locker(&mtxAccess);
    return  listComputers;
}

QList<Classroom> ConnectorToServer::getListClassrooms()
{
    QMutexLocker locker(&mtxAccess);
    return listClassrooms;
}

QList<TaskAmmFim> ConnectorToServer::getListTasksAMMforTrainee(int trainee_id)
{
    QMutexLocker locker(&mtxAccess);

    if(mapTasksAMM.contains(trainee_id))
        return mapTasksAMM.value(trainee_id);
    else
        return QList<TaskAmmFim>();
}

QList<TaskAmmFim> ConnectorToServer::getListTasksFIMforTrainee(int trainee_id)
{
    QMutexLocker locker(&mtxAccess);

    if(mapTasksFIM.contains(trainee_id))
        return mapTasksFIM.value(trainee_id);
    else
        return QList<TaskAmmFim>();
}

QList<SubProc> ConnectorToServer::getListSubProcForDMCode(QString dmCode)
{
    QMutexLocker locker(&mtxAccess);

    if(mapSubProc.contains(dmCode))
        return mapSubProc.value(dmCode);
    else
        return QList<SubProc>();
}

int ConnectorToServer::getCountTasksAMMforTrainee(int trainee_id)
{
    QMutexLocker locker(&mtxAccess);

    if(mapTasksAMM.contains(trainee_id))
        return mapTasksAMM.value(trainee_id).count();
    else
        return 0;
}

int ConnectorToServer::getCountTasksFIMforTrainee(int trainee_id)
{
    QMutexLocker locker(&mtxAccess);

    if(mapTasksFIM.contains(trainee_id))
        return mapTasksFIM.value(trainee_id).count();
    else
        return 0;
}

bool ConnectorToServer::isArchivedInstructor(int id)
{
    QMutexLocker locker(&mtxAccess);

    for(Instructor instructor : listInstructors)
    {
        if(instructor.getID() == id)
        {
            if(instructor.getArchived()) return true;
            else return false;
        }
    }
    return false;
}

bool ConnectorToServer::isAdminInstructor(int id)
{
    QMutexLocker locker(&mtxAccess);

    for(Instructor instructor : listInstructors)
    {
        if(instructor.getID() == id)
        {
            if(instructor.getIsAdmin()) return true;
            else return false;
        }
    }
    return false;
}

bool ConnectorToServer::isLoggedInInstructor(int id)
{
    QMutexLocker locker(&mtxAccess);

    for(Instructor instructor : listInstructors)
    {
        if(instructor.getID() == id)
        {
            if(instructor.getLoggedIn()) return true;
            else return false;
        }
    }
    return false;
}

Instructor ConnectorToServer::getInstructor(int id)
{
    QMutexLocker locker(&mtxAccess);

    for(Instructor instructor : listInstructors)
    {
        if(instructor.getID() == id)
            return instructor;
    }
    return Instructor();
}

QList<Trainee> ConnectorToServer::getListTraineesInGroup(int id)
{
    QMutexLocker locker(&mtxAccess);

    QList<Trainee> list;
    for(Trainee trainee : listTrainees)
    {
        if(trainee.getGroup().getID() == id)
            list.append(trainee);
    }
    return list;
}

bool ConnectorToServer::isArchivedTrainee(int id)
{
    QMutexLocker locker(&mtxAccess);

    for(Trainee trainee : listTrainees)
    {
        if(trainee.getID() == id)
        {
            if(trainee.getArchived()) return true;
            else return false;
        }
    }
    return false;
}

bool ConnectorToServer::isLoggedInTrainee(int id)
{
    QMutexLocker locker(&mtxAccess);

    for(Trainee trainee : listTrainees)
    {
        if(trainee.getID() == id)
        {
            if(trainee.getLoggedIn()) return true;
            else return false;
        }
    }
    return false;
}

Trainee ConnectorToServer::getTrainee(int id)
{
    QMutexLocker locker(&mtxAccess);

    for(Trainee trainee : listTrainees)
    {
        if(trainee.getID() == id)
            return trainee;
    }
    return Trainee();
}

Group ConnectorToServer::getGroup(int id)
{
    QMutexLocker locker(&mtxAccess);

    for(Group group : listGroups)
    {
        if(group.getID() == id)
            return group;
    }
    return Group();
}

int ConnectorToServer::getIdTraineeByLogin(QString login)
{
    QMutexLocker locker(&mtxAccess);

    for(Trainee trainee : listTrainees)
    {
        if(trainee.getLogin() == login)
            return trainee.getID();
    }
    return 0;
}

int ConnectorToServer::getIdInstructorByLogin(QString login)
{
    QMutexLocker locker(&mtxAccess);

    for(Instructor instructor : listInstructors)
    {
        if(instructor.getLogin() == login)
            return instructor.getID();
    }
    return 0;
}

void ConnectorToServer::clearListModelDB()
{
    QMutexLocker locker(&mtxAccess);

    listInstructors.clear();
    listGroups.clear();
    listTrainees.clear();
    listComputers.clear();
    listClassrooms.clear();
    mapTasksAMM.clear();
    mapTasksFIM.clear();

    mapSubProc.clear();

    listTaskFimArray.clear();
    listTaskAmmArray.clear();
}
