
#include "widgetmanager.h"

WidgetManager::WidgetManager(QObject *parent) : QObject(parent)
{
    mainWindow = new MainWindow;
    updateWidget = new UpdateNotifyWidget;
    commonButtonGroupWidget = new CommonButtonGroupWidget;
    entryWidget = new EntryWidget;
    waitAnimationWidget = new WaitAnimationWidget;
    instructorButtonGroupWiget = new InstructorButtonGroupWidget;
    notifyController = new NotifyController;
}

void WidgetManager::initialize()
{
    updateWidget->setParent(mainWindow);
    waitAnimationWidget->setParent(mainWindow);
    notifyController->setParent(mainWindow);

    mainWindow->addWidgetToChangeGroup(commonButtonGroupWidget);
    mainWindow->addWidgetToInteractiveGroup(entryWidget);

    updateWidget->initialize(mainWindow->pos());
    entryWidget->initialize();
    commonButtonGroupWidget->initialize();
    //activateLoadingAnimation(true);

    binding();
}

void WidgetManager::binding()
{
    connect(updateWidget,&UpdateNotifyWidget::sigShowMainFrame,this,&WidgetManager::showMainFrame,Qt::AutoConnection);
    connect(commonButtonGroupWidget,&CommonButtonGroupWidget::sigUpdateCommonWidget,mainWindow,&MainWindow::slotCommonWidgetState);
    connect(mainWindow,&MainWindow::sigShowSettings,this,&WidgetManager::showSettings,Qt::AutoConnection);
}
void WidgetManager::slotSetLoadSettings(ServerSettings *settings)
{
    mainWindow->setLoadSettings(settings);
    entryWidget->fillSettings(settings);
}

void WidgetManager::setLostConnectionState()
{
    commonButtonGroupWidget->updateProgressBar(0);
    mainWindow->slotConnectionState(false);
    waitAnimationWidget->hideWithStop();
    entryWidget->disconnectState();
    updateWidget->hide();
}

void WidgetManager::setLoginSuccess()
{
    entryWidget->showLoginWidget(false);
    mainWindow->setLoginSuccessState();
    activateLoadingAnimation(false);
}

void WidgetManager::setLoginFailed()
{
    entryWidget->showLoginWidget(true);
    mainWindow->setLoginFailedState();
    activateLoadingAnimation(false);
}

void WidgetManager::showUpdateInfo()
{
    waitAnimationWidget->hideWithStop();
    updateWidget->showWithFill();

    mainWindow->showMainFrame(false);
    mainWindow->slotShowUpdateInfo();
    entryWidget->hide();
    commonButtonGroupWidget->hide();
}

void WidgetManager::setLoadCompleteState()
{
    commonButtonGroupWidget->loadCompleteState();
    mainWindow->loadCompleteState();
}

void WidgetManager::setCompeteState()
{
    mainWindow->setInlineDebug(tr("Загрузка завершена"));
    waitAnimationWidget->hideWithStop();
}

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

void WidgetManager::setNeedUpdateState(quint64 size, quint64 fileCount,quint64 deleteCount)
{
    QString result;
    if(fileCount > 0)
    {
        result = tr("Доступно обновление: ") + Tools::convertFileSize(size,true);
        //result += tr("Количество файлов: ") + QString::number(fileCount);
    }
    else
    {
        result = tr("Файлов к удалению: ") + QString::number(deleteCount);
    }

    mainWindow->setNeedUpdateState(result);
    commonButtonGroupWidget->needUpdateState(true);
    waitAnimationWidget->hideWithStop();

}

void WidgetManager::setRecoveryState()
{
    mainWindow->setInlineDebug(tr("Восстановление версии..."));
}

void WidgetManager::setLastVersionState()
{
    mainWindow->setLastVersionState();
    commonButtonGroupWidget->lastVerInstalledState();
    waitAnimationWidget->hideWithStop();
}

void WidgetManager::setConnectionState(bool isConnected)
{
    mainWindow->slotConnectionState(isConnected);
    if (isConnected)
    {
        entryWidget->showLoginWidget(true);
    }
    waitAnimationWidget->hideWithStop();
}

void WidgetManager::setSendFileToServerState()
{
    mainWindow->loadToServer();
    commonButtonGroupWidget->showProgressBar(true);
    updateWidget->hide();
}

void WidgetManager::start()
{
    waitAnimationWidget->showWithPlay();
    mainWindow->setStartState();
}

void WidgetManager::setServerDisconnectState()
{
    commonButtonGroupWidget->disconnectState();
    mainWindow->slotServerDisconnect();
    entryWidget->disconnectState();
    waitAnimationWidget->hideWithStop();
}

void WidgetManager::setUndoCurrentChangesState()
{
    mainWindow->undoCurrentChanges();
    updateWidget->hide();
    commonButtonGroupWidget->showProgressBar(false);
    waitAnimationWidget->showWithPlay();
}

void WidgetManager::showMainFrame(bool flag)
{
    mainWindow->showMainFrame(flag);
}

void WidgetManager::setAppAvailable(bool isAvailable)
{
    commonButtonGroupWidget->startButtonActive(isAvailable);
    mainWindow->setStartOfflineButton(isAvailable);
}

void WidgetManager::showSettings(bool isActive)
{
    entryWidget->settingsState();
    entryWidget->isActive(isActive);
}

void WidgetManager::slotInlineDebug(const QString& text)
{
    mainWindow->setInlineDebug(text);
}

void WidgetManager::slotActivateLoadAnimation(bool flag)
{
    waitAnimationWidget->slotActivateLoadingAnimation(flag);
}

NotifyController *WidgetManager::getNotifyController() const
{
    return notifyController;
}

MainWindow *WidgetManager::getMainWindow() const
{
    return mainWindow;
}

UpdateNotifyWidget *WidgetManager::getUpdateWidget() const
{
    return updateWidget;
}

CommonButtonGroupWidget *WidgetManager::getCommonButtonGroupWidget() const
{
    return commonButtonGroupWidget;
}

EntryWidget *WidgetManager::getEntryWidget() const
{
    return entryWidget;
}

WaitAnimationWidget *WidgetManager::getWaitAnimationWidget() const
{
    return waitAnimationWidget;
}

InstructorButtonGroupWidget *WidgetManager::getInstructorButtonGroupWiget() const
{
    return instructorButtonGroupWiget;
}
