#include "mainobject.h"
#include "loginwidget.h"
#include "mainwindow.h"
#include "setupwaitprogressbar.h"
#include "global.h"

#include <QPropertyAnimation>
#include <QGraphicsOpacityEffect>
#include <QFileInfo>

MainObject::MainObject(QObject *parent)
    : QObject(parent)
{
    initUI();
    createConnections();

}

MainObject::~MainObject()
{

}

void MainObject::showLogin()
{
    mLoginWidget->show();

    // 位移动画
//    QPropertyAnimation *animation = new QPropertyAnimation(mLoginWidget, "geometry");
//    animation->setDuration(10000);
//    animation->setStartValue(QRect(0, 0, 100, 30));
//    animation->setEndValue(QRect(250, 250, 100, 30));
//    animation->start();

    //opacity
//    QGraphicsOpacityEffect *pButtonOpacity = new QGraphicsOpacityEffect(this);
//    pButtonOpacity->setOpacity(1);
//    mLoginWidget->setGraphicsEffect(pButtonOpacity);
//    QPropertyAnimation *pOpacityAnimation = new QPropertyAnimation(pButtonOpacity, "opacity");
//    pOpacityAnimation->setDuration(10000);
//    pOpacityAnimation->setStartValue(1);
//    pOpacityAnimation->setEndValue(0);
    //    pOpacityAnimation->start();
}

void MainObject::showMainWindow()
{
    mMainWindow->showMaximized();
    mMainWindow->setCameraInitPanel();
    emit Global::Instance()->refresh();
}

LoginWidget *MainObject::loginWidget()
{
    return mLoginWidget;
}

MainWindow *MainObject::mainWindow()
{
    return mMainWindow;
}

void MainObject::secondary()
{
#ifdef WIN32
 ::SetWindowPos(HWND(mMainWindow->winId()), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
 ::SetWindowPos(HWND(mMainWindow->winId()), HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
#else
    mMainWindow->activateWindow();
    mMainWindow->raise();
#endif
}

void MainObject::initUI()
{
    mMainWindow = new MainWindow;

    mLoginWidget = new LoginWidget;
    mLoginWidget->setBackgroundImage(Global::Instance()->resourceDirPath() + "/image/login/1.png");

    mProgressBar = new SetupWaitProgressBar;
}

void MainObject::createConnections()
{
    connect(mLoginWidget, &LoginWidget::loggedIn, mProgressBar, &SetupWaitProgressBar::show);
    connect(mProgressBar, &SetupWaitProgressBar::completed, mMainWindow, &MainWindow::showMaximized);
}
