#include <QMessageBox>
#include <QCoreApplication>
#include <QSqlError>
#include "interfacedatabaselms.h"

InterfaceDataBaseLMS::InterfaceDataBaseLMS(QWidget* parent):
    QWidget(parent),
    DataBaseLMS()
{

}

void InterfaceDataBaseLMS::slot_LanguageChanged(QString language)
{
    qtLanguageTranslator.load(QString(QStringLiteral("translations/DataBaseLMS_")) + language, QStringLiteral("."));
    QCoreApplication::installTranslator(&qtLanguageTranslator);
}

bool InterfaceDataBaseLMS::ConnectionToDB()
{
    if(!createConnection())
    {
        QMessageBox::critical(this, dbName, tr("Connection error: ") + db->lastError().text());
        return false;
    }
    else
    {
        //QMessageBox::information(this, dbName, tr("Connection is successful!"));
        return true;
    }
}

bool InterfaceDataBaseLMS::DisConnectionFromDB()
{
    deleteConnection();
    //QMessageBox::information(this, dbName, tr("Disconnection is successful!"));
    return true;
}

bool InterfaceDataBaseLMS::DBisConnected()
{
    return isConnected();
}

//Инструкторы

bool InterfaceDataBaseLMS::AuthorizationInstructor(QString login, QString password)
{
    if(! transactionBegin())
        return false;

    if(int id = selectInstructorID(login, password))
    {
        if(updateInstructorLoggedIn(id, true))
            return transactionEnd();
    }

    transactionEnd();
    return false;
}

bool InterfaceDataBaseLMS::deAuthorizationInstructor(QString login)
{
    if(! transactionBegin())
        return false;

    if(int id = selectInstructorID(login))
    {
        if(updateInstructorLoggedIn(id, false))
            return transactionEnd();
    }

    transactionEnd();
    return false;
}

bool InterfaceDataBaseLMS::deAuthorizationAllInstructors()
{
    return updateAllInstructorsLoggedIn(false);
}

QString InterfaceDataBaseLMS::getNameInstructorByLogin(QString login)
{
    return selectInstructorNameByLogin(login);
}

QList<Instructor> InterfaceDataBaseLMS::getListInstructors()
{
    return selectAllInstructors();
}

Instructor InterfaceDataBaseLMS::getInstructor(int id)
{
    return selectInstructor(id);
}

int InterfaceDataBaseLMS::newInstructor()
{
    return insertInstructor();
}

int InterfaceDataBaseLMS::delInstructor(int id)
{
    return deleteInstructor(id);
}

int InterfaceDataBaseLMS::editInstructor(Instructor instructor)
{
    //Проверка корректности логина, имени, пароля
    QList<Instructor> listInstructors = selectAllInstructors();
    for(Instructor exist_instructor : listInstructors)
    {
        if(instructor.getName() == QStringLiteral("<instructor>"))
        {//Имя не корректно!
            QMessageBox::critical(this, tr("Editing error!"),
                                  tr("Unacceptable instructor name has been entered.\nThe changes will not be accepted."));
            return 0;
        }

        if(instructor.getLogin() == QStringLiteral("<login>"))
        {//Логин не корректен!
            QMessageBox::critical(this, tr("Editing error!"),
                                  tr("Unacceptable instructor login has been entered.\nThe changes will not be accepted."));
            return 0;
        }

        if(instructor.getPassword() == QStringLiteral("<password>"))
        {//Пароль не корректный!
            QMessageBox::critical(this, tr("Editing error!"),
                                  tr("Unacceptable instructor password has been entered.\nThe changes will not be accepted."));
            return 0;
        }

        if(instructor.getName() == exist_instructor.getName() && instructor.getID() != exist_instructor.getID())
        {//Имя уже существует
            QMessageBox::critical(this, tr("Editing error!"),
                                  tr("An existing instructor name has been entered."));
            return 0;
        }

        if(instructor.getLogin() == exist_instructor.getLogin() && instructor.getID() != exist_instructor.getID())
        {//Логин уже существует!
            QMessageBox::critical(this, tr("Editing error!"),
                                  tr("An existing instructor login has been entered.\nThe changes will not be accepted."));
            return 0;
        }
    }

    return updateInstructor(instructor);
}

bool InterfaceDataBaseLMS::isAdminInstructor(int id)
{
    return selectInstructorIsAdmin(id);
}

bool InterfaceDataBaseLMS::isArchivedInstructor(int id)
{
    return selectInstructorArchived(id);
}

bool InterfaceDataBaseLMS::isLoggedInInstructor(int id)
{
    return selectInstructorLoggedIn(id);
}


//Инструкторы

bool InterfaceDataBaseLMS::AuthorizationTrainee(QString login, QString password, QString classroom_name, QString computer_name)
{
    if(! transactionBegin())
        return false;

    if(int id = selectTraineeID(login, password))
    {
        if(updateTraineeLoggedIn(id, true))
            return transactionEnd();
    }

    transactionEnd();
    return false;
}

