#ifndef DATABASELMS_H
#define DATABASELMS_H

#include <QSqlDatabase>
#include <QMutex>
#include <QWidget>
#include <QTranslator>

#include "instructor.h"
#include "trainee.h"
#include "group.h"
#include "tasksAmmFim.h"
#include "timingoftrainee.h"

class DataBaseSettings
{
public:
    QString dbName = "";
    QString dbType = "";
    QString connectionName = "";
    QString dbUserName = "";
    QString dbPassword = "";
    QString dbHostName = "";
    int dbPort = 0;
};

class DataBaseLMS : public QObject
{
    Q_OBJECT

public:
    DataBaseLMS(QWidget *ownerWidget, QObject *parent = nullptr);
    ~DataBaseLMS();

public Q_SLOTS:
    void slot_LanguageChanged(QString language);

public:
    static const QString TypeUserDBInstructor;
    static const QString TypeUserDBTrainee;

protected:
    //Подключение
    bool createConnection();
    void deleteConnection();
    bool isConnected();

    //Юзер
    int selectUserID(QString type, QString login, QString password = QStringLiteral(""));
    QString selectUserNameByLogin(QString type, QString login);
    bool selectUserLoggedIn(QString type, int id_user);
    bool selectUserArchived(QString type, int id_user);
    int updateUserLoggedIn(QString type, int id_user, bool loggedIn);
    bool updateAllUsersLoggedIn(QString type, bool loggedIn);
    int updateUserArchived(QString type, int id_user, bool archived);

    //Инструктор
    QList<Instructor> selectAllInstructors();
    Instructor selectInstructor(int id_instructor);
    bool selectInstructorIsAdmin(int id_instructor);
    int insertInstructor();
    int insertInstructor(Instructor instructor);
    int deleteInstructor(int id_instructor);
    int updateInstructor(Instructor instructor);

    //Группа
    QList<Group> selectAllGroups();
    Group selectGroup(int id_group);
    int insertGroup();
    int insertGroup(Group group);
    int deleteGroup(int id_group);
    int updateGroup(Group group);

    //Обучаемый
    QList<Trainee> selectAllTrainees();
    Trainee selectTrainee(int id_trainee);
    QList<Trainee> selectAllTraineesInGroup(int id_group);
    QString selectTraineeNameOnComputer(QString computer_name);
    Trainee selectTraineeOnComputer(QString computer_name);
    int insertTrainee(int id_group);
    int insertTrainee(Trainee trainee);
    int deleteTrainee(int id_trainee);
    int updateTrainee(Trainee trainee);

    //Тайминг
    int insertTimingTrainee(int id_trainee);
    int updateTimingTrainee(int id_trainee, TimingOfTrainee timing);
    TimingOfTrainee selectTimingTrainee(int id_trainee);

    //Задача AMM
    int insertTaskAMM(TaskAmmFim task, int id_trainee);
    int updateTaskAMM(TaskAmmFim task);
    int updateStatusTaskAMM(int task_id, QString status);
    int deleteTaskAMM(int id_task);
    QList<TaskAmmFim> selectTasksAMMofTrainee(int id_trainee);
    TaskAmmFim selectTaskAMMbyID(int id_task);
    //Задача FIM
    int insertTaskFIM(TaskAmmFim task, int id_trainee);
    int updateTaskFIM(TaskAmmFim task);
    int updateStatusTaskFIM(int task_id, QString status);
    int deleteTaskFIM(int id_task);
    QList<TaskAmmFim> selectTasksFIMofTrainee(int id_trainee);
    TaskAmmFim selectTaskFIMbyID(int id_task);
    //int deleteReportFIMforTask(int task_id);
    //int insertReportFIMforTask(TaskAmmFim task);
    int updateReportFIMforTask(TaskAmmFim task);

private:
    DataBaseSettings getDataBaseSettings();
    int queryExecInt(QString queryStr);
    QString queryExecString(QString queryStr);
    bool queryExecBool(QString queryStr);
    bool queryExec(QString queryStr, QSqlQuery* query);
    void messageWarningErrorQuery(QString queryStr, QSqlQuery* query);

protected:
    QSqlDatabase* db;
    DataBaseSettings dbSettings;
    QTranslator qtLanguageTranslator;

private:
    QWidget* ownerWidget;
};

#endif // DATABASELMS_H
