#include "camerabox.h"
#include "global.h"

#include <QMenu>
#include <QAction>
#include <QGridLayout>
#include <QDebug>

CameraBox::CameraBox(QObject *parent)
    : QObject(parent)
{
    gridLayout = nullptr;
    mCameraCount = 16;
    mCameraType = "1_16";

    menuFlag = "排列";
    actionFlag = "监控";

    // 通过这里设置好数据下面只需要循环添加和判断就行
    // 灵活性大大增强,只需要这里改动下就行
//    types.insert(4, QStringList() << "1_4"
//                                  << "5_8"
//                                  << "9_12"
//                                  << "13_16"
//                                  << "17_20"
//                                  << "21_24"
//                                  << "25_28"
//                                  << "29_32"
//                                  << "33_36");
//    types.insert(6, QStringList() << "1_6"
//                                  << "7_12"
//                                  << "13_18"
//                                  << "19_24"
//                                  << "25_30"
//                                  << "31_36");
//    types.insert(8, QStringList() << "1_8"
//                                  << "9_16"
//                                  << "17_24"
//                                  << "25_32"
//                                  << "33_40"
//                                  << "41_48"
//                                  << "49_57"
//                                  << "57_64");
//    types.insert(9, QStringList() << "1_9"
//                                  << "9_17"
//                                  << "18_26"
//                                  << "27_35"
//                                  << "36_42"
//                                  << "43_50"
//                                  << "51_59");
//    types.insert(13, QStringList() << "1_13"
//                                   << "14_26"
//                                   << "27_39"
//                                   << "40_52"
//                                   << "52_64");
//    types.insert(16, QStringList() << "1_16"
//                                   << "17_32"
//                                   << "33_48"
//                                   << "49_64");
//    types.insert(25, QStringList() << "1_25");
//    types.insert(36, QStringList() << "1_36");
//    types.insert(64, QStringList() << "1_64");
    types.insert(2, QStringList() << "1_2"
                                  << "3_4"
                                  << "5_6"
                                  << "7_8"
                                  << "9_10"
                                  << "11_12"
                                  << "13_14"
                                  << "15_16");
    types.insert(4, QStringList() << "1_4"
                                  << "5_8"
                                  << "9_12"
                                  << "13_16");
    types.insert(6, QStringList() << "1_6"
                                  << "7_12"
                                  << "11_16");
    types.insert(8, QStringList() << "1_8"
                                  << "9_16");
    types.insert(9, QStringList() << "1_9"
                                  << "8_16");
    types.insert(13, QStringList() << "1_13"
                                   << "4_16");
    types.insert(16, QStringList() << "1_16");
}

void CameraBox::addMenu(QMenu *menu, int type)
{
    // 父菜单文字
    QString name = QString("切换到%1%2").arg(type).arg(menuFlag);
    // 链表中取出该布局大类下的小类集合
    QStringList flags = types.value(type);

    // 超过一个子元素则添加子菜单
    QMenu* menuSub;
    if (flags.count() > 1)
    {
        menuSub = menu->addMenu(name);
    }
    else
    {
        menuSub = menu;
    }

    foreach (QString flag, flags)
    {
        QStringList list = flag.split("_");
        QString start = list.at(0);
        QString end = list.at(1);

        // 对应菜单文本
        QString text = QString("%1%2-%1%3").arg(actionFlag).arg(start).arg(end);
        if (flags.count() == 1)
        {
            text = name;
        }

        // 添加菜单动作
        QAction *action = menuSub->addAction(text, this, SLOT(showCamera()));
        // 设置弱属性传入大类和子类布局标识等
        action->setProperty("index", start);
        action->setProperty("type", type);
        action->setProperty("flag", flag);
    }
}

void CameraBox::setCameraType(const QString &cameraType)
{
    this->mCameraType = cameraType;
}

void CameraBox::setLayout(QGridLayout *gridLayout)
{
    this->gridLayout = gridLayout;
    this->gridLayout->setContentsMargins(0, 0, 0, 0);
    this->gridLayout->setSpacing(1);
    this->gridLayout->setMargin(0);
}

void CameraBox::setWidgets(QWidgetList widgets)
{
    this->widgets = widgets;
    this->mCameraCount = widgets.count();
}

void CameraBox::setMenuFlag(const QString &menuFlag)
{
    this->menuFlag = menuFlag;
}

void CameraBox::setActionFlag(const QString &actionFlag)
{
    this->actionFlag = actionFlag;
}

void CameraBox::setTypes(const QMap<int, QStringList> &types)
{
    this->types = types;
}

