#include <QScrollBar>
#include "listsubproc.h"
#include "ui_listsubproc.h"

ListSubProc::ListSubProc(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ListSubProc),
    treeWidget(nullptr)
{
    ui->setupUi(this);

    ui->btnAssign->setObjectName("btnAssign");

    treeWidget = new QTreeWidget();
    ui->horizontalLayout_Tree->addWidget(treeWidget);

    preparationTreeWidget();
}

ListSubProc::~ListSubProc()
{
    delete treeWidget;
    delete ui;
}

void ListSubProc::closeEvent(QCloseEvent *event)
{

}

void ListSubProc::resizeEvent(QResizeEvent *event)
{
    setWidthColumnsTree();
}

void ListSubProc::changeEvent(QEvent *event)
{
    // В случае получения события изменения языка приложения
    if (event->type() == QEvent::LanguageChange)
    {// переведём окно заново
        ui->retranslateUi(this);

        reSetHeadTreeWidget();
    }
}

void ListSubProc::setTask(TaskAmmFim *task)
{
    this->task = *task;

    ui->lbl_Title->setText(task->ammProcedure.title);
    ui->lbl_DMCode->setText(task->ammProcedure.dmCode);

    QList<SubProc> list = task->listSubProc;

    for(SubProc subProc : list)
    {
        QTreeWidgetItem* item = new QTreeWidgetItem(treeWidget);
        //item->setIcon(0, QIcon(":/resources/icons/link.png"));
        item->setText(ColumnsTreeSubProc::clmnSP_title, subProc.getTitle());
        item->setText(ColumnsTreeSubProc::clmnSP_code, subProc.getDmCode());
        item->setText(ColumnsTreeSubProc::clmnSP_canplay,  subProc.getModeListStr());
        item->setToolTip(ColumnsTreeSubProc::clmnSP_title, subProc.getTitle());

        ModeList modeList = subProc.getModeList();
        if(modeList.autoM && (modeList.demo || modeList.train || modeList.exam))
        {
            item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
            item->setCheckState(ColumnsTreeSubProc::clmnSP_title, Qt::Checked);

            treeWidget->addTopLevelItem(item);
        }
        else
        {
            item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
            item->setCheckState(ColumnsTreeSubProc::clmnSP_title, Qt::Unchecked);

            //item->setFlags(item->flags() ^ Qt::ItemIsEnabled);

#ifdef PROJECT_TYPE_DEBUG
            treeWidget->addTopLevelItem(item);
#else
            item->setFlags(item->flags() ^ Qt::ItemIsEnabled);
#endif
        }
    }
}

bool ListSubProc::getListCheckedSubProc(QList<SubProc>* listSubProc)
{
    //Заполнение списка отмеченных ПП
    for (int i = 0; i < treeWidget->topLevelItemCount(); i++)
    {
        QTreeWidgetItem *item = treeWidget->topLevelItem(i);
        if(item->checkState(0) == Qt::Checked)
        {
            SubProc subProc;
            subProc.setDmCode(item->text(ColumnsTreeSubProc::clmnSP_code));
            subProc.setTitle(item->text(ColumnsTreeSubProc::clmnSP_title));
            subProc.setModeListStr(item->text(ColumnsTreeSubProc::clmnSP_canplay));
            listSubProc->append(subProc);
        }
    }

    return true;
}

void ListSubProc::preparationTreeWidget()
{
    treeWidget->setColumnCount(ColumnsTreeSubProc::clmnSP_count);

    reSetHeadTreeWidget();

#ifdef PROJECT_TYPE_DEBUG
    treeWidget->setColumnHidden(ColumnsTreeSubProc::clmnSP_canplay, false);
#else
    treeWidget->setColumnHidden(ColumnsTreeSubProc::clmnSP_canplay, true);
#endif
}

void ListSubProc::reSetHeadTreeWidget()
{
    QStringList listHeaders;
    listHeaders = QStringList{tr("Subprocedure"), tr("DM code"), tr("Canplay")};
    treeWidget->setHeaderLabels(listHeaders);
}

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

    treeWidget->setColumnWidth(ColumnsTreeSubProc::clmnSP_title, 100);
    listWidthColumn.append(100);
    treeWidget->setColumnWidth(ColumnsTreeSubProc::clmnSP_code, 250);
    listWidthColumn.append(250);
    treeWidget->setColumnWidth(ColumnsTreeSubProc::clmnSP_canplay, 80);
    listWidthColumn.append(80);

    treeWidget->setColumnWidth(ColumnsTreeSubProc::clmnSP_title, calculateWidth_0Column());
}

int ListSubProc::calculateWidth_0Column()
{
    int widthHeader = treeWidget->width() - 25;
    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 ListSubProc::on_btnAssign_clicked()
{
    emit signal_Accepted();
}
