#ifndef MISSION_DEVICES_GYROADIS16507HANDLER_H_
#define MISSION_DEVICES_GYROADIS16507HANDLER_H_

#include <mission/acs/acsBoardPolling.h>
#include <mission/acs/gyroAdisHelpers.h>

#include "FSFWConfig.h"
#include "OBSWConfig.h"
#include "fsfw/devicehandlers/DeviceHandlerBase.h"
#include "fsfw/globalfunctions/PeriodicOperationDivider.h"

/**
 * @brief   Device handle for the ADIS16507 Gyroscope by Analog Devices
 * @details
 * Flight manual:
 * https://egit.irs.uni-stuttgart.de/redmine/projects/eive-flight-manual/wiki/ADIS16507_Gyro
 */
class GyrAdis1650XHandler : public DeviceHandlerBase {
 public:
  GyrAdis1650XHandler(object_id_t objectId, object_id_t deviceCommunication, CookieIF *comCookie,
                      adis1650x::Type type);

  void enablePeriodicPrintouts(bool enable, uint8_t divider);

  // DeviceHandlerBase abstract function implementation
  void doStartUp() override;
  void doShutDown() override;
  ReturnValue_t buildNormalDeviceCommand(DeviceCommandId_t *id) override;
  ReturnValue_t buildTransitionDeviceCommand(DeviceCommandId_t *id) override;
  ReturnValue_t buildCommandFromCommand(DeviceCommandId_t deviceCommand, const uint8_t *commandData,
                                        size_t commandDataLen) override;
  void fillCommandAndReplyMap() override;
  ReturnValue_t scanForReply(const uint8_t *start, size_t remainingSize, DeviceCommandId_t *foundId,
                             size_t *foundLen) override;
  ReturnValue_t interpretDeviceReply(DeviceCommandId_t id, const uint8_t *packet) override;
  uint32_t getTransitionDelayMs(Mode_t modeFrom, Mode_t modeTo) override;
  ReturnValue_t initializeLocalDataPool(localpool::DataPool &localDataPoolMap,
                                        LocalDataPoolManager &poolManager) override;
  LocalPoolDataSetBase *getDataSetHandle(sid_t sid) override;

 private:
  std::array<uint8_t, 32> cmdBuf;
  acs::Adis1650XRequest adisRequest;
  adis1650x::Type adisType;
  AdisGyroPrimaryDataset primaryDataset;
  AdisGyroConfigDataset configDataset;
  double sensitivity = adis1650x::SENSITIVITY_UNSET;

  bool warningSwitch = true;

  enum class InternalState { NONE, STARTUP, SHUTDOWN };

  InternalState internalState = InternalState::STARTUP;
  bool commandExecuted = false;

  PoolEntry<uint16_t> rangMdl = PoolEntry<uint16_t>();

  adis1650x::BurstModes getBurstMode();

  Countdown breakCountdown;
  void prepareWriteCommand(uint8_t startReg, uint8_t valueOne, uint8_t valueTwo);
  ReturnValue_t preparePeriodicRequest(acs::SimpleSensorMode mode);

  ReturnValue_t handleSensorData(const uint8_t *packet);

  bool periodicPrintout = false;
  PeriodicOperationDivider debugDivider = PeriodicOperationDivider(3);
};

#endif /* MISSION_DEVICES_GYROADIS16507HANDLER_H_ */