#ifndef BSP_Q7S_CORE_CORECONTROLLER_H_
#define BSP_Q7S_CORE_CORECONTROLLER_H_

#include <fsfw/globalfunctions/PeriodicOperationDivider.h>
#include <libxiphos.h>

#include <cstddef>

#include "CoreDefinitions.h"
#include "bsp_q7s/memory/SdCardManager.h"
#include "events/subsystemIdRanges.h"
#include "fsfw/controller/ExtendedControllerBase.h"
#include "mission/devices/devicedefinitions/GPSDefinitions.h"

class Timer;
class SdCardManager;

namespace xsc {

enum Chip : int { CHIP_0, CHIP_1, NO_CHIP, SELF_CHIP, ALL_CHIP };
enum Copy : int { COPY_0, COPY_1, NO_COPY, SELF_COPY, ALL_COPY };

}  // namespace xsc

struct RebootFile {
  static constexpr uint8_t DEFAULT_MAX_BOOT_CNT = 10;

  bool enabled = true;
  size_t maxCount = DEFAULT_MAX_BOOT_CNT;
  uint32_t img00Cnt = 0;
  uint32_t img01Cnt = 0;
  uint32_t img10Cnt = 0;
  uint32_t img11Cnt = 0;
  bool img00Lock = false;
  bool img01Lock = false;
  bool img10Lock = false;
  bool img11Lock = false;
  uint32_t* relevantBootCnt = &img00Cnt;
  bool bootFlag = false;
  xsc::Chip lastChip = xsc::Chip::CHIP_0;
  xsc::Copy lastCopy = xsc::Copy::COPY_0;
  xsc::Chip mechanismNextChip = xsc::Chip::NO_CHIP;
  xsc::Copy mechanismNextCopy = xsc::Copy::NO_COPY;
};

class CoreController : public ExtendedControllerBase {
 public:
  static xsc::Chip CURRENT_CHIP;
  static xsc::Copy CURRENT_COPY;

  static constexpr char CHIP_PROT_SCRIPT[] = "get-chip-prot-status.sh";
  static constexpr char CHIP_STATE_FILE[] = "/tmp/chip_prot_status.txt";
  static constexpr char CURR_COPY_FILE[] = "/tmp/curr_copy.txt";
  static constexpr char CONF_FOLDER[] = "conf";
  static constexpr char VERSION_FILE_NAME[] = "version.txt";
  static constexpr char REBOOT_FILE_NAME[] = "reboot.txt";
  static constexpr char TIME_FILE_NAME[] = "time.txt";
  const std::string VERSION_FILE =
      "/" + std::string(CONF_FOLDER) + "/" + std::string(VERSION_FILE_NAME);
  const std::string REBOOT_FILE =
      "/" + std::string(CONF_FOLDER) + "/" + std::string(REBOOT_FILE_NAME);
  const std::string TIME_FILE = "/" + std::string(CONF_FOLDER) + "/" + std::string(TIME_FILE_NAME);

  static constexpr ActionId_t LIST_DIRECTORY_INTO_FILE = 0;
  static constexpr ActionId_t SWITCH_REBOOT_FILE_HANDLING = 5;
  static constexpr ActionId_t RESET_REBOOT_COUNTERS = 6;
  static constexpr ActionId_t SWITCH_IMG_LOCK = 7;
  static constexpr ActionId_t SET_MAX_REBOOT_CNT = 8;

  //! Reboot using the xsc_boot_copy command
  static constexpr ActionId_t XSC_REBOOT_OBC = 32;
  static constexpr ActionId_t MOUNT_OTHER_COPY = 33;
  //! Reboot using the reboot command
  static constexpr ActionId_t REBOOT_OBC = 34;

  static constexpr uint8_t SUBSYSTEM_ID = SUBSYSTEM_ID::CORE;

