/*****************************************************************************
 *
 * 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_H
#define PARAMETER_H

#include "MainWindow.h"
#include <chrono>
#include <pdcom5/Subscription.h>
#include <pdcom5/Variable.h>
#include <pdcom5/Subscriber.h>
#include <pdcom5/Subscription.h>

#include <QUrl>
#include <QIcon>
#include <qabstractitemmodel.h>

class ParameterModel;
class ParameterTableModel;
class DataModel;
class DataNode;

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

class Parameter: public QObject, public PdCom::Subscriber
{
        Q_OBJECT

    public:
        Parameter(ParameterModel *);
        Parameter(ParameterTableModel *);
        virtual ~Parameter();

        QUrl url;
        QString name;

        QIcon getIcon() const;

        void fromJson(const QJsonValue &);
        QJsonObject toJson(bool) const;

        void replaceUrl(const QUrl &, const QUrl &);

        PdCom::Subscription *getSubscription() { return &subscription; }

        bool hasData() const { return dataPresent; }
        double getCurrentValue() const { return currentValue; }

        bool hasSavedData() const { return savedDataPresent; }
        double getSavedValue() const { return savedValue; }
        void setSavedValue(double);

        void connectParameters();

    private:
        ParameterModel *const model;
        MainWindow *const mainWindow;
        DataNode *dataNode;
        PdCom::Subscription subscription;
        double currentValue;
        bool dataPresent;
        double savedValue;
        bool savedDataPresent;

        // virtual from PdCom::Subscriber
        void newValues(std::chrono::nanoseconds) override;
        void stateChanged(const PdCom::Subscription &subscription) override
        {
            Q_UNUSED(subscription);
        }

        Parameter();
};

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

#endif
