#include "versionselectwidget.h"
#include "ui_versionselectwidget.h"
#include "ui_versionselectwidget.h"

#include <QMessageBox>

VersionSelectWidget::VersionSelectWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::VersionSelectWidget),
    selectedVersion(nullptr)
{
    ui->setupUi(this);
}

void VersionSelectWidget::initialize(SendSystem *sendSystem,VersionContainer *versionContainer)
{
    connect(this,&VersionSelectWidget::sigSendSwitchVersion,sendSystem,&SendSystem::sendChangeVersion,Qt::AutoConnection);
    connect(this,&VersionSelectWidget::sigSendCopyVersion,sendSystem,&SendSystem::sendCopyVersion,Qt::AutoConnection);
    connect(this,&VersionSelectWidget::sigSendDeleteVersion,sendSystem,&SendSystem::sendDeleteVersion,Qt::AutoConnection);
    this->versionContainer = versionContainer;
    hide();
}

void VersionSelectWidget::fillView(QList<StreamingVersionData *> *serverData)
{
    show();
    ui->verListView->clear();
    serverDataList = serverData;
    ui->verValue->setText(versionContainer->getServerVersionData()->getViewName());

    foreach(StreamingVersionData *data,*serverData)
    {
        ui->verListView->addItem(data->getViewName());
    }
}

void VersionSelectWidget::on_verListView_itemDoubleClicked(QListWidgetItem *item)
{
    foreach(StreamingVersionData *data,*serverDataList)
    {
        if(data->getViewName() == item->text())
        {
            QString info = "Имя версии: " + data->getViewName() + "\n";
            info.append("Создан: " + data->getCreateData().toString());
            ui->infoValue->setText(info);
            selectedVersion = data;
        }
    }
}


void VersionSelectWidget::on_createDuplicateButton_clicked()
{
    NewVersionWidget *newVersionWidget = new NewVersionWidget;
    newVersionWidget->initialize(this,selectedVersion->getViewName());
    newVersionWidget->show();
}

void VersionSelectWidget::sendCopyEmit(QString newName)
{
    QString result = selectedVersion->getViewName() + ";" + newName;

    if (selectedVersion == nullptr)
    {
        showWarning(tr("Версия не выбрана"));
        return;
    }

    quint16 matchIndex = ui->verListView->findItems(newName,Qt::MatchFlag::MatchFixedString).length();

    if (matchIndex > 0)
    {
       showWarning(tr("Версия с таким именем уже существует"));
       return;
    }

        versionContainer->setLocalVersionData(selectedVersion);
        emit sigSendCopyVersion(result);
}

void VersionSelectWidget::on_DeleteVersionButton_clicked()
{
    if (selectedVersion == nullptr)
    {
        showWarning(tr("Версия не выбрана"));
        return;
    }

    if (selectedVersion->getViewName() == baseNamePackage)
    {
        showWarning(tr("Нельзя удалить базовый пакет"));
        return;
    }

    if (selectedVersion->getViewName() == versionContainer->getServerVersion())
    {
        showWarning(tr("Нельзя удалить активную версию"));
        return;
    }
        emit sigSendDeleteVersion(selectedVersion);
}

void VersionSelectWidget::on_switchServerVersionButton_clicked()
{
    if (selectedVersion == nullptr)
    {
        showWarning(tr("Версия не выбрана"));
        return;
    }

    versionContainer->setServerVersonData(selectedVersion);
    ui->verValue->setText(selectedVersion->getViewName());
    emit sigSendSwitchVersion(selectedVersion);
}

void VersionSelectWidget::showWarning(QString text)
{
    QMessageBox warning;
    warning.setText(text);
    warning.setIcon(QMessageBox::Warning);
    warning.setWindowTitle(tr("Ошибка"));
    warning.exec();
}

VersionSelectWidget::~VersionSelectWidget()
{
    delete ui;
}