  static constexpr Event ALLOC_FAILURE = event::makeEvent(SUBSYSTEM_ID, 0, severity::MEDIUM);
  //! [EXPORT] : [COMMENT]  Software reboot occurred. Can also be a systemd reboot.
  //! P1: Current Chip, P2: Current Copy
  static constexpr Event REBOOT_SW = event::makeEvent(SUBSYSTEM_ID, 1, severity::MEDIUM);
  //! [EXPORT] : [COMMENT] The reboot mechanism was triggered.
  //! P1: First 16 bits: Last Chip, Last 16 bits: Last Copy,
  //! P2: Each byte is the respective reboot count for the slots
  static constexpr Event REBOOT_MECHANISM_TRIGGERED =
      event::makeEvent(SUBSYSTEM_ID, 2, severity::MEDIUM);
  //! Trying to find a way how to determine that the reboot came from ProASIC3 or PCDU..
  static constexpr Event REBOOT_HW = event::makeEvent(SUBSYSTEM_ID, 3, severity::MEDIUM);

  CoreController(object_id_t objectId);
  virtual ~CoreController();

  ReturnValue_t initialize() override;

  ReturnValue_t initializeAfterTaskCreation() override;

  ReturnValue_t executeAction(ActionId_t actionId, MessageQueueId_t commandedBy,
                              const uint8_t* data, size_t size) override;

  ReturnValue_t handleCommandMessage(CommandMessage* message) override;
  void performControlOperation() override;

  /**
   * Generate a file containing the chip lock/unlock states inside /tmp/chip_prot_status.txt
   * @return
   */
  static ReturnValue_t generateChipStateFile();
  static ReturnValue_t incrementAllocationFailureCount();
  static void getCurrentBootCopy(xsc::Chip& chip, xsc::Copy& copy);

  ReturnValue_t updateProtInfo(bool regenerateChipStateFile = true);

  /**
   * Checks whether the target chip and copy are write protected and protect set them to a target
   * state where applicable.
   * @param targetChip
   * @param targetCopy
   * @param protect       Target state
   * @param protOperationPerformed [out] Can be used to determine whether any operation
   * was performed
   * @param updateProtFile Specify whether the protection info file is updated
   * @return
   */
  ReturnValue_t setBootCopyProtection(xsc::Chip targetChip, xsc::Copy targetCopy, bool protect,
                                      bool& protOperationPerformed, bool updateProtFile = true);

  bool sdInitFinished() const;

 private:
  static constexpr MutexIF::TimeoutType TIMEOUT_TYPE = MutexIF::TimeoutType::WAITING;
  static constexpr uint32_t MUTEX_TIMEOUT = 20;
  // Designated value for rechecking FIFO open
  static constexpr int RETRY_FIFO_OPEN = -2;
  int watchdogFifoFd = 0;
  GpsHyperion::FixMode gpsFix = GpsHyperion::FixMode::UNKNOWN;

  // States for SD state machine, which is used in non-blocking mode
  enum class SdStates {
    NONE,
    START,
    GET_INFO,
    SET_STATE_SELF,
    MOUNT_SELF,
    // Determine operations for other SD card, depending on redundancy configuration
    DETERMINE_OTHER,
    SET_STATE_OTHER,
    // Mount or unmount other
    MOUNT_UNMOUNT_OTHER,
    // Skip period because the shell command used to generate the info file sometimes is
    // missing the last performed operation if executed too early
    SKIP_CYCLE_BEFORE_INFO_UPDATE,
    UPDATE_INFO,
    // SD initialization done
    IDLE,
    // Used if SD switches or mount commands are issued via telecommand
    SET_STATE_FROM_COMMAND,
  };
  static constexpr bool BLOCKING_SD_INIT = false;

  SdCardManager* sdcMan = nullptr;
  MessageQueueIF* eventQueue = nullptr;

