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

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

}

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

    updateButtons();

    if(treeInstructor)
        loadInstructorsFromDB();
}

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

    treeWidget->setColumnCount(7);

    reSetHeadTreeWidget();

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

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

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

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

        if(adminMode)
        {

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

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

    //Инструкторы
    QList<Instructor> listInstructors;
    listInstructors = dbLMS->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(":/icons/instructorArchive.png")));
            setItemColorArchive(ItemInstructor);
        }
        else
        {//Не Архивный
            ItemInstructor->setText(ColumnsTreeInsructors::clmn_Archived, tr("no"));
            ItemInstructor->setIcon(ColumnsTreeInsructors::clmn_Instructor, QIcon(QStringLiteral(":/icons/instructor.png")));
            setItemColorNoArchive(ItemInstructor);
        }

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

    mtxTreeWidget.unlock();
}

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

