#include "providerdblms.h"

#include <QThread>

ProviderDBLMS::ProviderDBLMS(QWidget *parentWidget, QObject *parent) :
    QObject(parent),
    dbLMS(nullptr)
{
    dbLMS = new InterfaceDataBaseLMS(parentWidget);
    //ConnectionToDB();
}

ProviderDBLMS::~ProviderDBLMS()
{
    DisConnectionFromDB();
}

void ProviderDBLMS::ConnectionToDB()
{
    mtxAccess.lock();
   if(! dbLMS->DBisConnected())
   {
       if(dbLMS->ConnectionToDB())
       {
           Q_EMIT signal_BlockAutorization(false);
           //Q_EMIT signal_NeedUpdateUI(true, true);
       }
   }
   mtxAccess.unlock();
}

void ProviderDBLMS::DisConnectionFromDB()
{
     mtxAccess.lock();
    if(dbLMS->DBisConnected())
    {
        Q_EMIT signal_BlockAutorization(true);

        dbLMS->DisConnectionFromDB();

        //Q_EMIT signal_NeedUpdateUI(true, true);
    }
    mtxAccess.unlock();
}

QString ProviderDBLMS::getMainInstructorName()
{
    mtxAccess.lock();

    if(! dbLMS->DBisConnected())
    {
        mtxAccess.unlock();
        return QStringLiteral("");
    }

    //QString res = dbLMS->getNameInstructorByLogin(loginInstructorLoggedInLocal);

    mtxAccess.unlock();
    //return res;
    return QStringLiteral("");
}

bool ProviderDBLMS::authorizationTrainee(QString login, QString password, QString classroom_name, QString computer_name)
{
    mtxAccess.lock();

    if(! dbLMS->DBisConnected())
    {
        mtxAccess.unlock();
        return false;
    }

    Q_EMIT signal_BlockAutorization(true);

    bool res = dbLMS->AuthorizationTrainee(login, password, classroom_name, computer_name);

    Q_EMIT signal_BlockAutorization(false);

    //if(res)
        //Q_EMIT signal_NeedUpdateUI(false, true);

     mtxAccess.unlock();
     return res;
}

bool ProviderDBLMS::deAuthorizationTrainee(QString login)
{
    mtxAccess.lock();

    if(! dbLMS->DBisConnected())
    {
        mtxAccess.unlock();
        return false;
    }

    Q_EMIT signal_BlockAutorization(true);

    bool res = dbLMS->deAuthorizationTrainee(login);

    Q_EMIT signal_BlockAutorization(false);

    //if(res)
        //Q_EMIT signal_NeedUpdateUI(false, true);

    mtxAccess.unlock();
    return res;
}

QString ProviderDBLMS::getNameTraineeByLogin(QString login)
{
    mtxAccess.lock();

    if(! dbLMS->DBisConnected())
    {
        mtxAccess.unlock();
        return QStringLiteral("");
    }

    QString res =  dbLMS->getNameTraineeByLogin(login);

    mtxAccess.unlock();
    return res;
}

bool ProviderDBLMS::authorizationInstructor(QString login, QString password)
{
    mtxAccess.lock();

    if(! dbLMS->DBisConnected())
    {
        mtxAccess.unlock();
        return false;
    }

    Q_EMIT signal_BlockAutorization(true);

    bool res = dbLMS->AuthorizationInstructor(login, password);

    Q_EMIT signal_BlockAutorization(false);

    //if(res)
        //Q_EMIT signal_NeedUpdateUI(true, false);

    mtxAccess.unlock();
    return res;
}

