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

#include <QMessageBox>
#include <QResizeEvent>

VersionSelectWidget::VersionSelectWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::VersionSelectWidget),
    selectedVersion(nullptr),
    waitAnimationWidget(nullptr),
    recognizeSystem(nullptr)
{
    ui->setupUi(this);
    setWindowFlags(Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
    setAttribute(Qt::WA_ShowModal,true);
}

void VersionSelectWidget::initialize(SendSystem *sendSystem,VersionContainer *versionContainer,NotifyController *notifyController, RecognizeSystem *recognizeSystem)
{
    this->recognizeSystem = recognizeSystem;

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

    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);
    connect(this,&VersionSelectWidget::sigSendNotify,notifyController,&NotifyController::showWarning,Qt::AutoConnection);

    connect(recognizeSystem,&RecognizeSystem::sigAnimationActivated,this,&VersionSelectWidget::slot_activateLoadAnimation,Qt::AutoConnection);

    this->versionContainer = versionContainer;
    hide();
    setWindowTitle(tr("Version control"));
}

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_itemClicked(QListWidgetItem *item)
{
    foreach(StreamingVersionData *data,*serverDataList)
    {
        if(data->getViewName() == item->text())
        {
            QString info = tr("Version name: ") + data->getViewName() + "\n";
            info.append(tr("Created: ") + data->getCreateData().toString() + "\n");
            info.append(tr("Changeable: ") + changableText(data->getIsChangeable()) + "\n");
            info.append(tr("Author: ") + data->getAuthor());
            ui->infoValue->setText(info);
            selectedVersion = data;
        }
    }
}

QString VersionSelectWidget::changableText(bool flag)
{
    if(flag) return tr("Yes");
    else return tr("No");
}


void VersionSelectWidget::on_createDuplicateButton_clicked()
{

    if (selectedVersion == nullptr)
    {
        emit sigSendNotify(tr("Version not selected"));
        return;
    }

    DialogNewVersion *dlgNewVersion = new DialogNewVersion(this);
    dlgNewVersion->initialize(this,selectedVersion->getViewName());
    dlgNewVersion->exec();
    delete dlgNewVersion;
}

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

    if (selectedVersion == nullptr)
    {
        emit sigSendNotify(tr("Version not selected"));
        return;
    }

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

void VersionSelectWidget::on_DeleteVersionButton_clicked()
{
    if (selectedVersion == nullptr)
    {
        emit sigSendNotify(tr("Version not selected"));
        return;
    }

        emit sigSendDeleteVersion(selectedVersion);
}

void VersionSelectWidget::on_switchServerVersionButton_clicked()
{
    if (selectedVersion == nullptr)
    {
        emit sigSendNotify(tr("Version not selected"));
        return;
    }

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

void VersionSelectWidget::slot_activateLoadAnimation(bool flag)
{
    if (flag)
    {
        waitAnimationWidget->showWithPlay();
    }
    else
    {
        waitAnimationWidget->hideWithStop();
    }
}

void VersionSelectWidget::setAuthor(QString name)
{
    authorName = name;
}

void VersionSelectWidget::resizeEvent(QResizeEvent *event)
{
    QSize size = event->size();
    waitAnimationWidget->resize(size);
}

VersionSelectWidget::~VersionSelectWidget()
{
    waitAnimationWidget->hideWithStop();

    delete waitAnimationWidget;

    delete ui;
}