void CameraBox::initMenu(QMenu *menu, const QList<bool> &enable)
{
    // 通过菜单是否可见设置每个菜单可见与否
    if (enable.count() < 7)
    {
        return;
    }

    if (enable.at(0))
    {
        addMenu(menu, 2);
    }
    if (enable.at(1))
    {
        addMenu(menu, 4);
    }
    if (enable.at(2))
    {
        addMenu(menu, 6);
    }
    if (enable.at(3))
    {
        addMenu(menu, 8);
    }
    if (enable.at(4))
    {
        addMenu(menu, 9);
    }
    if (enable.at(5))
    {
        addMenu(menu, 13);
    }
    if (enable.at(6))
    {
        addMenu(menu, 16);
    }

}

void CameraBox::showCamera(int type, int index)
{
    // 根据不同的父菜单类型执行对应的函数
    if (type == 1)
    {
        changeCamera1(index);
    }
    else if (type == 2)
    {
        changeCamera2(index);
    }
    else if (type == 4)
    {
        changeCamera4(index);
    }
    else if (type == 6)
    {
        changeCamera6(index);
    }
    else if (type == 8)
    {
        changeCamera8(index);
    }
    else if (type == 9)
    {
        changeCamera9(index);
    }
    else if (type == 13)
    {
        changeCamera13(index);
    }
    else if (type == 16)
    {
        changeCamera16(index);
    }

    CameraDisplayParam p;
    p.type = type;
    p.index = index;
    Global::Instance()->setCameraDisplayParam(p);
    emit changeCamera(type, mCameraType, false);
}

void CameraBox::showCamera()
{
    // 识别具体是哪个动作菜单触发的
    QAction *action = (QAction *)sender();
    // 从弱属性取出值
    int index = action->property("index").toInt() - 1;
    int type = action->property("type").toInt();
    QString cameraType = action->property("flag").toString();

    // 只有当画面布局类型改变了才需要切换
    if (this->mCameraType != cameraType)
    {
        this->mCameraType = cameraType;
        showCamera(type, index);
    }
}

void CameraBox::showAllCamera()
{
    // 一般是从配置文件读取到了最后的通道画面类型进行设置
    int type = 1;
    if (mCameraType.startsWith("0_"))
    {
        int index = mCameraType.split("_").last().toInt() - 1;
        changeCamera1(index);
        emit changeCamera(type, mCameraType, true);
    }
    else
    {
        int index = mCameraType.split("_").first().toInt() - 1;
        QMap<int, QStringList>::iterator iter = types.begin();
        while (iter != types.end())
        {
            QStringList flags = iter.value();
            if (flags.contains(mCameraType))
            {
                type = iter.key();
                showCamera(type, index);
                break;
            }
            iter++;
        }
    }
}

void CameraBox::hideAllCamera()
{
    for (int i = 0; i < mCameraCount; ++i)
    {
        gridLayout->removeWidget(widgets.at(i));
        widgets.at(i)->setVisible(false);
    }
}

void CameraBox::changeCameranormal(int index, int flag)
{
    // 首先隐藏所有通道
    hideAllCamera();
    int count = 0;
    int row = 0;
    int column = 0;

    // 行列数一致的比如 2*2 3*4 4*4 5*5 等可以直接套用通用的公式
    // 按照这个函数还可以非常容易的拓展出 10*10 16*16=256 通道界面
    for (int i = 0; i < mCameraCount; ++i)
    {
        if (i >= index)
        {
            // 添加到对应布局并设置可见
            gridLayout->addWidget(widgets.at(i), row, column);
            widgets.at(i)->setVisible(true);

            count++;
            column++;
            if (column == flag)
            {
                row++;
                column = 0;
            }
        }

        if (count == (flag * flag))
        {
            break;
        }
    }
}

void CameraBox::changeCameracustom(int index, int type)
{
    // 从开始索引开始往后衍生多少个通道
    QList<int> indexs;
    for (int i = index; i < (index + type); ++i)
    {
        indexs << i;
    }

    if (type == 6)
    {
        changeCamera6(indexs);
    }
    else if (type == 8)
    {
        changeCamera8(indexs);
    }
    else if (type == 13)
    {
        changeCamera13(indexs);
    }
}

void CameraBox::changeCamera6(const QList<int> &indexs)
{
    // 过滤防止索引越界
    if (indexs.count() < 6)
    {
        return;
    }

    // 首先隐藏所有通道
    hideAllCamera();
    // 挨个重新添加到布局
    gridLayout->addWidget(widgets.at(indexs.at(0)), 0, 0, 2, 2);
    gridLayout->addWidget(widgets.at(indexs.at(1)), 0, 2, 1, 1);
    gridLayout->addWidget(widgets.at(indexs.at(2)), 1, 2, 1, 1);
    gridLayout->addWidget(widgets.at(indexs.at(3)), 2, 2, 1, 1);
    gridLayout->addWidget(widgets.at(indexs.at(4)), 2, 1, 1, 1);
    gridLayout->addWidget(widgets.at(indexs.at(5)), 2, 0, 1, 1);
    // 设置通道控件可见
    for (int i = indexs.first(); i <= indexs.last(); ++i)
    {
        widgets.at(i)->setVisible(true);
    }
}