bool ProviderDBLMS::deAuthorizationInstructor(QString login)
{
    mtxAccess.lock();

    if(! dbLMS->DBisConnected())
    {
        mtxAccess.unlock();
        return false;
    }

    Q_EMIT signal_BlockAutorization(true);

    bool res = dbLMS->deAuthorizationInstructor(login);

    Q_EMIT signal_BlockAutorization(false);

    /*
    if(res)
    {
        if(loginInstructorLoggedInLocal == login)
        {
            loginInstructorLoggedInLocal = QStringLiteral("");
            adminMode = false;

            viewerInstructors->setAdminMode(adminMode);
            viewerTrainees->setAdminMode(adminMode);
            viewerInstructors->setAuthComplited(false);
            viewerTrainees->setAuthComplited(false);

            Q_EMIT signal_NeedUpdateUI(true, false);
        }
        else
            Q_EMIT signal_NeedUpdateUI(true, false);

    }*/

    mtxAccess.unlock();
    return res;
}

QString ProviderDBLMS::getNameInstructorByLogin(QString login)
{
    mtxAccess.lock();

    if(! dbLMS->DBisConnected())
    {
        mtxAccess.unlock();
        return QStringLiteral("");
    }

    QString res = dbLMS->getNameInstructorByLogin(login);

    mtxAccess.unlock();
    return res;
}

QList<Instructor> ProviderDBLMS::GetListAllInstructors()
{
    QList<Instructor> listInstructors;
    qDebug() << "ProviderDBLMS " << QThread::currentThreadId();
    mtxAccess.lock();

    if(! dbLMS->DBisConnected())
    {
        mtxAccess.unlock();
        return listInstructors;
    }

    listInstructors = dbLMS->getListInstructors();

    mtxAccess.unlock();
    return listInstructors;
}

QList<Trainee> ProviderDBLMS::GetListAllTrainees()
{
    QList<Trainee> listTrainees;

    mtxAccess.lock();

    if(! dbLMS->DBisConnected())
    {
        mtxAccess.unlock();
        return listTrainees;
    }

    listTrainees = dbLMS->getListTrainees();

    mtxAccess.unlock();
    return listTrainees;
}

QList<Group> ProviderDBLMS::GetListAllGroups()
{
    QList<Group> listGroups;

    mtxAccess.lock();

    if(! dbLMS->DBisConnected())
    {
        mtxAccess.unlock();
        return listGroups;
    }

    listGroups = dbLMS->getListGroups();

    mtxAccess.unlock();
    return listGroups;
}

int ProviderDBLMS::newInstructor()
{
    return dbLMS->newInstructor();
}

int ProviderDBLMS::delInstructor(int id)
{
    return dbLMS->delInstructor(id);
}

int ProviderDBLMS::editInstructor(Instructor instructor)
{
    return dbLMS->editInstructor(instructor);
}

int ProviderDBLMS::newTrainee(int id_group)
{
    return dbLMS->newTrainee(id_group);
}

int ProviderDBLMS::delTrainee(int id)
{
    return dbLMS->delTrainee(id);
}

int ProviderDBLMS::editTrainee(Trainee trainee)
{
    return dbLMS->editTrainee(trainee);
}

int ProviderDBLMS::newGroup()
{
    return dbLMS->newGroup();
}

int ProviderDBLMS::delGroup(int id)
{
    return dbLMS->delGroup(id);
}

int ProviderDBLMS::editGroup(Group group)
{
    return dbLMS->editGroup(group);
}

int ProviderDBLMS::newTaskAMM(int id_trainee)
{
    return dbLMS->newTaskAMM(id_trainee);
}

int ProviderDBLMS::delTaskAMM(int id)
{
    return dbLMS->delTaskAMM(id);
}

int ProviderDBLMS::editTaskAMM(TaskAmmFim task)
{
    return dbLMS->editTaskAMM(task);
}

int ProviderDBLMS::newTaskFIM(int id_trainee)
{
    return dbLMS->newTaskFIM(id_trainee);
}

int ProviderDBLMS::delTaskFIM(int id)
{
    return dbLMS->delTaskFIM(id);
}

int ProviderDBLMS::editTaskFIM(TaskAmmFim task)
{
    return dbLMS->editTaskFIM(task);
}
