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

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

}

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

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

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


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

bool InterfaceDataBaseLMS::authorizationInstructor(QString login, QString password)
{
    if(int id = selectUserID(DataBaseLMS::TypeUserDBInstructor, login, password))
    {
        if(selectUserArchived(DataBaseLMS::TypeUserDBInstructor, id) || selectUserLoggedIn(DataBaseLMS::TypeUserDBInstructor, id))
            return false;

        if(updateUserLoggedIn(DataBaseLMS::TypeUserDBInstructor, id, true))
            return true;
    }
    return false;
}

bool InterfaceDataBaseLMS::deAuthorizationInstructor(QString login)
{
    if(int id = selectUserID(DataBaseLMS::TypeUserDBInstructor, login))
    {
        if(updateUserLoggedIn(DataBaseLMS::TypeUserDBInstructor, id, false))
            return true;
    }
    return false;
}

bool InterfaceDataBaseLMS::deAuthorizationAllInstructors()
{
    return updateAllUsersLoggedIn(DataBaseLMS::TypeUserDBInstructor, false);
}

QString InterfaceDataBaseLMS::getNameInstructorByLogin(QString login)
{
    return selectUserNameByLogin(DataBaseLMS::TypeUserDBInstructor, login);
}

int InterfaceDataBaseLMS::getIdInstructorByLogin(QString login)
{
    return selectUserID(DataBaseLMS::TypeUserDBInstructor, 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)
{
    return updateInstructor(instructor);
}

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

bool InterfaceDataBaseLMS::isArchivedInstructor(int id)
{
    return selectUserArchived(DataBaseLMS::TypeUserDBInstructor, id);
}

bool InterfaceDataBaseLMS::isLoggedInInstructor(int id)
{
    return selectUserLoggedIn(DataBaseLMS::TypeUserDBInstructor, id);
}


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

bool InterfaceDataBaseLMS::authorizationTrainee(QString login, QString password, QString classroom_name, QString computer_name)
{
    if(int id = selectUserID(DataBaseLMS::TypeUserDBTrainee, login, password))
    {
        if(selectUserArchived(DataBaseLMS::TypeUserDBTrainee, id) || selectUserLoggedIn(DataBaseLMS::TypeUserDBTrainee, id))
            return false;

        if(updateUserLoggedIn(DataBaseLMS::TypeUserDBTrainee, id, true))
            return true;
    }
    return false;
}

bool InterfaceDataBaseLMS::deAuthorizationTrainee(QString login)
{
    if(int id = selectUserID(DataBaseLMS::TypeUserDBTrainee, login))
    {
        if(updateUserLoggedIn(DataBaseLMS::TypeUserDBTrainee, id, false))
            return true;
    }
    return false;
}

bool InterfaceDataBaseLMS::deAuthorizationAllTrainees()
{
    return updateAllUsersLoggedIn(DataBaseLMS::TypeUserDBTrainee, false);
}

int InterfaceDataBaseLMS::entryTraineeOnSimulator(int id_trainee)
{
    TimingOfTrainee timing(id_trainee);

    timing = selectTimingTrainee(id_trainee);

    if(!timing.getID())
    {//Еще нет записи
        int timing_id = insertTimingTrainee(id_trainee);
        timing.setID(timing_id);

        if(!timing_id)
            return  0;
    }

    timing.fixEntry();

    return updateTimingTrainee(id_trainee, timing);
}

int InterfaceDataBaseLMS::exitTraineeFromSimulator(int id_trainee)
{
    TimingOfTrainee timing(id_trainee);

    timing = selectTimingTrainee(id_trainee);

    if(!timing.getID())
    {//Еще нет записи
        int timing_id = insertTimingTrainee(id_trainee);
        timing.setID(timing_id);

        if(!timing_id)
            return  0;
    }

    timing.fixExit();

    return updateTimingTrainee(id_trainee, timing);
}

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 selectUserNameByLogin(DataBaseLMS::TypeUserDBTrainee, login);
}

int InterfaceDataBaseLMS::getIdTraineeByLogin(QString login)
{
    return selectUserID(DataBaseLMS::TypeUserDBTrainee, 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)
{
    return updateGroup(group);
}

int InterfaceDataBaseLMS::newTaskAMM(TaskAmmFim task, int id_trainee)
{
    return insertTaskAMM(task, id_trainee);
}

int InterfaceDataBaseLMS::delTaskAMM(int id)
{
    return deleteTaskAMM(id);
}

int InterfaceDataBaseLMS::editTaskAMM(TaskAmmFim task)
{
    return updateTaskAMM(task);
}

QList<TaskAmmFim> InterfaceDataBaseLMS::getListTasksAMMofTrainee(int id_trainee)
{
    return selectTasksAMMofTrainee(id_trainee);
}

QList<TaskAmmFim> InterfaceDataBaseLMS::getListTasksFIMofTrainee(int id_trainee)
{
    return selectTasksFIMofTrainee(id_trainee);
}

TaskAmmFim InterfaceDataBaseLMS::getTaskAMMbyID(int id_task)
{
    return selectTaskAMMbyID(id_task);
}

TaskAmmFim InterfaceDataBaseLMS::getTaskFIMbyID(int id_task)
{
    return selectTaskFIMbyID(id_task);
}

int InterfaceDataBaseLMS::newTaskFIM(TaskAmmFim task, int id_trainee)
{
    return insertTaskFIM(task, id_trainee);
}

int InterfaceDataBaseLMS::delTaskFIM(int id)
{
    return deleteTaskFIM(id);
}

int InterfaceDataBaseLMS::editTaskFIM(TaskAmmFim task)
{
    return updateTaskFIM(task);
}

int InterfaceDataBaseLMS::replaceReportFIM(TaskAmmFim task)
{
    return updateReportFIMforTask(task);
}

int InterfaceDataBaseLMS::changeStatusTaskFIM(int id_task, QString status)
{
    return updateStatusTaskFIM(id_task, status);
}

int InterfaceDataBaseLMS::changeStatusTaskAMM(int id_task, QString status)
{
    return updateStatusTaskAMM(id_task, status);
}

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

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

int InterfaceDataBaseLMS::editTrainee(Trainee trainee)
{
    return updateTrainee(trainee);
}

bool InterfaceDataBaseLMS::isArchivedTrainee(int id)
{
    return selectUserArchived(DataBaseLMS::TypeUserDBTrainee, id);
}

bool InterfaceDataBaseLMS::isLoggedInTrainee(int id)
{
    return selectUserLoggedIn(DataBaseLMS::TypeUserDBTrainee, id);
}