  struct SdInfo {
    sd::SdCard pref = sd::SdCard::NONE;
    sd::SdCard other = sd::SdCard::NONE;
    sd::SdState prefState = sd::SdState::OFF;
    sd::SdState otherState = sd::SdState::OFF;
    std::string prefChar = "0";
    std::string otherChar = "1";
    std::pair<bool, bool> mountSwitch = {true, true};
    SdStates state = SdStates::START;
    // Used to track whether a command was executed
    bool commandExecuted = true;
    bool initFinished = false;
    SdCardManager::SdStatePair currentState;
    uint16_t cycleCount = 0;
    // These two flags are related to external commanding
    bool commandIssued = false;
    bool commandFinished = false;
    sd::SdState currentlyCommandedState = sd::SdState::OFF;
    sd::SdCard commandedCard = sd::SdCard::NONE;
    sd::SdState commandedState = sd::SdState::OFF;
  } sdInfo;
  RebootFile rebootFile = {};
  std::string currMntPrefix;
  bool performOneShotSdCardOpsSwitch = true;

  /**
   * Index 0: Chip 0 Copy 0
   * Index 1: Chip 0 Copy 1
   * Index 2: Chip 1 Copy 0
   * Index 3: Chip 1 Copy 1
   */
  std::array<bool, 4> protArray;
  PeriodicOperationDivider opDivider5;
  PeriodicOperationDivider opDivider10;

  core::HkSet hkSet;

  ReturnValue_t initializeLocalDataPool(localpool::DataPool& localDataPoolMap,
                                        LocalDataPoolManager& poolManager) override;
  Countdown sdCardCheckCd = Countdown(120000);
  LocalPoolDataSetBase* getDataSetHandle(sid_t sid) override;
  ReturnValue_t checkModeCommand(Mode_t mode, Submode_t submode, uint32_t* msToReachTheMode);
  void performMountedSdCardOperations();
  ReturnValue_t initVersionFile();

  ReturnValue_t initClockFromTimeFile();
  ReturnValue_t performSdCardCheck();
  ReturnValue_t timeFileHandler();
  ReturnValue_t initBootCopy();
  ReturnValue_t initWatchdogFifo();
  ReturnValue_t initSdCardBlocking();
  void initPrint();

  ReturnValue_t sdStateMachine();
  void updateSdInfoOther();
  ReturnValue_t sdCardSetup(sd::SdCard sdCard, sd::SdState targetState, std::string sdChar,
                            bool printOutput = true);
  ReturnValue_t sdColdRedundantBlockingInit();

  void currentStateSetter(sd::SdCard sdCard, sd::SdState newState);
  void executeNextExternalSdCommand();
  void checkExternalSdCommandStatus();
  void performRebootFileHandling(bool recreateFile);

  ReturnValue_t actionListDirectoryIntoFile(ActionId_t actionId, MessageQueueId_t commandedBy,
                                            const uint8_t* data, size_t size);
  ReturnValue_t actionXscReboot(const uint8_t* data, size_t size);
  ReturnValue_t actionReboot(const uint8_t* data, size_t size);

  ReturnValue_t gracefulShutdownTasks(xsc::Chip chip, xsc::Copy copy, bool& protOpPerformed);

  void performWatchdogControlOperation();

  ReturnValue_t handleProtInfoUpdateLine(std::string nextLine);
  int handleBootCopyProtAtIndex(xsc::Chip targetChip, xsc::Copy targetCopy, bool protect,
                                bool& protOperationPerformed, bool selfChip, bool selfCopy,
                                bool allChips, bool allCopies, uint8_t arrIdx);
  void determineAndExecuteReboot(RebootFile& rf, bool& needsReboot, xsc::Chip& tgtChip,
                                 xsc::Copy& tgtCopy);
  void resetRebootCount(xsc::Chip tgtChip, xsc::Copy tgtCopy);
  void setRebootMechanismLock(bool lock, xsc::Chip tgtChip, xsc::Copy tgtCopy);
  bool parseRebootFile(std::string path, RebootFile& file);
  void rewriteRebootFile(RebootFile file);
  void readHkData();
  bool isNumber(const std::string& s);
};

#endif /* BSP_Q7S_CORE_CORECONTROLLER_H_ */