#include <QJsonArray>
#include <QJsonParseError>
#include <QJsonObject>
#include <QMenu>
#include "rktwebview_qt.h"
#include "utils.h"

static QAction *addMenuItemFromJson(Rktwebview_qt *owner,
                                    QMenu *menu,
                                    const QJsonObject &item,
                                    int source_handle);

static QMenu *buildMenuObjectFromJson(Rktwebview_qt *owner,
                                      const QJsonArray &items,
                                      int source_handle,
                                      QWidget *parent = nullptr)
{
    QMenu *menu = new QMenu(parent);

    for (const QJsonValue &v : items) {
        if (!v.isObject()) {
            continue;
        }

        QJsonObject item = v.toObject();

        if (item.value("separator").toBool(false)) {
            menu->addSeparator();
            continue;
        }

        addMenuItemFromJson(owner, menu, item, source_handle);
    }

    return menu;
}

static QAction *addMenuItemFromJson(Rktwebview_qt *owner,
                                    QMenu *menu,
                                    const QJsonObject &item,
                                    int source_handle)
{
    QString id = item.value("id").toString();
    QString name = item.value("name").toString();
    QString icon_file = item.value("icon").toString();

    QAction *action = nullptr;

    QJsonValue submenu_value = item.value("submenu");
    if (submenu_value.isObject()) {
        QJsonObject submenu_obj = submenu_value.toObject();
        QJsonArray submenu_items = submenu_obj.value("menu").toArray();

        QMenu *submenu = buildMenuObjectFromJson(owner,
                                                 submenu_items,
                                                 source_handle,
                                                 menu);

        if (!icon_file.isEmpty()) {
            submenu->setIcon(QIcon(icon_file));
        }

        submenu->setTitle(name);
        action = menu->addMenu(submenu);
    } else {
        if (icon_file.isEmpty()) {
            action = menu->addAction(name);
        } else {
            action = menu->addAction(QIcon(icon_file), name);
        }

        QObject::connect(action, &QAction::triggered,
                         owner, [owner, source_handle, id]() {
                             EventContainer e("tray-menu-item-chosen");
                             e["id"] = id;
                             e["menu_item"] = id;
                             owner->triggerEvent(source_handle, e);
                         });
    }

    action->setData(id);
    return action;
}

QMenu *buildMenuFromJson(Rktwebview_qt *self, const QString &menu_json, int source_handle)
{
    QJsonParseError err;
    QJsonDocument doc = QJsonDocument::fromJson(menu_json.toUtf8(), &err);

    if (err.error != QJsonParseError::NoError || !doc.isObject()) {
        ERROR2("Json parse error for menu: %d, '%s'\n", err.error, err.errorString().toUtf8().constData());
        return nullptr;
    }

    QJsonObject root = doc.object();
    QJsonArray items = root.value("menu").toArray();

    return buildMenuObjectFromJson(self, items, source_handle);
}
