#include "dvieMainWindow.h"
#include "./ui_dvieMainWindow.h"
#include "transparentMask.h"
#include "dashboardFrame.h"
#include "dvieSecondaryWindow.h"
#include <QDateTime>

DvieMainWindow::DvieMainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::dvieMainWindow)
    , m_pTransparentMask(nullptr)
    , m_pDashboardFrame(nullptr)
{
    ui->setupUi(this);
    setProperty("windowType", "main");

    m_pDashboardFrame = new DashboardFrame("mainDVIE_dashboardFrame", dashboardFrame::ft_main, this);
    ui->centralLayout->addWidget(m_pDashboardFrame);
}

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

bool DvieMainWindow::event(QEvent* event)
{
    if(event->type() == QEvent::Resize)
    {
        if(m_pTransparentMask  && m_pTransparentMask->isVisible())
            m_pTransparentMask->setGeometry(ui->topWidget->geometry());
    }

    return QMainWindow::event(event);
}

/*void DvieMainWindow::resizeEvent(QResizeEvent* event)
{
    if(m_pTransparentMask  && m_pTransparentMask->isVisible())
        m_pTransparentMask->setGeometry(ui->topWidget->geometry());

    QMainWindow::resizeEvent(event);
}*/

void DvieMainWindow::showTransparentMask()
{
    if(m_pTransparentMask == nullptr)
        m_pTransparentMask = new TransparentMask(this);

    m_pTransparentMask->setGeometry(ui->topWidget->geometry());
    m_pTransparentMask->show();
}
void DvieMainWindow::hideTransparentMask()
{
    if(m_pTransparentMask  && m_pTransparentMask->isVisible())
        m_pTransparentMask->hide();
}

DashboardFrame* DvieMainWindow::dashboardFrame()
{
    return m_pDashboardFrame;
}
DashboardFrame* DvieMainWindow::getDashboardFrame(const QString& strName)
{
    DashboardFrame* frame = nullptr;
    if(m_pDashboardFrame->objectName() == strName)
        frame = m_pDashboardFrame;
    else
    {
        int nIndex = strName.indexOf("_dashboardFrame");
        if(nIndex != -1)
        {
            QString strDVIEWindowName = strName.left(nIndex);
            //qDebug() << strDVIEWindowName;
            auto itr = m_hashSecondaryWindow.find(strDVIEWindowName);
            if(itr != m_hashSecondaryWindow.end())
                frame = itr.value()->dashboardFrame();
        }
    }

    return frame;
}

void DvieMainWindow::creatSecondaryWindowAndAddDashboard(QPoint pos, Dashboard* dashboard)
{
    QString strName = "secondaryDVIE_" + QDateTime::currentDateTime().toString("yyMMdd-HHmmss");
    DvieSecondaryWindow* secondartWindow = new DvieSecondaryWindow(strName, this);
    secondartWindow->setMainWindow(this);
    secondartWindow->move(pos);
    secondartWindow->show();
    if(secondartWindow->dashboardFrame())
        secondartWindow->dashboardFrame()->addDashboard(dashboard);
    m_hashSecondaryWindow.insert(strName, secondartWindow);
    //qDebug() << m_hashSecondaryWindow.count();
}
void DvieMainWindow::removeSecondartWindow(QString& strName)
{
    DvieSecondaryWindow* secondartWindow = m_hashSecondaryWindow.take(strName);
    if(secondartWindow)
        secondartWindow->deleteLater(); //不要直接delete，因为后续还需要该对象做一些其它工作
    //qDebug() << m_hashSecondaryWindow.count();
}
