#include "dashboard.h"
#include "dashboardFrame.h"
#include "dataPanel.h"
#include "customTab.h"

#include <QDialog>
#include <QPushButton>
#include <QMenu>
#include <QVBoxLayout>
#include <QKeyEvent>
#include <QDateTime>

#define tabButtonHeight 41
Dashboard::Dashboard(const QString& strName, QObject *parent)
    : QObject(parent)
{
    m_pFrame = nullptr;
    //displayArea
    m_pDisplayArea = new QWidget();
    //m_pDisplayArea->setObjectName("displayArea");
    //m_pDisplayArea->setStyleSheet("QWidget #displayArea {background-color: rgb(18, 25, 30);}");
    m_pDisplayAreaLayout = nullptr;
    //tab,自适应text内容
    m_pTab = new CustomTab();
    m_pTab->setDashboard(this);
    //m_pTabButton->setIcon(QIcon(":/images/btn_float_default.png"));
    connect(m_pTab, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(contextMenu_tab(const QPoint&)));
    //右键菜单
    m_pTabMenu = new QMenu();
    m_pTabMenu->setStyleSheet("QMenu{\n"
                                 "   background-color:rgb(36,43,50);\n"
                                 "   border:1px solid rgb(6, 6, 6);\n"
                                 "}\n"
                                 "QMenu:item{\n"
                                 "   padding-left:20px;\n"
                                 "   padding-right:20px;\n"
                                 "   font:9pt \"微软雅黑\";\n"
                                 "   color:rgb(220,220,220);\n"
                                 "   height:26px;\n"
                                 "}\n"
                                 "QMenu:item:selected{\n"
                                 "   background-color: rgba(67,160,249, 80);\n"
                                 "}\n");
    m_pTabMenu->addAction(QString::fromWCharArray(L"删除"), this, SLOT(onAction_remove()));
    m_pTabMenu->addAction(QString::fromWCharArray(L"重命名"), this, SLOT(onAction_rename()));

    m_nPanenlNameNumber = 0;

    setName(strName);
}

Dashboard::~Dashboard()
{
    //m_pDisplayArea和m_pTab不需要手动析构，因为QObject建立起的父子结构会自动析构，自动析构时不会将其置为nullptr，所以if判断无效，会引起'SIGSEGV'（存储器段错误）报错
    if(m_pTabMenu)
    {
        delete m_pTabMenu;
        m_pTabMenu = nullptr;
    }
}

void Dashboard::deleteSubWidgets()
{
    if(m_pTab)
    {
        delete m_pTab;
        m_pTab = nullptr;
    }

    if(m_pDisplayArea)
    {
        delete m_pDisplayArea;
        m_pDisplayArea = nullptr;
    }
}

const QString& Dashboard::name()
{
    return m_strName;
}
void Dashboard::setName(const QString& strName)
{
    m_strName = strName;
    m_pTab->setText(strName);
}

void Dashboard::setFrame(DashboardFrame* frame)
{
    m_pFrame = frame;
    setParent(m_pFrame);
}
DashboardFrame* Dashboard::frame()
{
    return m_pFrame;
}

void Dashboard::setDisplayAreaLayout(QHBoxLayout* pLayout)
{
    m_pDisplayAreaLayout = pLayout;
}

CustomTab* Dashboard::tab()
{
    return m_pTab;
}

QWidget* Dashboard::displayArea()
{
    return m_pDisplayArea;
}

void Dashboard::setActive(bool bActive)
{
    if(bActive && m_pDisplayAreaLayout)
    {
        m_pDisplayAreaLayout->addWidget(m_pDisplayArea);
        m_pDisplayArea->show();
        for(int n = 0; n < m_dataPanels.count(); n++)
        {
            //让所有dataPanel根据当前area的size进行同步缩放
            m_dataPanels.at(n)->resizeByNewSize(m_pDisplayArea->size());
        }
    }
    else if(!bActive && m_pDisplayAreaLayout)
    {
        m_pDisplayAreaLayout->removeWidget(m_pDisplayArea);
        m_pDisplayArea->hide();
        for(int n = 0; n < m_dataPanels.count(); n++)
        {
            //让所有dataPanel记录area当前的size，当再次被激活时若size发生变化，让所有dataPanel进行同步缩放
            m_dataPanels.at(n)->setDisplayAreaSize(m_pDisplayArea->size());
        }
    }

    m_pTab->setActive(bActive);
}

void Dashboard::addPanel(DataPanelType type)
{
    DataPanel* panel = new DataPanel(m_pDisplayArea, type);
    connect(panel, SIGNAL(sgl_remove(const QString&)), this, SLOT(onSignal_removePanel(const QString&)));
    connect(panel, SIGNAL(sgl_openCofigurationDialog(DataPanel*)), this, SLOT(onSignal_openConfigurationDlg(DataPanel*)));
    QString strDefaultName = "dataPanel-" + QString::number(m_nPanenlNameNumber);
    m_nPanenlNameNumber++;
    panel->setName(strDefaultName);
    int nPanelWidth = m_pDisplayArea->width() * 0.3;
    int nPanelHeight = nPanelWidth * 0.56; //16:9宽高比
    panel->setInitialSize(QSize(nPanelWidth, nPanelHeight));
    int nCount = m_dataPanels.count();
    //QPoint originPoint = m_pDisplayArea->mapToGlobal(QPoint(0, 0));
    int nX = /*originPoint.x() +*/ (m_pDisplayArea->width() - nPanelWidth) * 0.5 + nCount * 10;
    int nY = /*originPoint.y() +*/ (m_pDisplayArea->height() - nPanelHeight) * 0.5 + nCount * 10;
    panel->move(nX, nY);
    panel->show();
    m_dataPanels.push_back(panel);
}

void Dashboard::removePanel(const QString& strName)
{
    int nIndex = -1;
    for(int n = 0; n < m_dataPanels.count(); n++)
    {
        if(m_dataPanels.at(n)->getName() == strName)
        {
            nIndex = n;
            break;
        }
    }

    if(nIndex != -1)
    {
        DataPanel* panel = m_dataPanels.takeAt(nIndex);
        delete panel;
    }
}

void Dashboard::resizePanel(double ratioX, double ratioY)
{
    for(int n = 0; n < m_dataPanels.count(); n++)
        m_dataPanels.at(n)->resizeByRatio(ratioX, ratioY);
}

void Dashboard::setDateTime(const QDateTime& dateTime)
{
    for(int n = 0; n < m_dataPanels.count(); n++)
        m_dataPanels.at(n)->setDateTime(dateTime);
}

void Dashboard::setTimeRange(TimeUnit unit)
{
    for(int n = 0; n < m_dataPanels.count(); n++)
        m_dataPanels.at(n)->setTimeRange(unit);
}

void Dashboard::contextMenu_tab(const QPoint& pos)
{
    QPoint originPoint = m_pTab->mapToGlobal(QPoint(0, 0));
    m_pTabMenu->popup(originPoint + pos);
}

void Dashboard::onAction_remove()
{
    emit sgl_remove();
}
void Dashboard::onAction_rename()
{
    emit sgl_rename();
}

void Dashboard::onSignal_removePanel(const QString& strName)
{
    removePanel(strName);
}

void Dashboard::onSignal_openConfigurationDlg(DataPanel* pPanel)
{
    if(m_pFrame)
        m_pFrame->openPanelConfigurationDialog(pPanel);
}
