#include "commonview.h"
#include <QFile>
#include <QMessageBox>
#include <QScrollBar>
#include <QResizeEvent>


CommonView::CommonView(ConnectorToServer* connectorToServer, TypeView type, QWidget *parent):
    QWidget(parent),
    connectorToServer(connectorToServer),
    treeWidget(nullptr),
    TypeUserDB(User::TypeUserDBInstructor),
    typeView(type),
    archiveVisible(false),
    notLoggedInVisible(true),
    adminMode(false),
    authComplited(false),
    lastCurrentID(0),
    typeObject(TypeObject::objInstructor),
    waitAnimationWidget(nullptr)
{
    treeWidget = new QTreeWidget();
    treeWidget->setFocusPolicy(Qt::FocusPolicy::NoFocus);

    treeWidget->setEnabled(false);

    treeWidget->setSortingEnabled(false);

    waitAnimationWidget = new WaitAnimationWidget;
    QMovie *movie = new QMovie(":/resources/icons/762.gif");
    waitAnimationWidget->initialize(movie,this);
}

CommonView::~CommonView()
{
    waitAnimationWidget->hideWithStop();
    delete waitAnimationWidget;
}

void CommonView::setItemColorArchive(QTreeWidgetItem *item)
{
    setItemColor(item,QColor(240, 240, 240));
}

void CommonView::setItemColorNoArchive(QTreeWidgetItem *item)
{
    setItemColor(item,QColor(255, 255, 255));
}

void CommonView::setItemColor(QTreeWidgetItem *item, QColor color)
{
    for (int i = 0; i < item->columnCount(); i++)
        item->setBackground(i, color);
}

int CommonView::calculateWidth_0Column()
{
    int widthHeader = treeWidget->width() - 20;
    int width0Column = 0;
    int widthSB = 0;
    if(treeWidget->verticalScrollBar()->isVisible())
        widthSB = treeWidget->verticalScrollBar()->size().width();
    int cntColumns = treeWidget->columnCount();
    int widthRightColumns = 0;
    for (int i = 1; i < cntColumns; i++)
    {
        if(! treeWidget->isColumnHidden(i))
        {//Колонка не скрыта
            int w = listWidthColumn.at(i);
            widthRightColumns += w;
        }
    }
    width0Column = widthHeader - widthRightColumns - widthSB;

    return width0Column;
}

void CommonView::setWidthColumnsTree()
{
    listWidthColumn.clear();

    treeWidget->setColumnWidth(ColumnsTreeUsers::clmn_Name, 100);
    listWidthColumn.append(100);
    treeWidget->setColumnWidth(ColumnsTreeUsers::clmn_Login, 100);
    listWidthColumn.append(100);
    treeWidget->setColumnWidth(ColumnsTreeUsers::clmn_Password, 100);
    listWidthColumn.append(100);

    treeWidget->setColumnWidth(ColumnsTreeUsers::clmn_Class, 100);
    listWidthColumn.append(100);
    treeWidget->setColumnWidth(ColumnsTreeUsers::clmn_Computer, 100);
    listWidthColumn.append(100);
    treeWidget->setColumnWidth(ColumnsTreeUsers::clmn_IP_address, 100);
    listWidthColumn.append(100);

    treeWidget->setColumnWidth(ColumnsTreeUsers::clmn_Administrator, 140);
    listWidthColumn.append(140);
    treeWidget->setColumnWidth(ColumnsTreeUsers::clmn_Archived, 100);
    listWidthColumn.append(100);
    treeWidget->setColumnWidth(ColumnsTreeUsers::clmn_Logged, 80);
    listWidthColumn.append(80);
    treeWidget->setColumnWidth(ColumnsTreeUsers::clmn_ID, 40);
    listWidthColumn.append(40);

    treeWidget->setColumnWidth(ColumnsTreeUsers::clmn_Name, calculateWidth_0Column());
}

void CommonView::reSetHeadTreeWidget()
{
    QStringList listHeaders = {tr("Name"), tr("Login"), tr("Password"), tr("Class"), tr("Computer"), tr("IP address"), tr("Administrator"), tr("Archived"), tr("Online"), tr("ID")};
    treeWidget->setHeaderLabels(listHeaders);
}

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

    treeWidget->setColumnCount(clmn_count);

    reSetHeadTreeWidget();

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

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

        if(adminMode)
        {
#ifdef PROJECT_TYPE_DEBUG
            treeWidget->setColumnHidden(ColumnsTreeUsers::clmn_ID, false);
#else
            treeWidget->setColumnHidden(ColumnsTreeUsers::clmn_ID, true);
#endif
        }
        else
        {
            treeWidget->setColumnHidden(ColumnsTreeUsers::clmn_ID, true);
        }
    }
    treeWidget->setColumnHidden(ColumnsTreeUsers::clmn_Administrator, true);
    treeWidget->setColumnHidden(ColumnsTreeUsers::clmn_Password, true);
    treeWidget->setColumnHidden(ColumnsTreeUsers::clmn_Class, true);
    treeWidget->setColumnHidden(ColumnsTreeUsers::clmn_Computer, true);
    treeWidget->setColumnHidden(ColumnsTreeUsers::clmn_IP_address, true);
    if(TypeUserDB == User::TypeUserDBTrainee)
        treeWidget->setColumnHidden(ColumnsTreeUsers::clmn_Administrator, true);

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

    mtxTreeWidget.unlock();
}

void CommonView::resizeEvent(QResizeEvent *event)
{
    setWidthColumnsTree();

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