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

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

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

    treeWidget->setColumnWidth(ColumnsTreeInsructors::clmn_ID, 50);
    treeWidget->setColumnWidth(ColumnsTreeInsructors::clmn_Login, 100);
    treeWidget->setColumnWidth(ColumnsTreeInsructors::clmn_Password, 100);
    treeWidget->setColumnWidth(ColumnsTreeInsructors::clmn_Administrator, 120);
    treeWidget->setColumnWidth(ColumnsTreeInsructors::clmn_Archived, 80);
    treeWidget->setColumnWidth(ColumnsTreeInsructors::clmn_Logged, 100);

    int widthInstructor;

    if(typeView == TypeView::onlyView)
    {//onlyView
        widthInstructor = width - (230 + 10);
    }
    else
    {//control
        if(adminMode)
            widthInstructor = width - (550 + 10);
        else
            widthInstructor = width - (420 + 10);
    }
    if(widthInstructor < 250)
        widthInstructor = 250;

    treeWidget->setColumnWidth(ColumnsTreeInsructors::clmn_Instructor, widthInstructor);

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

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

    if(treeInstructor)
        loadInstructorsFromDB();
}

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

    treeWidget->setColumnCount(clmn_count);

    reSetHeadTreeWidget();    

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

        treeWidget->setColumnHidden(ColumnsTreeInsructors::clmn_ID, true);
        treeWidget->setColumnHidden(ColumnsTreeInsructors::clmn_Password, true);
        treeWidget->setColumnHidden(ColumnsTreeInsructors::clmn_Archived, true);
        treeWidget->setColumnHidden(ColumnsTreeInsructors::clmn_Administrator, true);
    }
    else
    {//control
        archiveVisible = true;

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

    treeWidget->setSortingEnabled(true);
    treeWidget->sortItems(ColumnsTreeInsructors::clmn_Instructor, Qt::SortOrder::AscendingOrder);

    mtxTreeWidget.unlock();
}

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

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

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

    listInstructors = connectorToServer->getListInstructors();

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

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

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

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

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

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

    mtxTreeWidget.unlock();

    waitAnimationWidget->hideWithStop();
}

void InstructorsView::reSetHeadTreeWidget()
{
    QStringList listHeaders = {tr("Instructor"), tr("Login"), tr("Password"), tr("Administrator"), tr("Archived"), tr("Logged"), tr("ID")};
    treeWidget->setHeaderLabels(listHeaders);
}

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

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

