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

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

void VersionSelectWidget::initialize(SendSystem *sendSystem)
{
    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);
    hide();
}

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

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

void VersionSelectWidget::fillCurrentVersionName(QString versionName)
{
    ui->verValue->setText(versionName);
}

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

void VersionSelectWidget::on_DeleteVersionButton_clicked()
{
    if (selectedVersion != nullptr)
        emit sigSendDeleteVersion(selectedVersion);
}

void VersionSelectWidget::on_switchServerVersionButton_clicked()
{
    if (selectedVersion != nullptr){
        emit sigSendSwitchVersion(selectedVersion);
    }

}

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

