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

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

void TraineesView::resizeEvent(QResizeEvent *event)
{
    int width = treeWidget->width();

    treeWidget->setColumnWidth(ColumnsTreeTrainees::clmn_ID, 50);
    treeWidget->setColumnWidth(ColumnsTreeTrainees::clmn_Login, 100);
    treeWidget->setColumnWidth(ColumnsTreeTrainees::clmn_Password, 100);
    treeWidget->setColumnWidth(ColumnsTreeTrainees::clmn_Class, 100);
    treeWidget->setColumnWidth(ColumnsTreeTrainees::clmn_Computer, 100);
    treeWidget->setColumnWidth(ColumnsTreeTrainees::clmn_IP_address, 130);
    treeWidget->setColumnWidth(ColumnsTreeTrainees::clmn_Archived, 80);
    treeWidget->setColumnWidth(ColumnsTreeTrainees::clmn_Logged, 100);

    int widthTrainee;

    if(typeView == TypeView::onlyView)
    {//onlyView
        widthTrainee = width - (230 + 10);
    }
    else
    {//control
        if(adminMode)
            widthTrainee = width - (760 + 10);
        else
            widthTrainee = width - (630 + 10);
    }
    if(widthTrainee < 250)
        widthTrainee = 250;

    treeWidget->setColumnWidth(ColumnsTreeTrainees::clmn_Trainee, widthTrainee);

    QSize size = event->size();
    waitAnimationWidget->resize(size);
}

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

    if(treeTrainee)
        loadTraineesFromDB();
}

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

    treeWidget->setColumnCount(clmn_count);

    reSetHeadTreeWidget();

    if(typeView == TypeView::onlyView)
    {//onlyView
        archiveVisible = false;

        treeWidget->setColumnHidden(ColumnsTreeTrainees::clmn_ID, true);
        treeWidget->setColumnHidden(ColumnsTreeTrainees::clmn_Password, true);
        treeWidget->setColumnHidden(ColumnsTreeTrainees::clmn_Archived, true);

        treeWidget->setColumnHidden(ColumnsTreeTrainees::clmn_Class, true);
        treeWidget->setColumnHidden(ColumnsTreeTrainees::clmn_Computer, true);
        treeWidget->setColumnHidden(ColumnsTreeTrainees::clmn_IP_address, true);
    }
    else
    {//control
        archiveVisible = true;

        if(adminMode)
        {
            treeWidget->setColumnHidden(ColumnsTreeTrainees::clmn_ID, false);
            treeWidget->setColumnHidden(ColumnsTreeTrainees::clmn_Archived, false);
        }
        else
        {
            treeWidget->setColumnHidden(ColumnsTreeTrainees::clmn_ID, true);
            treeWidget->setColumnHidden(ColumnsTreeTrainees::clmn_Archived, true);
        }
    }

    treeWidget->setSortingEnabled(true);
    treeWidget->sortItems(ColumnsTreeTrainees::clmn_Trainee, Qt::SortOrder::AscendingOrder);

    mtxTreeWidget.unlock();
}

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(ColumnsTreeTrainees::clmn_ID, QString::number(group.getID()));
        ItemGroup->setText(ColumnsTreeTrainees::clmn_Trainee, group.getName());
        ItemGroup->setIcon(ColumnsTreeTrainees::clmn_Trainee, QIcon(QStringLiteral(":/resources/icons/group.png")));
        setItemColor(ItemGroup, QColor(170, 190, 170));

        //ItemGroup->set Property("greenButton", QVariant(true));
        //((QAbstractItemView*)ItemGroup)->setIconSize(QSize(32, 32));

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

            QTreeWidgetItem *ItemTrainee = new QTreeWidgetItem();

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

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

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

            if(trainee.getLoggedIn())
            {//Залогинен
                //ItemTrainee->setText(ColumnsTreeTrainees::clmn_Logged, tr("yes"));
                ItemTrainee->setIcon(ColumnsTreeTrainees::clmn_Logged, QIcon(QStringLiteral(":/resources/icons/circleGreen.png")));
            }
            else
            {//Не Залогинен
                //ItemTrainee->setText(ColumnsTreeTrainees::clmn_Logged, tr("no"));
                ItemTrainee->setIcon(ColumnsTreeTrainees::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(ColumnsTreeTrainees::clmn_Trainee, Qt::SortOrder::AscendingOrder);

    mtxTreeWidget.unlock();

    waitAnimationWidget->hideWithStop();
}

void TraineesView::reSetHeadTreeWidget()
{
    QStringList listHeaders = {tr("Trainee"), tr("Login"), tr("Password"), tr("Class"), tr("Computer"), tr("IP address"), tr("Archived"), tr("Logged"), tr("ID")};
    treeWidget->setHeaderLabels(listHeaders);
}

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(ColumnsTreeTrainees::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(ColumnsTreeTrainees::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);
        */
}
