/**
 * This file is part of ArmarX.
 *
 * ArmarX is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * ArmarX 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 this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * @author     Fabian Reister ( fabian dot reister at kit dot edu )
 * @date       2021
 * @copyright  http://www.gnu.org/licenses/gpl-2.0.txt
 *             GNU General Public License
 */

#pragma once

#include <ArmarXCore/core/ComponentPlugin.h>
#include <ArmarXCore/core/ManagedIceObject.h>
#include <ArmarXCore/core/time/Duration.h>

#include <RobotAPI/interface/components/RobotHealthInterface.h>

namespace armarx::plugins
{

    class HeartbeatComponentPlugin : public ComponentPlugin
    {
    public:
        using ComponentPlugin::ComponentPlugin;

        /**
         * @brief register component to heartbeat
         */
        void signUp(const std::string& channelName = "",
                    const std::vector<std::string>& aliases = {},
                    const std::string& description = "");

        /**
         * @brief register component to heartbeat
         */
        void signUp(const armarx::core::time::Duration& warning,
                    const armarx::core::time::Duration& error,
                    const std::vector<std::string>& aliases = {},
                    const std::string& description = "");

        /**
         * @brief register component to heartbeat
         */
        void signUp(const std::string& channelName,
                    const armarx::core::time::Duration& warning,
                    const armarx::core::time::Duration& error,
                    const std::vector<std::string>& aliases = {},
                    const std::string& description = "");

        /**
         * @brief register component to heartbeat, possibly with different component name
         */
        void signUp(const RobotHealthHeartbeatArgs& args);

        /**
         * @brief Sends out a heartbeat using the default config
         *
         */
        void heartbeat();

        /**
         * @brief Sends out a heartbeat for a subchannel.
         *
         * Note: You must call configureHeartbeatChannel(...) first to register the channel config!
         *
         * @param channel Identifier of the heartbeat channel
         */
        void heartbeatOnChannel(const std::string& channelName);

    protected:
        void preOnInitComponent() override;
        void postOnInitComponent() override;
        // void preOnConnectComponent() override;
        void postOnConnectComponent() override;

        void postCreatePropertyDefinitions(PropertyDefinitionsPtr& properties) override;

    private:
        RobotHealthComponentInterfacePrx robotHealthComponentPrx;

        //
        static constexpr auto healthPropertyName = "heartbeat.ComponentName";
        static constexpr auto maximumCycleTimeWarningMSPropertyName =
            "heartbeat.maximumCycleTimeWarningMS";
        static constexpr auto maximumCycleTimeErrorMSPropertyName =
            "heartbeat.maximumCycleTimeErrorMS";

        struct Properties
        {
            long maximumCycleTimeWarningMS = 100; // [ms]
            long maximumCycleTimeErrorMS = 200; // [ms]
        } p;

        //! default config used in heartbeat(), set via properties
        RobotHealthHeartbeatArgs defaultHeartbeatArgs;
    };
} // namespace armarx::plugins