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

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

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

    connect(treeWidget, &QTreeWidget::itemClicked, this, &ViewerTrainees::on_treeWidget_itemClicked);

    ui->horizontalLayout_1->addWidget(treeWidget);

    /*
    ammTasksWidget_pers0 = new AMMtasksWidget(connectorToServer, TypeListTreeAMMFIM::listForTrainee, this);
    fimTasksWidget_pers0 = new FIMtasksWidget(connectorToServer, TypeListTreeAMMFIM::listForTrainee, this);

    connect(this, &ViewerTrainees::signal_traineeSelected, fimTasksWidget_pers0, &FIMtasksWidget::slot_traineeSelected);
    connect(this, &ViewerTrainees::signal_traineeSelected, ammTasksWidget_pers0, &AMMtasksWidget::slot_traineeSelected);

    connect(connectorToServer, &ConnectorToServer::signal_UpdateTasksAMMforTrainee, ammTasksWidget_pers0, &AMMtasksWidget::slot_UpdateTasksAMMforTrainee);
    connect(connectorToServer, &ConnectorToServer::signal_UpdateTasksFIMforTrainee, fimTasksWidget_pers0, &FIMtasksWidget::slot_UpdateTasksFIMforTrainee);

    ui->verticalLayout_2->addWidget(ammTasksWidget_pers0);
    ui->verticalLayout_2->addWidget(fimTasksWidget_pers0);
    */

    //------
    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);

    //connect(fimTasksWidgetCommon, &FIMtasksWidget::signal_AssignedTask, this, &ViewerTrainees::slot_waitAnimationWidgetShowWithPlayFIM);
    //connect(ammTasksWidgetCommon, &AMMtasksWidget::signal_AssignedTask, this, &ViewerTrainees::slot_waitAnimationWidgetShowWithPlayAMM);

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

    preparationTreeWidget();
    setNotLoggedInVisible(true);

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

ViewerTrainees::~ViewerTrainees()
{
    delete ammTasksWidgetCommon;
    delete fimTasksWidgetCommon;

    delete ui;
}

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

void ViewerTrainees::deactivate()
{
    CommonView::deactivate();
    //ammTasksWidget_pers0->deactivate();
    //fimTasksWidget_pers0->deactivate();

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

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

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

void ViewerTrainees::on_btnEditorTrainees_clicked()
{
    Q_EMIT signal_BlockAutorization(true);

    EditorTrainees editorTraineesGroups(connectorToServer, adminMode);
    connect(connectorToServer, &ConnectorToServer::signal_UpdateDB, &editorTraineesGroups, &EditorTrainees::slot_NeedUpdateUI);

    QDialog* dialog = new QDialog(this);
    QHBoxLayout *layout = new QHBoxLayout(dialog);
    layout->addWidget(&editorTraineesGroups);
    dialog->setWindowTitle(tr("Editor of trainees"));
    dialog->setMinimumSize(1600, 800);
    dialog->exec();

    loadTraineesFromDB();

    Q_EMIT signal_BlockAutorization(false);
}

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

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


            PersonalCardTrainee cardTrainee(connectorToServer, messangerController, id_trainee, ammTasksWidgetCommon, fimTasksWidgetCommon, this);

            QDialog* dialog = new QDialog(this,
                                            Qt::WindowSystemMenuHint
                                          | Qt::WindowMaximizeButtonHint
                                          | Qt::WindowMinimizeButtonHint
                                          | Qt::WindowCloseButtonHint);

            QHBoxLayout *layout = new QHBoxLayout(dialog);
            layout->addWidget(&cardTrainee);
            dialog->setWindowTitle(tr("Personal card trainee"));
            dialog->setMinimumSize(1900, 1000);
            dialog->exec();

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

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

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

        QString login = item->text(ColumnsTreeTrainees::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(ColumnsTreeTrainees::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);
    }
}
