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

TraineesView::TraineesView(InterfaceDataBaseLMS* dbLMS, TypeView type, QWidget *parent):
    CommonView(dbLMS, type, parent)
{

}

void TraineesView::slot_NeedUpdateUI(bool treeInstructor, bool treeTrainee)
{
    if(typeView == TypeView::onlyView)
    {
        if(adminMode)
            archiveVisible = false;
        else
            archiveVisible = false;
    }
    else
    {
        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);
    }

    updateButtons();

    if(treeTrainee)
        loadTraineesFromDB();
}

void TraineesView::preparationTreeWidget(/*QTreeWidget *tree*/)
{
    mtxTreeWidget.lock();

    treeWidget->setColumnCount(10);

    reSetHeadTreeWidget();

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


    if(typeView == TypeView::onlyView)
    {//onlyView
        treeWidget->setColumnHidden(ColumnsTreeTrainees::clmn_ID, true);
        //treeWidget->setColumnHidden(ColumnsTreeTrainees::clmn_Login, true);
        treeWidget->setColumnHidden(ColumnsTreeTrainees::clmn_Password, true);
        treeWidget->setColumnHidden(ColumnsTreeTrainees::clmn_Archived, true);

        if(adminMode)
            archiveVisible = false;
        else
            archiveVisible = false;

        notLoggedInVisible = true;
    }
    else
    {//control
        archiveVisible = true;
        notLoggedInVisible = true;

        if(adminMode)
        {

        }
        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();

    if(!dbLMS->DBisConnected())
    {
        mtxTreeWidget.unlock();
        return;
    }

    QList <Group> listGroups;
    QList <Trainee> listTrainees;
    //dbLMS->transactionBegin();
    listGroups = dbLMS->getListGroups();
    listTrainees = dbLMS->getListTrainees();
    //dbLMS->transactionEnd();

    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(":/icons/group.png")));
        setItemColor(ItemGroup, QColor(170, 190, 170));
        //((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(":/icons/traineeArchive.png")));
                setItemColorArchive(ItemTrainee);
            }
            else
            {//Не Архивный
                ItemTrainee->setText(ColumnsTreeTrainees::clmn_Archived, tr("no"));
                ItemTrainee->setIcon(ColumnsTreeTrainees::clmn_Trainee, QIcon(QStringLiteral(":/icons/trainee.png")));
                setItemColorNoArchive(ItemTrainee);
                cntChildsNotArchived++;
            }

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

            QString tasksStr;
            for(Task task: trainee.getTasks())
            {
                tasksStr += task.getName() + QStringLiteral("; ");
            }
            ItemTrainee->setText(ColumnsTreeTrainees::clmn_Tasks, tasksStr);

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

    mtxTreeWidget.unlock();
}

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