#include "camerapanel.h"
#include "camerabox.h"
#include "cameradisplay.h"
#include "global.h"

#include <QApplication>
#include <QGridLayout>
#include <QEvent>
#include <QLabel>
#include <QMenu>
#include <QCursor>
#include <QList>
#include <QDebug>

CameraPanel::CameraPanel(QWidget *parent)
    : QWidget(parent)
{
    this->initUI();
}

CameraPanel::~CameraPanel()
{
}

bool CameraPanel::eventFilter(QObject *watched, QEvent *event)
{
    if (event->type() == QEvent::MouseButtonDblClick)
    {
        // 双击最大化再次双击还原
        QLabel *widget = (QLabel *)watched;
        if (!max)
        {
            max = true;
            box->hideAllCamera();
            mGLayout->addWidget(widget, 0, 0);
            widget->setVisible(true);
        }
        else
        {
            max = false;
            box->showAllCamera();
        }

        return true;
    }
    else if (event->type() == QEvent::MouseButtonPress)
    {
        // 鼠标右键的地方弹出菜单
        if (qApp->mouseButtons() == Qt::RightButton)
        {
            menu->exec(QCursor::pos());
        }
    }

    return QWidget::eventFilter(watched, event);
}

void CameraPanel::initUI()
{
    mMainVLayout = new QVBoxLayout;
    mMainVLayout->setContentsMargins(1, 1, 0, 1);
    mGLayout = new QGridLayout;
    mLabel = new QLabel;
    mLabel->setFixedHeight(30);
    mMainVLayout->addLayout(mGLayout);
//    mMainVLayout->addWidget(mLabel);
    this->setLayout(mMainVLayout);

    max = false;
    // 安装事件过滤器
    this->installEventFilter(this);

    // 实例化子对象
    QWidgetList widgets;
    for (int i = 0; i < 16; ++i)
    {
        CameraDisplay* cd = new CameraDisplay;
        cd->setObjectName("CameraDisplay");
        cd->installEventFilter(this);
        int num = i + 1;
        QString  numStr;
        if(num < 10)
        {
            numStr = "0" + QString::number(num);
        }
        else
        {
            numStr = QString::number(num);
        }
        cd->setIndex(QString("C%1").arg(numStr));
        cd->setTitle("Unknow");
        cd->setState(CameraDisplay::UNKNOW);
        cd->setContent(QString("通道 %1").arg(i + 1));
        widgets << cd;
    }

    // 实例化盒子
    box = new CameraBox(this);
    // 关联信号槽
    connect(box, SIGNAL(changeCamera(int, QString, bool)), this, SLOT(changeCamera(int, QString, bool)));
    // 可以改成 1_4 5_8 1_36 等
    box->setCameraType("1_16");
    box->setLayout(mGLayout);
    box->setWidgets(widgets);
    box->setMenuFlag("画面");
    box->setActionFlag("摄像头");
    // box->showAllCamera();
    CameraDisplayParam p = Global::Instance()->cameraDisplayParam();
    box->showCamera(p.type, p.index);

    // 实例化菜单
    menu = new QMenu(this);
    // 先安排自己的菜单
    // 这里关联到一个槽函数处理,也可以关联到不同的槽函数
    // menu->addAction("查看属性", this, SLOT(doAction()));
    // menu->addAction("启动轮询视频", this, SLOT(doAction()));
    // menu->addSeparator();

    // 把菜单加到盒子上,并控制布局切换菜单是否可用(默认9个布局切换菜单)
    QList<bool> enable;
    enable << true << true << true << true << true << true << true;
    box->initMenu(menu, enable);

}

void CameraPanel::doAction()
{
    // 判断是哪个动作触发的
    QAction *action = (QAction *)sender();
    // mLabel->setText(QString("触发了菜单： %1").arg(action->text()));
}

void CameraPanel::changeCamera(int type, const QString &videoType, bool videoMax)
{
    QString info = QString("主菜单：%1  子菜单：%2").arg(type).arg(videoType);
    mLabel->setText(info);
}
