 /********************************************************************************
 * Copyright (c) 2025-2026 ZF Friedrichshafen AG
 *
 * This program and the accompanying materials are made available under the 
 * terms of the Apache License Version 2.0 which is available at
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * SPDX-License-Identifier: Apache-2.0 
 ********************************************************************************/

#include <string>
#include <support/app_control.h>
#include <support/signal_support.h>


/**
* @brief Application Class of the open trunk example
*/
class CTrunkControl
{
public:

    /**
    * @brief Start and initialize the application control and load vehicle devices and basic services
    * @param[in] bSimulate used in case it is running as a standalone application
    *                      if false an asc file is used for setting the input speed, otherwise a simulation thread 
    * @param[in] uiInstance Instance number the application will connect to. 0 will start a standalone application 
    * @return Return true on success otherwise false
    */
    bool Initialize(bool bSimulate, uint32_t uiInstance);

    /**
    * @brief After initialization/configuration the system mode needs to be set to running mode
    */
    void SetRunningMode();

    /**
    * @brief Shutdown the system.
    */
    void Shutdown();

    /**
     * @brief Starts a thread to set the speed value.
     */    
    void StartSimulation();

private:

    /**
    * @brief check if SDV_FRAMEWORK_RUNTIME environment variable exists
    * @return Return true if environment variable is found otherwise false
    */
    bool IsSDVFrameworkEnvironmentSet();

    /**
     * @brief Provide simulated signals until m_bRunning is disabled.
     */
    void SimulateThreadFunction();    

    /**
     * @brief Load config file and register vehicle device and basic service.
     * @param[in] inputMsg message string to be printed on console in case of success and failure
     * @param[in] configFileName config toml file name
     * @return Return true on success otherwise false
     */
    bool LoadConfigFile(const std::string& inputMsg, const std::string& configFileName);

    sdv::app::CAppControl    m_appcontrol;               ///< App-control of Eclipse Open Vehicle-API.
    bool                     m_bInitialized = false;     ///< Set when initialized.

    sdv::core::CSignal       m_signalSpeed;              ///< Vehicle speed signal (input) - simulated datalink
    sdv::core::CSignal       m_signalTrunk;              ///< Trunk signal (output) - simulated datalink
    std::atomic_bool         m_bSimulateRunning = false; ///< When set, the simulation thread is running.    
    sdv::core::secure_thread m_threadSimulate;           ///< Simulation datalink thread.    
};
