/*****************************************************************************
 *
 * Testmanager - Graphical Automation and Visualisation Tool
 *
 * Copyright (C) 2018  Florian Pose <fp@igh.de>
 *
 * This file is part of Testmanager.
 *
 * Testmanager is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * Testmanager is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with Testmanager. If not, see <http://www.gnu.org/licenses/>.
 *
 ****************************************************************************/

#include "GraphPlugin.h"

#include "DataSlot.h"
#include "DataModel.h"
#include "DataNode.h"
#include "SlotModel.h"

#include <QtPdWidgets2/Graph.h>

#include <QDebug>
#include <QJsonObject>

/****************************************************************************/

QString GraphPlugin::name() const
{
    return tr("Graph");
}

/****************************************************************************/

QIcon GraphPlugin::icon() const
{
    return QIcon(":/images/plugin-graph.svg");
}

/****************************************************************************/

int GraphPlugin::getSlotCapacity() const
{
    return -1;  // unlimited
}

/****************************************************************************/

int GraphPlugin::getFreeSlots(const SlotModel &) const
{
    return -1;  // unlimited
}

/****************************************************************************/

void GraphPlugin::appendVariable(
        QWidget *widget,
        void *,
        SlotModel *slotModel,
        DataNode *dataNode) const
{
    auto graph(dynamic_cast<Pd::Graph *>(widget));

    DataSlot *slot = new DataSlot(slotModel);
    slot->url = dataNode->nodeUrl();
    slot->period = std::chrono::milliseconds(100);
    slot->color = slotModel->nextColor();

    auto pv(dataNode->getVariable());
    if (!pv.empty()) {
        graph->addVariable(
                pv,
                {},
                slot->period,
                slot->scale,
                slot->offset,
                slot->tau,
                slot->color);
        dataNode->notifySubscribed(slot->period);
    }
}

/****************************************************************************/

void GraphPlugin::connectDataSlots(
        QWidget *widget,
        void *,
        const SlotModel *slotModel,
        const DataModel *dataModel) const
{
    auto graph(dynamic_cast<Pd::Graph *>(widget));

    for (int i = 0; i < slotModel->getChildCount(); i++) {
        DataSlot *slot(dynamic_cast<DataSlot *>(slotModel->getChildNode(i)));
        if (!slot) {
            continue;
        }

        DataNode *dataNode(dataModel->findDataNode(slot->url));
        if (dataNode) {
            auto pv(dataNode->getVariable());
            if (!pv.empty()) {
                graph->addVariable(
                        pv,
                        {},
                        slot->period,
                        slot->scale,
                        slot->offset,
                        slot->tau,
                        slot->color);
                dataNode->notifySubscribed(slot->period);
            }
        }
        else {
            qWarning() << tr("Url not found: %1").arg(slot->url.toString());
        }
    }
}

/****************************************************************************/

void GraphPlugin::clearVariables(QWidget *widget, void *) const
{
    auto graph(dynamic_cast<Pd::Graph *>(widget));
    graph->clearVariables();
}

/****************************************************************************/

QWidget *GraphPlugin::createWidget(QWidget *parent) const
{
    return new Pd::Graph(parent);
}

/****************************************************************************/
