#include "editortrainees.h"
#include "viewertrainees.h"
#include "personalcardtrainee.h"
#include "dialogcardtrainee.h"
#include "ui_viewertrainees.h"

ViewerTrainees::ViewerTrainees(ConnectorToServer* connectorToServer, MessangerController* messangerController, QWidget *parent) :
    TraineesView(connectorToServer, CommonView::TypeView::onlyView, parent),    
    ammTasksWidgetCommon(nullptr),
    fimTasksWidgetCommon(nullptr),
    messangerController(messangerController),
    dlgRedactor(nullptr),
    dlgCardTrainee(nullptr),
    ui(new Ui::ViewerTrainees)
{
    ui->setupUi(this);    

    connect(treeWidget, &QTreeWidget::itemDoubleClicked, this, &ViewerTrainees::on_itemDoubleClicked);
    connect(treeWidget, &QTreeWidget::itemClicked, this, &ViewerTrainees::on_treeWidgetItemClicked);

    ui->horizontalLayout_1->addWidget(treeWidget);

    ammTasksWidgetCommon = new AMMtasksWidget(connectorToServer, TypeListTreeAMMFIM::listCommon, /*this*/nullptr);
    fimTasksWidgetCommon = new FIMtasksWidget(connectorToServer, TypeListTreeAMMFIM::listCommon, /*this*/nullptr);

    connect(connectorToServer, &ConnectorToServer::signal_UpdateTasksFIM, fimTasksWidgetCommon, &FIMtasksWidget::slot_NeedUpdateUI);
    connect(connectorToServer, &ConnectorToServer::signal_UpdateTasksAMM, ammTasksWidgetCommon, &AMMtasksWidget::slot_NeedUpdateUI);
    connect(this, &ViewerTrainees::signal_traineeSelected, fimTasksWidgetCommon, &FIMtasksWidget::slot_traineeSelected);
    connect(this, &ViewerTrainees::signal_traineeSelected, ammTasksWidgetCommon, &AMMtasksWidget::slot_traineeSelected);

    preparationTreeWidget();
    setNotLoggedInVisible(true);

    ui->btnEditorTrainees->setVisible(false);
}

ViewerTrainees::~ViewerTrainees()
{
    if(ammTasksWidgetCommon)
    {
        delete ammTasksWidgetCommon;
        ammTasksWidgetCommon = nullptr;
    }

    if(fimTasksWidgetCommon)
    {
        delete fimTasksWidgetCommon;
        fimTasksWidgetCommon = nullptr;
    }

    if(dlgRedactor)
        dlgRedactor->close();

    if(dlgCardTrainee)
        dlgCardTrainee->close();

    delete ui;
}

void ViewerTrainees::setAuthComplited(bool authComplited)
{
    this->authComplited = authComplited;
    updateButtons();
}

void ViewerTrainees::deactivate()
{
    if(dlgRedactor)
        dlgRedactor->close();

    if(dlgCardTrainee)
        dlgCardTrainee->close();

    CommonView::deactivate();

    ammTasksWidgetCommon->deactivate();
    fimTasksWidgetCommon->deactivate();
    updateButtons();
}

void ViewerTrainees::changeEvent(QEvent *event)
{
    // В случае получения события изменения языка приложения
    if (event->type() == QEvent::LanguageChange)
    {// переведём окно заново
        ui->retranslateUi(this);

        reSetHeadTreeWidget();
        slot_NeedUpdateUI(false, true);
    }
}

void ViewerTrainees::on_btnEditorTrainees_clicked()
{
    connectorToServer->sendQueryBlockAuth(true);

    dlgRedactor = new DialogRedactorTrainees(connectorToServer, adminMode, this);
    dlgRedactor->exec();

    if(dlgRedactor)
    {
        delete dlgRedactor;
        dlgRedactor = nullptr;
    }

    if(authComplited)
        loadTraineesFromDB();

    connectorToServer->sendQueryBlockAuth(false);
}

void ViewerTrainees::on_btnPersonalCard_clicked()
{
    if(lastCurrentID && typeObject == TypeObject::objTrainee)
    {
        dlgCardTrainee = new DialogCardTrainee(connectorToServer, messangerController, lastCurrentID, ammTasksWidgetCommon, fimTasksWidgetCommon, this);
        dlgCardTrainee->exec();

        ammTasksWidgetCommon->setParent(this);
        fimTasksWidgetCommon->setParent(this);

        if(dlgCardTrainee)
        {
            delete dlgCardTrainee;
            dlgCardTrainee = nullptr;
        }
    }
}

void ViewerTrainees::on_itemDoubleClicked(QTreeWidgetItem *item, int column)
{
    if(item == nullptr)
        return;

    if(item->childCount() == 0)
    {//Выбран обучаемый
        QString login = item->text(ColumnsTreeUsers::clmn_Login);
        if(login != "")
        {
            int id_trainee = connectorToServer->getIdTraineeByLogin(login);

            dlgCardTrainee = new DialogCardTrainee(connectorToServer, messangerController, id_trainee, ammTasksWidgetCommon, fimTasksWidgetCommon, this);
            dlgCardTrainee->exec();

            ammTasksWidgetCommon->setParent(this);
            fimTasksWidgetCommon->setParent(this);

            if(dlgCardTrainee)
            {
                delete dlgCardTrainee;
                dlgCardTrainee = nullptr;
            }
        }
    }
}

void ViewerTrainees::on_treeWidgetItemClicked(QTreeWidgetItem *item, int column)
{
    if(item == nullptr)
        return;

    if(item->childCount() == 0)
    {//Выбран обучаемый
        typeObject = TypeObject::objTrainee;

        QString login = item->text(ColumnsTreeUsers::clmn_Login);
        //if(login != "")
        {
            int newCurrentID = connectorToServer->getIdTraineeByLogin(login);

            //if(newCurrentID == lastCurrentID)
                //return;

            lastCurrentID = newCurrentID;

            Q_EMIT signal_traineeSelected(newCurrentID);
        }
    }
    else
    {//Выбрана группа
        typeObject = TypeObject::objGroup;

        int id_group = item->text(ColumnsTreeUsers::clmn_ID).toInt();

        lastCurrentID = id_group;
        typeObject = TypeObject::objGroup;

        QString login = "";
        Q_EMIT signal_traineeSelected(0);
    }
}

void ViewerTrainees::updateButtons()
{
    if(authComplited)
    {
        ui->btnEditorTrainees->setEnabled(true);
    }
    else
    {
        ui->btnEditorTrainees->setEnabled(false);
    }
}