bool InterfaceDataBaseLMS::deAuthorizationTrainee(QString login)
{
    if(! transactionBegin())
        return false;

    if(int id = selectTraineeID(login))
    {
        if(updateTraineeLoggedIn(id, false))
            return transactionEnd();
    }

    transactionEnd();
    return false;
}

bool InterfaceDataBaseLMS::deAuthorizationAllTrainees()
{
    return updateAllTraineesLoggedIn(false);
}

QList<Task> InterfaceDataBaseLMS::getTasksTrainee(int id)
{
    return selectTasksOfTrainee(id);
}

QString InterfaceDataBaseLMS::getNameTraineeOnComputer(QString computer_name)
{
    return selectTraineeNameOnComputer(computer_name);
}

Trainee InterfaceDataBaseLMS::getTraineeOnComputer(QString computer_name)
{
    return selectTraineeOnComputer(computer_name);
}

QString InterfaceDataBaseLMS::getNameTraineeByLogin(QString login)
{
    return selectTraineeNameByLogin(login);
}

QList<Trainee> InterfaceDataBaseLMS::getListTraineesInGroup(int id)
{
    return selectAllTraineesInGroup(id);
}

QList<Group> InterfaceDataBaseLMS::getListGroups()
{
    return selectAllGroups();
}

QList<Trainee> InterfaceDataBaseLMS::getListTrainees()
{
    return selectAllTrainees();
}

Trainee InterfaceDataBaseLMS::getTrainee(int id)
{
    return selectTrainee(id);
}

Group InterfaceDataBaseLMS::getGroup(int id)
{
    return selectGroup(id);
}

int InterfaceDataBaseLMS::newGroup()
{
    return insertGroup();
}

int InterfaceDataBaseLMS::delGroup(int id)
{
    return deleteGroup(id);
}

int InterfaceDataBaseLMS::editGroup(Group group)
{
    //Проверка корректности имени
    QList<Group> listGroups = selectAllGroups();
    for(Group exist_group : listGroups)
    {
        if(group.getName() == QStringLiteral("<group>"))
        {//Имя не корректно!
            QMessageBox::critical(this, tr("Editing error!"),
                                  tr("Unacceptable group name has been entered.\nThe changes will not be accepted."));
            return 0;
        }

        if(group.getName() == exist_group.getName() && group.getID() != exist_group.getID())
        {//Имя уже существует
            QMessageBox::critical(this, tr("Editing error!"),
                                  tr("An existing group name has been entered.\nThe changes will not be accepted."));
            return 0;
        }
    }

    return updateGroup(group);
}

int InterfaceDataBaseLMS::newTrainee(int id_group)
{
    return insertTrainee(id_group);
}

int InterfaceDataBaseLMS::delTrainee(int id)
{
    return deleteTrainee(id);
}

int InterfaceDataBaseLMS::editTrainee(Trainee trainee)
{
    //Проверка корректности логина, имени, пароля
    QList<Trainee> listTrainees = selectAllTrainees();
    for(Trainee exist_trainee : listTrainees)
    {
        if(trainee.getName() == QStringLiteral("<trainee>"))
        {//Имя не корректно!
            QMessageBox::critical(this, tr("Editing error!"),
                                  tr("Unacceptable trainee name has been entered.\nThe changes will not be accepted."));
            return 0;
        }

        if(trainee.getLogin() == QStringLiteral("<login>"))
        {//Логин не корректен!
            QMessageBox::critical(this, tr("Editing error!"),
                                  tr("Unacceptable trainee login has been entered.\nThe changes will not be accepted."));
            return 0;
        }

        if(trainee.getPassword() == QStringLiteral("<password>"))
        {//Пароль не корректный!
            QMessageBox::critical(this, tr("Editing error!"),
                                  tr("Unacceptable trainee password has been entered.\nThe changes will not be accepted."));
            return 0;
        }

        if(trainee.getName() == exist_trainee.getName() && trainee.getID() != exist_trainee.getID())
        {//Имя уже существует
            QMessageBox::critical(this, tr("Editing error!"),
                                  tr("An existing trainee name has been entered."));
            return 0;
        }

        if(trainee.getLogin() == exist_trainee.getLogin() && trainee.getID() != exist_trainee.getID())
        {//Логин уже существует!
            QMessageBox::critical(this, tr("Editing error!"),
                                  tr("An existing trainee login has been entered.\nThe changes will not be accepted."));
            return 0;
        }
    }

    return updateTrainee(trainee);
}

bool InterfaceDataBaseLMS::isArchivedTrainee(int id)
{
    return selectTraineeArchived(id);
}

bool InterfaceDataBaseLMS::isLoggedInTrainee(int id)
{
    return selectTraineeLoggedIn(id);
}
