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


CommonView::CommonView(InterfaceDataBaseLMS* dbLMS, TypeView type, QWidget *parent):
    QWidget(parent),
    dbLMS(dbLMS),
    treeWidget(nullptr),
    typeView(type),
    archiveVisible(false),
    notLoggedInVisible(false),
    adminMode(false),
    authComplited(false)
{
    treeWidget = new QTreeWidget();

    //QString name = treeWidget->objectName();
    //treeWidget->setObjectName("onlyView");
    //treeWidget->setAccessibleName("onlyView");

    //treeWidget->header()->setStyleSheet(QStringLiteral("font-size: 10pt;"));
    //treeWidget->setStyleSheet(QStringLiteral("font-size: 10pt;"
                                           //  "font-family: Tahoma;"));

    treeWidget->setIconSize(QSize(20, 20));
    treeWidget->setFocusPolicy(Qt::FocusPolicy::NoFocus);

    /*
    treeWidget->setStyleSheet("QTreeWidget {font-size: 10pt; font-family: Tahoma;}"
                              "QTreeWidget::item:selected {"
                              "background-color: #ddffdd; "
                              "border-style: solid solid solid solid; "
                              "color: #000000;}"
                              "QTreeWidgetItem::item:selected {"
                              "background-color: #ddaadd; "
                              "border-style: solid solid solid solid; "
                              "color: #000000;}");

    treeWidget->setStyleSheet("QTreeWidget {font-size: 10pt; font-family: Tahoma;}"
                              "QTreeWidget::item:selected {"
                              "background-color: #ddffdd; "
                              "border-style: solid solid solid solid; "
                              "color: #000000;}");*/

    updateMyStyleSheet();
}

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

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

void CommonView::setItemColor(QTreeWidgetItem *item, QColor color)
{
    //int columnCount = treeWidget->columnCount();

    for (int i = 0; i < item->columnCount(); i++)
        item->setBackground(i, color);
}

void CommonView::updateMyStyleSheet()
{
    QString styleSheet = loadStyleSheet();
    styleSheet = styleSheet.replace("\n", "");
    treeWidget->setStyleSheet(styleSheet);

    QString style = treeWidget->styleSheet();
    int i = 0;
}

QString CommonView::loadStyleSheet()
{
    QString fileName = "./styleSheetTreeWidget.css";
    QFile styleSheetTreeWidgetFile(fileName);
    if (!styleSheetTreeWidgetFile.open(QFile::ReadOnly | QFile::Text))
    {
        QMessageBox::critical(this, tr("Attention!"), tr("The file could not be opened ") + fileName);
        return QStringLiteral("");
    }
    else
    {
        QByteArray byteArray = styleSheetTreeWidgetFile.readAll();
        styleSheetTreeWidgetFile.close();

        QString style = byteArray;
        return style;
    }
}
