/*****************************************************************************
 *
 * Testmanager - Graphical Automation and Visualisation Tool
 *
 * Copyright (C) 2023 Daniel Ramirez <dr@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 "ParameterSetWidgetPlugin.h"

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

#include <QtPdWidgets2/ParameterSetWidget.h>

#include <QtPdCom1/Process.h>
#include <pdcom5/Process.h>

#include <QJsonObject>
#include <QFileDialog>
#include <qabstractitemmodel.h>

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

QString ParameterSetWidgetPlugin::name() const
{
    return tr("Parameter Set Widget");
}

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

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

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

int ParameterSetWidgetPlugin::getSlotCapacity() const
{
    return 0;
}

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

int ParameterSetWidgetPlugin::getFreeSlots(const SlotModel &) const
{
    return 0;
}

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

void ParameterSetWidgetPlugin::openEditor(
        QWidget *widget,
        void *data,
        QWidget *parent)
{
    Q_UNUSED(data);
    Q_UNUSED(parent);

    auto *parametersWidget = dynamic_cast<Pd::ParameterSetWidget *>(widget);
    if (!parametersWidget) {
        return;
    }

    QFileDialog dialog(widget);
    dialog.setAcceptMode(QFileDialog::AcceptOpen);
    dialog.setFileMode(QFileDialog::Directory);
    dialog.setOption(QFileDialog::ShowDirsOnly, true);

    int ret = dialog.exec();  // FIXME replace exec()

    if (ret != QDialog::Accepted) {
        return;
    }

    parametersWidget->setPath(dialog.selectedFiles()[0]);
}

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

void ParameterSetWidgetPlugin::appendVariable(
        [[maybe_unused]] QWidget *widget,
        [[maybe_unused]] void *data,
        [[maybe_unused]] SlotModel *slotModel,
        [[maybe_unused]] DataNode *dataNode) const
{}

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

void ParameterSetWidgetPlugin::connectDataSlots(
        QWidget *widget,
        [[maybe_unused]] void *data,
        [[maybe_unused]] const SlotModel *slotModel,
        const DataModel *dataModel) const
{
    QSet<QtPdCom::Process *> processes;
    int numberRows = dataModel->rowCount(QModelIndex());
    for (int row = 0; row < numberRows; row++) {
        QModelIndex index = dataModel->index(row, 0, QModelIndex());
        QtPdCom::Process &process =
                dataModel->getDataSource(index)->getProcess();
        processes.insert(&process);
    }
    auto parametersWidget(dynamic_cast<Pd::ParameterSetWidget *>(widget));
    parametersWidget->setProcesses(processes);
}

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

void ParameterSetWidgetPlugin::clearVariables(
        [[maybe_unused]] QWidget *widget,
        [[maybe_unused]] void *data) const
{}

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

QWidget *ParameterSetWidgetPlugin::createWidget(QWidget *parent) const
{
    auto widget = new Pd::ParameterSetWidget(parent);
    auto container = qobject_cast<WidgetContainer *>(widget->parent());

    widget->setContextMenuPolicy(Qt::NoContextMenu);
    container->setMinimumSize(331, 30);
    return widget;
}
