#include <QHeaderView>
#include <QResizeEvent>
#include "traineesview.h"

TraineesView::TraineesView(ConnectorToServer* connectorToServer, TypeView type, QWidget *parent):
    CommonView(connectorToServer, type, parent)
{
    typeObject = TypeObject::objTrainee;
    TypeUserDB = User::TypeUserDBTrainee;
}

void TraineesView::slot_NeedUpdateUI(bool treeInstructor, bool treeTrainee)
{
    updateButtons();

    if(authComplited)
    {
        if(treeTrainee)
            loadTraineesFromDB();
    }
}

void TraineesView::loadTraineesFromDB()
{
    mtxTreeWidget.lock();

    //Обновление дерева
    treeWidget->clear();

    QList <Group> listGroups;
    QList <Trainee> listTrainees;

    listGroups = connectorToServer->getListGroups();
    listTrainees = connectorToServer->getListTrainees();


    for(Group group : listGroups)
    {
        //Группа
        int cntChildsNotArchived = 0;
        QTreeWidgetItem *ItemGroup = new QTreeWidgetItem(treeWidget);
        ItemGroup->setText(ColumnsTreeUsers::clmn_ID, QString::number(group.getID()));
        ItemGroup->setText(ColumnsTreeUsers::clmn_Name, group.getName());
        ItemGroup->setIcon(ColumnsTreeUsers::clmn_Name, QIcon(QStringLiteral(":/resources/icons/group.png")));
        setItemColor(ItemGroup, QColor(220, 220, 220));


        //Обучаемые
        for(Trainee trainee : listTrainees)
        {
            if(trainee.getGroup().getID() != group.getID())
                continue;

            QTreeWidgetItem *ItemTrainee = new QTreeWidgetItem();

            ItemTrainee->setText(ColumnsTreeUsers::clmn_ID, QString::number(trainee.getID()));
            ItemTrainee->setText(ColumnsTreeUsers::clmn_Name, trainee.getName());
            ItemTrainee->setText(ColumnsTreeUsers::clmn_Login, trainee.getLogin());
            ItemTrainee->setText(ColumnsTreeUsers::clmn_Password, trainee.getPassword());
            ItemTrainee->setText(ColumnsTreeUsers::clmn_Class, trainee.getComputer().getClassroom().getName());
            ItemTrainee->setText(ColumnsTreeUsers::clmn_Computer, trainee.getComputer().getName());
            ItemTrainee->setText(ColumnsTreeUsers::clmn_IP_address, trainee.getComputer().getIpAddress());

            //Сокрытие пароля
            ItemTrainee->setText(ColumnsTreeUsers::clmn_Password, QStringLiteral("******"));

            if(trainee.getArchived())
            {//Архивный
                ItemTrainee->setText(ColumnsTreeUsers::clmn_Archived, tr("yes"));
                ItemTrainee->setIcon(ColumnsTreeUsers::clmn_Name, QIcon(QStringLiteral(":/resources/icons/archive.png")));
                setItemColorArchive(ItemTrainee);
            }
            else
            {//Не Архивный
                ItemTrainee->setText(ColumnsTreeUsers::clmn_Archived, tr("no"));
                ItemTrainee->setIcon(ColumnsTreeUsers::clmn_Name, QIcon(QStringLiteral(":/resources/icons/trainee.png")));
                setItemColorNoArchive(ItemTrainee);
                cntChildsNotArchived++;
            }

            if(trainee.getLoggedIn())
            {//Залогинен
                //ItemTrainee->setText(ColumnsTreeUsers::clmn_Logged, tr("yes"));
                ItemTrainee->setIcon(ColumnsTreeUsers::clmn_Logged, QIcon(QStringLiteral(":/resources/icons/circleGreen.png")));
            }
            else
            {//Не Залогинен
                //ItemTrainee->setText(ColumnsTreeUsers::clmn_Logged, tr("no"));
                ItemTrainee->setIcon(ColumnsTreeUsers::clmn_Logged, QIcon(QStringLiteral(":/resources/icons/circleGray.png")));
            }

            ItemGroup->addChild(ItemTrainee);

            //Скрываем архивных (при необходимости)
            if(trainee.getArchived())
                if(! archiveVisible)
                    ItemTrainee->setHidden(true);

            //Скрываем незалогиненых (при необходимости)
            if(! trainee.getLoggedIn())
                if(! notLoggedInVisible)
                    ItemTrainee->setHidden(true);
        }

        //if(! archiveVisible && cntChildsNotArchived == 0)
            //delete ItemGroup;
    }

    treeWidget->expandAll();

    /*
    //if(typeView == TypeView::control)
    {
        QTreeWidgetItem * item = treeWidget->topLevelItem(0);
        if(item != nullptr)
        {
            QTreeWidgetItem * itemChild = item->child(0);
            if(itemChild != nullptr)
                treeWidget->setCurrentItem(itemChild);
        }
    }*/

    if(typeObject == TypeObject::objGroup)
        setCurrentGroup(lastCurrentID);
    else
        setCurrentTrainee(lastCurrentID);

    treeWidget->sortItems(ColumnsTreeUsers::clmn_Name, Qt::SortOrder::AscendingOrder);

    mtxTreeWidget.unlock();

    waitAnimationWidget->hideWithStop();
}

void TraineesView::setCurrentGroup(int id)
{
    typeObject = TypeObject::objGroup;

    for(int i = 0; i < treeWidget->topLevelItemCount(); i++)
    {
        QTreeWidgetItem * item = treeWidget->topLevelItem(i);
        if(item != nullptr)
            if(item->text(ColumnsTreeUsers::clmn_ID).toInt() == id)
            {
                treeWidget->setCurrentItem(item);
                return;
            }
    }

    /*
    QTreeWidgetItem * item = treeWidget->topLevelItem(0);
    if(item != nullptr)
        treeWidget->setCurrentItem(item);
        */
}

void TraineesView::setCurrentTrainee(int id)
{
    typeObject = TypeObject::objTrainee;

    for(int i = 0; i < treeWidget->topLevelItemCount(); i++)
    {
        QTreeWidgetItem * item = treeWidget->topLevelItem(i);
        if(item != nullptr)
        {
            for (int j = 0; j < item->childCount(); j++)
            {
                QTreeWidgetItem * itemChild = item->child(j);
                if(itemChild != nullptr)
                    if(itemChild->text(ColumnsTreeUsers::clmn_ID).toInt() == id)
                    {
                        treeWidget->setCurrentItem(itemChild);
                        return;
                    }
            }//for (int j = 0; j < item->childCount(); j++)
        }
    }//for(int i = 0; i < treeWidget->topLevelItemCount(); i++)

    /*
    QTreeWidgetItem * item = treeWidget->topLevelItem(0);
    if(item != nullptr)
        treeWidget->setCurrentItem(item);
        */
}
