#include <QHeaderView>
#include <QTranslator>
#include <QResizeEvent>
#include <QScrollBar>
#include "instructorsview.h"

InstructorsView::InstructorsView(ConnectorToServer* connectorToServer, TypeView type, QWidget *parent):
    CommonView(connectorToServer, type, parent)
{
    typeObject = TypeObject::objInstructor;
    TypeUserDB = User::TypeUserDBInstructor;
}

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

    if(authComplited)
    {
        if(treeInstructor)
            loadInstructorsFromDB();
    }
}

void InstructorsView::loadInstructorsFromDB()
{
    mtxTreeWidget.lock();

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

    //Инструкторы
    QList<Instructor> listInstructors;

    listInstructors = connectorToServer->getListInstructors();

    for(Instructor instructor : listInstructors)
    {
        QTreeWidgetItem *ItemInstructor = new QTreeWidgetItem(treeWidget);

        ItemInstructor->setText(ColumnsTreeUsers::clmn_ID, QString::number(instructor.getID()));
        ItemInstructor->setText(ColumnsTreeUsers::clmn_Name, instructor.getName());
        ItemInstructor->setText(ColumnsTreeUsers::clmn_Login, instructor.getLogin());
        ItemInstructor->setText(ColumnsTreeUsers::clmn_Password, instructor.getPassword());

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

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

        if(instructor.getIsAdmin())
        {//Админ
            ItemInstructor->setText(ColumnsTreeUsers::clmn_Administrator, tr("yes"));
            ItemInstructor->setIcon(ColumnsTreeUsers::clmn_Name, QIcon(QStringLiteral(":/resources/icons/admin.png")));
        }
        else
        {//Не Админ
            ItemInstructor->setText(ColumnsTreeUsers::clmn_Administrator, tr("no"));
        }

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

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

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

    treeWidget->expandAll();

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

    setCurrentInstructor(lastCurrentID);

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

    mtxTreeWidget.unlock();

    waitAnimationWidget->hideWithStop();
}

void InstructorsView::setCurrentInstructor(int id)
{
    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);
        */
}