void CameraBox::changeCamera8(const QList<int> &indexs)
{
    // 过滤防止索引越界
    if (indexs.count() < 8)
    {
        return;
    }

    // 首先隐藏所有通道
    hideAllCamera();
    // 挨个重新添加到布局
    gridLayout->addWidget(widgets.at(indexs.at(0)), 0, 0, 3, 3);
    gridLayout->addWidget(widgets.at(indexs.at(1)), 0, 3, 1, 1);
    gridLayout->addWidget(widgets.at(indexs.at(2)), 1, 3, 1, 1);
    gridLayout->addWidget(widgets.at(indexs.at(3)), 2, 3, 1, 1);
    gridLayout->addWidget(widgets.at(indexs.at(4)), 3, 3, 1, 1);
    gridLayout->addWidget(widgets.at(indexs.at(5)), 3, 2, 1, 1);
    gridLayout->addWidget(widgets.at(indexs.at(6)), 3, 1, 1, 1);
    gridLayout->addWidget(widgets.at(indexs.at(7)), 3, 0, 1, 1);
    // 设置通道控件可见
    for (int i = indexs.first(); i <= indexs.last(); ++i)
    {
        widgets.at(i)->setVisible(true);
    }
}

void CameraBox::changeCamera13(const QList<int> &indexs)
{
    // 过滤防止索引越界
    if (indexs.count() < 13)
    {
        return;
    }

    // 首先隐藏所有通道
    hideAllCamera();
    // 挨个重新添加到布局
    gridLayout->addWidget(widgets.at(indexs.at(0)), 0, 0, 1, 1);
    gridLayout->addWidget(widgets.at(indexs.at(1)), 0, 1, 1, 1);
    gridLayout->addWidget(widgets.at(indexs.at(2)), 0, 2, 1, 1);
    gridLayout->addWidget(widgets.at(indexs.at(3)), 0, 3, 1, 1);
    gridLayout->addWidget(widgets.at(indexs.at(4)), 1, 0, 1, 1);
    gridLayout->addWidget(widgets.at(indexs.at(5)), 2, 0, 1, 1);
    gridLayout->addWidget(widgets.at(indexs.at(6)), 1, 1, 2, 2);
    gridLayout->addWidget(widgets.at(indexs.at(7)), 1, 3, 1, 1);
    gridLayout->addWidget(widgets.at(indexs.at(8)), 2, 3, 1, 1);
    gridLayout->addWidget(widgets.at(indexs.at(9)), 3, 0, 1, 1);
    gridLayout->addWidget(widgets.at(indexs.at(10)), 3, 1, 1, 1);
    gridLayout->addWidget(widgets.at(indexs.at(11)), 3, 2, 1, 1);
    gridLayout->addWidget(widgets.at(indexs.at(12)), 3, 3, 1, 1);
    // 设置通道控件可见
    for (int i = indexs.first(); i <= indexs.last(); ++i)
    {
        widgets.at(i)->setVisible(true);
    }
}

void CameraBox::changeCamera1(int index)
{
    // 首先隐藏所有通道
    hideAllCamera();
    // 添加通道到布局
    gridLayout->addWidget(widgets.at(index), 0, 0);
    // 设置可见
    widgets.at(index)->setVisible(true);
}

void CameraBox::changeCamera2(int index)
{
    hideAllCamera();
    gridLayout->addWidget(widgets.at(index), 0, 0);
    gridLayout->addWidget(widgets.at(index + 1), 0, 1);
    // 设置可见
    widgets.at(index)->setVisible(true);
    widgets.at(index + 1)->setVisible(true);
}

void CameraBox::changeCamera4(int index)
{
    changeCameranormal(index, 2);
}

void CameraBox::changeCamera6(int index)
{
    changeCameracustom(index, 6);
}

void CameraBox::changeCamera8(int index)
{
    changeCameracustom(index, 8);
}

void CameraBox::changeCamera9(int index)
{
    changeCameranormal(index, 3);
}

void CameraBox::changeCamera13(int index)
{
    changeCameracustom(index, 13);
}

void CameraBox::changeCamera16(int index)
{
    changeCameranormal(index, 4);
}

