#ifndef BSP_Q7S_MEMORY_LOCALPARAMETERHANDLER_H_
#define BSP_Q7S_MEMORY_LOCALPARAMETERHANDLER_H_

#include <mission/memory/NvmParameterBase.h>
#include <mission/memory/SdCardMountedIF.h>

#include <string>

/**
 * @brief	Class to handle persistent parameters
 *
 */
class LocalParameterHandler : public NVMParameterBase {
 public:
  static constexpr uint8_t INTERFACE_ID = CLASS_ID::LOCAL_PARAM_HANDLER;

  static constexpr ReturnValue_t SD_NOT_READY = returnvalue::makeCode(INTERFACE_ID, 0);
  /**
   * @brief	Constructor
   *
   * @param sdRelativeName	Absolute name of json file relative to mount
   * 												directory
   * of SD card. E.g. conf/example.json
   * @param sdcMan	Pointer to SD card manager
   */
  LocalParameterHandler(std::string sdRelativeName, SdCardMountedIF* sdcMan);
  virtual ~LocalParameterHandler();

  /**
   * @brief Will initialize the local parameter handler
   *
   * @return OK if successful, otherwise error return value
   */
  ReturnValue_t initialize();

  /**
   * @brief Function to add parameter to json file. If the json file does
   * 				not yet exist it will be created here.
   *
   * @param key	The string to identify the parameter
   * @param value	The value to set for this parameter
   *
   * @return OK if successful, otherwise error return value
   *
   * @details The function will add the parameter only if it is not already
   * 					present in the json file
   */
  template <typename T>
  ReturnValue_t addParameter(std::string key, T value);

  /**
   * @brief	Function will update a parameter which already exists in the json
   * 				file
   *
   * @param key 	The unique string to identify the parameter to update
   * @param value The new new value to set
   *
   * @return OK if successful, otherwise error return value
   */
  template <typename T>
  ReturnValue_t updateParameter(std::string key, T value);

 private:
  // Name relative to mount point of SD card where parameters will be stored
  std::string sdRelativeName;

  SdCardMountedIF* sdcMan;

  virtual ReturnValue_t writeJsonFile();

  /**
   * @brief This function sets the name of the json file dependent on the
   * 				currently active SD card
   *
   * @return OK if successful, otherwise error return value
   */
  ReturnValue_t updateFullName();
};

template <typename T>
inline ReturnValue_t LocalParameterHandler::addParameter(std::string key, T value) {
  ReturnValue_t result = insertValue(key, value);
  if (result != returnvalue::OK) {
    return result;
  }
  result = writeJsonFile();
  if (result != returnvalue::OK) {
    return result;
  }
  return returnvalue::OK;
}

template <typename T>
inline ReturnValue_t LocalParameterHandler::updateParameter(std::string key, T value) {
  ReturnValue_t result = setValue(key, value);
  if (result != returnvalue::OK) {
    return result;
  }
  result = writeJsonFile();
  if (result != returnvalue::OK) {
    return result;
  }
  return returnvalue::OK;
}

#endif /* BSP_Q7S_MEMORY_LOCALPARAMETERHANDLER_H_ */