/*****************************************************************************
 *
 * 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/>.
 *
 ****************************************************************************/
#ifndef PARAMETER_MODEL_H
#define PARAMETER_MODEL_H

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

#include "Parameter.h"

#include <QAbstractTableModel>
#include <QJsonArray>

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

class MainWindow;

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

class ParameterModel:
    public QAbstractTableModel
{
    Q_OBJECT

    public:
        ParameterModel(MainWindow *);
        ~ParameterModel();

        MainWindow *getMainWindow() const { return mainWindow; }
        
        void clear();
        bool isEmpty() const { return parameters.isEmpty(); }

        void fromJson(const QJsonArray &);
        QJsonArray toJson(bool) const;

        int rowCount(const QModelIndex &) const;
        int columnCount(const QModelIndex &) const;
        QVariant data(const QModelIndex &, int) const;
        QVariant headerData(int, Qt::Orientation, int) const;
        Qt::ItemFlags flags(const QModelIndex &) const;
        bool setData(const QModelIndex &, const QVariant &, int);

        QStringList mimeTypes() const;
        bool dropMimeData(const QMimeData *, Qt::DropAction, int, int,
                const QModelIndex &);
        bool canDropMimeData(const QMimeData *, Qt::DropAction, int, int,
                const QModelIndex &) const;

        void connectParameters();
        void saveParameters();
        void replaceUrl(const QUrl &, const QUrl &);
        QSet<QUrl> getUrls() const;

        void notify(Parameter *);

        Parameter *getParameter(const QModelIndex &) const;

        QString getPath() const { return parameterPath; }

        void remove(Parameter *);

    private:
        QList<Parameter *> parameters;
        MainWindow *mainWindow;
        DataNode *dataNode;
        QString parameterPath;

        void saveDialog();

        ParameterModel();
};

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

#endif
