#ifndef LINUX_DEVICES_ACSBOARDPOLLING_H_
#define LINUX_DEVICES_ACSBOARDPOLLING_H_

#include <fsfw/devicehandlers/DeviceCommunicationIF.h>
#include <fsfw/objectmanager/SystemObject.h>
#include <fsfw/tasks/ExecutableObjectIF.h>
#include <fsfw/tasks/SemaphoreIF.h>
#include <fsfw_hal/devicehandlers/devicedefinitions/mgmRm3100Helpers.h>
#include <fsfw_hal/linux/spi/SpiComIF.h>
#include <mission/devices/devicedefinitions/acsPolling.h>
#include <mission/devices/devicedefinitions/gyroAdisHelpers.h>

class AcsBoardPolling : public SystemObject,
                        public ExecutableObjectIF,
                        public DeviceCommunicationIF {
 public:
  AcsBoardPolling(object_id_t objectId, SpiComIF& lowLevelComIF, GpioIF& gpioIF);

  ReturnValue_t performOperation(uint8_t operationCode) override;
  ReturnValue_t initialize() override;

 private:
  enum class InternalState { IDLE, BUSY } state = InternalState::IDLE;
  MutexIF* ipcLock;
  static constexpr MutexIF::TimeoutType LOCK_TYPE = MutexIF::TimeoutType::WAITING;
  static constexpr uint32_t LOCK_TIMEOUT = 20;
  static constexpr char LOCK_CTX[] = "AcsBoardPolling";
  SemaphoreIF* semaphore;
  std::array<uint8_t, 32> cmdBuf;

  struct DevBase {
    SpiCookie* cookie = nullptr;
    bool performStartup = false;
    acs::SimpleSensorMode mode = acs::SimpleSensorMode::OFF;
    ReturnValue_t replyResult = returnvalue::OK;
  };

  struct GyroAdis : public DevBase {
    adis1650x::Type type;
    Countdown countdown;
    acs::Adis1650XReply ownReply;
    acs::Adis1650XReply readerReply;
  };
  GyroAdis gyro0Adis{};
  GyroAdis gyro2Adis{};

  struct GyroL3g : public DevBase {
    uint8_t sensorCfg[5];
    acs::GyroL3gReply ownReply;
    acs::GyroL3gReply readerReply;
  };
  GyroL3g gyro1L3g{};
  GyroL3g gyro3L3g{};

  struct MgmRm3100 : public DevBase {
    uint8_t tmrcValue = mgmRm3100::TMRC_DEFAULT_37HZ_VALUE;
    acs::MgmRm3100Reply ownReply;
    acs::MgmRm3100Reply readerReply;
  };
  MgmRm3100 mgm1Rm3100;
  MgmRm3100 mgm3Rm3100;

  struct MgmLis3 : public DevBase {
    uint8_t cfg[5]{};
    acs::MgmLis3Reply ownReply;
    acs::MgmLis3Reply readerReply;
  };
  MgmLis3 mgm0Lis3;
  MgmLis3 mgm2Lis3;

  uint8_t* rawReply = nullptr;
  size_t dummy = 0;

  SpiComIF& spiComIF;
  GpioIF& gpioIF;

  ReturnValue_t initializeInterface(CookieIF* cookie) override;
  ReturnValue_t sendMessage(CookieIF* cookie, const uint8_t* sendData, size_t sendLen) override;
  ReturnValue_t getSendSuccess(CookieIF* cookie) override;
  ReturnValue_t requestReceiveMessage(CookieIF* cookie, size_t requestLen) override;
  ReturnValue_t readReceivedMessage(CookieIF* cookie, uint8_t** buffer, size_t* size) override;

  void gyroL3gHandler(GyroL3g& l3g);
  void gyroAdisHandler(GyroAdis& gyro);
  void mgmLis3Handler(MgmLis3& mgm);
  void mgmRm3100Handler(MgmRm3100& mgm);
  // Special readout: 16us stall time between small 2 byte transfers.
  ReturnValue_t readAdisCfg(SpiCookie& spiCookie, size_t transferLen);
};

#endif /* LINUX_DEVICES_ACSBOARDPOLLING_H_ */