#include "personalcardtrainee.h"
#include "ui_personalcardtrainee.h"

PersonalCardTrainee::PersonalCardTrainee(ConnectorToServer* connectorToServer, MessangerController* messangerController, int id_trainee, AMMtasksWidget* ammTasksWidgetIn, FIMtasksWidget* fimTasksWidgetIn, QWidget *parent) :
    QWidget(parent),
    connectorToServer(connectorToServer),
    messangerController(messangerController),
    id_trainee(id_trainee),
    ammTasksWidget_personal(nullptr),
    fimTasksWidget_personal(nullptr),
    ammTasksWidget_common(ammTasksWidgetIn),
    fimTasksWidget_common(fimTasksWidgetIn),
    messangerWidget(nullptr),
    ui(new Ui::PersonalCardTrainee)
{
    ui->setupUi(this);

    loadInfo();

    ammTasksWidget_personal = new AMMtasksWidget(connectorToServer, TypeListTreeAMMFIM::listForTrainee, this);
    fimTasksWidget_personal = new FIMtasksWidget(connectorToServer, TypeListTreeAMMFIM::listForTrainee, this);

    connect(connectorToServer, &ConnectorToServer::signal_UpdateTasksAMMforTrainee, ammTasksWidget_personal, &AMMtasksWidget::slot_UpdateTasksAMMforTrainee);
    connect(connectorToServer, &ConnectorToServer::signal_UpdateTasksFIMforTrainee, fimTasksWidget_personal, &FIMtasksWidget::slot_UpdateTasksFIMforTrainee);

    connect(ammTasksWidget_personal, &AMMtasksWidget::signal_countTasksAMMforTraineeChanged, this, &PersonalCardTrainee::slot_countTasksAMMforTraineeChanged);
    connect(fimTasksWidget_personal, &FIMtasksWidget::signal_countTasksFIMforTraineeChanged, this, &PersonalCardTrainee::slot_countTasksFIMforTraineeChanged);

    connect(connectorToServer, &ConnectorToServer::signal_UpdateDB, this, &PersonalCardTrainee::slot_NeedUpdateUI);

    fimTasksWidget_personal->slot_traineeSelected(id_trainee);
    ammTasksWidget_personal->slot_traineeSelected(id_trainee);

    ui->verticalLayout_AMMpersonal->addWidget(ammTasksWidget_personal);
    ui->verticalLayout_FIMpersonal->addWidget(fimTasksWidget_personal);

    ui->verticalLayout_AMMcommon->addWidget(ammTasksWidget_common);
    ui->verticalLayout_FIMcommon->addWidget(fimTasksWidget_common);

    ui->groupBox_Messenger->setMaximumWidth(500);
    ui->groupBox_Messenger->setMinimumWidth(500);

    /* Messanger*/
    Trainee trainee = connectorToServer->getTrainee(id_trainee);
    messangerWidget = messangerController->newWidget(this, &trainee, ui->verticalLayout_Messenger);
}

PersonalCardTrainee::~PersonalCardTrainee()
{
    delete ammTasksWidget_personal;
    delete fimTasksWidget_personal;

    messangerController->deleteWidget(messangerWidget);

    delete ui;
}

void PersonalCardTrainee::loadInfo()
{
    Trainee trainee = connectorToServer->getTrainee(id_trainee);

    ui->lblName->setText(trainee.getName());

    if(trainee.getLoggedIn())
        ui->lblLoginIn->setPixmap(QPixmap(QStringLiteral(":/resources/icons/circleGreen.png")));
    else
        ui->lblLoginIn->setPixmap(QPixmap(QStringLiteral(":/resources/icons/circleGray.png")));

    //QList<TaskAmmFim> listTasksAMM = connectorToServer->getListTasksAMMforTrainee(id_trainee);
    //QList<TaskAmmFim> listTasksFIM = connectorToServer->getListTasksFIMforTrainee(id_trainee);

    //ui->lblCntAMM->setText(QString::number(listTasksAMM.count()));
    //ui->lblCntFIM->setText(QString::number(listTasksFIM.count()));

    TimingOfTrainee timing = trainee.getTiming();

    ui->lblTimeOperation->setText(timing.getOperatingTimeS());
    ui->lblLoginTime->setText(timing.getEntryTimeS());
    ui->lblTimeExit->setText(timing.getExitTimeS());
}

void PersonalCardTrainee::slot_countTasksAMMforTraineeChanged(int trainee_id, int count)
{
    if(id_trainee == trainee_id)
    {
        ui->lblCntAMM->setText(QString::number(count));
    }
}

void PersonalCardTrainee::slot_countTasksFIMforTraineeChanged(int trainee_id, int count)
{
    if(id_trainee == trainee_id)
    {
        ui->lblCntFIM->setText(QString::number(count));
    }
}

void PersonalCardTrainee::slot_NeedUpdateUI(bool treeInstructor, bool treeTrainee)
{
    if(treeTrainee)
    {
        //TODO Собственно обновление
        loadInfo();
    }
}
