/*****************************************************************************
 *
 * 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 "ParameterDialog.h"

#include "SlotModel.h"
#include "DataSlot.h"
#include "Plugin.h"
#include "MainWindow.h"
#include "ParameterTableModel.h"

#include <QMenu>
#include <QDebug>

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

ParameterDialog::ParameterDialog(
        MainWindow *mainWindow,
        QWidget *parent
        ):
    QDialog(parent),
    mainWindow(mainWindow),
    model(new ParameterTableModel(mainWindow))
{
    setupUi(this);

    tableViewParameters->setContextMenuPolicy(Qt::CustomContextMenu);
    connect(tableViewParameters,
            SIGNAL(customContextMenuRequested(const QPoint &)),
            this, SLOT(tableViewParametersCustomContextMenu(const QPoint &)));
    connect(saveButton, SIGNAL(clicked()),
            this, SLOT(saveParameters()));
    connect(saveAsButton, SIGNAL(clicked()),
            this, SLOT(saveParametersAs()));
    connect(applyButton, SIGNAL(clicked()),
            this, SLOT(applyParameters()));

    tableViewParameters->setModel(
        dynamic_cast<QAbstractTableModel*> (model));

    model->loadParameters();
    model->connectParameters();

    tableViewParameters->resizeColumnsToContents();
}

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

ParameterDialog::~ParameterDialog()
{
}

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

void ParameterDialog::saveParameters()
{
    model->saveParameters();
}

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

void ParameterDialog::saveParametersAs()
{
    model->saveParametersAs();
}

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

void ParameterDialog::applyParameters()
{
    model->applyParameters();
    accept();
}

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

void ParameterDialog::tableViewParametersCustomContextMenu(
        const QPoint &point)
{
    QMenu *menu = new QMenu;
    QModelIndex index(tableViewParameters->indexAt(point));
    parameter = model->getParameter(index);

    QAction *action;
    action = menu->addAction(tr("Remove parameter"),
            this, SLOT(removeParameter()));
    action->setEnabled(parameter);
    action->setIcon(QIcon(":/images/list-remove.svg"));

    menu->addSeparator(); // -------------------------------

    action = menu->addAction(tr("Clear parameter list"),
            this, SLOT(clearParameters()));
    action->setEnabled(not model->isEmpty());
    action->setIcon(QIcon(":/images/edit-clear.svg"));

    menu->exec(tableViewParameters->viewport()->mapToGlobal(point));
}

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

void ParameterDialog::removeParameter()
{
    const QModelIndexList indexes =
        tableViewParameters->selectionModel()->selectedIndexes();
    QList<Parameter *> parameters;

    for (const QModelIndex &index : indexes) {
        if (not parameters.contains(model->getParameter(index))) {
            parameter = model->getParameter(index);
            parameters.append(parameter);
        }
    }
    foreach (Parameter *parameter, parameters) {
        model->remove(parameter);
    }
}

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

void ParameterDialog::clearParameters()
{
    model->clear();
}

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