#include <fsfw/filesystem/HasFileSystemIF.h>
#include <linux/payload/ScexHelper.h>
#include <mission/memory/SdCardMountedIF.h>
#include <mission/payload/ScexDeviceHandler.h>
#include <mission/payload/scexHelpers.h>

#include <algorithm>
#include <ctime>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <random>

#include "eive/definitions.h"
#include "fsfw/globalfunctions/CRC.h"

using std::ofstream;
using namespace returnvalue;

ScexDeviceHandler::ScexDeviceHandler(object_id_t objectId, ScexUartReader& reader, CookieIF* cookie,
                                     SdCardMountedIF& sdcMan)
    : DeviceHandlerBase(objectId, reader.getObjectId(), cookie), sdcMan(sdcMan), reader(reader) {}

ScexDeviceHandler::~ScexDeviceHandler() {}

void ScexDeviceHandler::doStartUp() { setMode(MODE_ON); }

void ScexDeviceHandler::doShutDown() {
  reader.reset();
  commandActive = false;
  fileNameSet = false;
  multiFileFinishOutstanding = false;
  setMode(_MODE_POWER_DOWN);
}

ReturnValue_t ScexDeviceHandler::buildNormalDeviceCommand(DeviceCommandId_t* id) { return OK; }

ReturnValue_t ScexDeviceHandler::buildTransitionDeviceCommand(DeviceCommandId_t* id) { return OK; }

ReturnValue_t ScexDeviceHandler::buildCommandFromCommand(DeviceCommandId_t deviceCommand,
                                                         const uint8_t* commandData,
                                                         size_t commandDataLen) {
  using namespace scex;

  auto cmdTyped = static_cast<scex::Cmds>(deviceCommand);
  if (std::find(VALID_CMDS.begin(), VALID_CMDS.end(), deviceCommand) == VALID_CMDS.end()) {
    return DeviceHandlerIF::COMMAND_NOT_IMPLEMENTED;
  }
  bool tempCheck = false;
  if (commandDataLen == 1) {
    tempCheck = commandData[0];
  }
  if (commandActive) {
    return DeviceHandlerIF::BUSY;
  }

  switch (deviceCommand) {
    case (PING): {
      finishCountdown.setTimeout(SHORT_CD);
      // countdown starten
      finishCountdown.resetTimer();
      prepareScexCmd(cmdTyped, {cmdBuf.data(), cmdBuf.size()}, rawPacketLen, {nullptr, 0},
                     tempCheck);
      break;
    }
    case (EXP_STATUS_CMD): {
      finishCountdown.setTimeout(SHORT_CD);
      // countdown starten
      finishCountdown.resetTimer();
      prepareScexCmd(cmdTyped, {cmdBuf.data(), cmdBuf.size()}, rawPacketLen, {nullptr, 0},
                     tempCheck);
      break;
    }
    case (ION_CMD): {
      finishCountdown.setTimeout(SHORT_CD);
      // countdown starten
      finishCountdown.resetTimer();
      prepareScexCmd(cmdTyped, {cmdBuf.data(), cmdBuf.size()}, rawPacketLen, {nullptr, 0},
                     tempCheck);
      break;
    }
    case (TEMP_CMD): {
      finishCountdown.setTimeout(SHORT_CD);
      // countdown starten
      finishCountdown.resetTimer();
      prepareScexCmd(cmdTyped, {cmdBuf.data(), cmdBuf.size()}, rawPacketLen, {nullptr, 0},
                     tempCheck);
      break;
    }
    case (FRAM): {
      finishCountdown.setTimeout(LONG_CD);
      // countdown starten
      finishCountdown.resetTimer();
      if (debugMode) {
        uint32_t remainingMillis = finishCountdown.getRemainingMillis();

        sif::info << "ScexDeviceHandler::buildCommandFromCommand: RemainingMillis: "
                  << remainingMillis << std::endl;
      }

      multiFileFinishOutstanding = true;
      prepareScexCmd(cmdTyped, {cmdBuf.data(), cmdBuf.size()}, rawPacketLen,
                     {commandData + 1, commandDataLen - 1}, tempCheck);
      updatePeriodicReply(true, deviceCommand);
      break;
    }
    case (ONE_CELL): {
      finishCountdown.setTimeout(LONG_CD);
      // countdown starts
      finishCountdown.resetTimer();
      multiFileFinishOutstanding = true;
      prepareScexCmd(cmdTyped, {cmdBuf.data(), cmdBuf.size()}, rawPacketLen,
                     {commandData + 1, commandDataLen - 1}, tempCheck);
      updatePeriodicReply(true, deviceCommand);
      break;
    }
    case (ALL_CELLS_CMD): {
      finishCountdown.setTimeout(LONG_CD);
      // countdown starts
      finishCountdown.resetTimer();
      multiFileFinishOutstanding = true;
      prepareScexCmd(cmdTyped, {cmdBuf.data(), cmdBuf.size()}, rawPacketLen,
                     {commandData + 1, commandDataLen - 1}, tempCheck);
      updatePeriodicReply(true, deviceCommand);
      break;
    }
    default: {
      return DeviceHandlerIF::COMMAND_NOT_IMPLEMENTED;
    }
  }
  commandActive = true;
  rawPacket = cmdBuf.data();
  return OK;
}

void ScexDeviceHandler::fillCommandAndReplyMap() {
  insertInCommandAndReplyMap(scex::Cmds::PING, 5, nullptr, 0, false, false, 0, &finishCountdown);
  insertInCommandAndReplyMap(scex::Cmds::ION_CMD, 3, nullptr, 0, false, false, 0, &finishCountdown);
  insertInCommandAndReplyMap(scex::Cmds::TEMP_CMD, 3, nullptr, 0, false, false, 0,
                             &finishCountdown);
  insertInCommandAndReplyMap(scex::Cmds::EXP_STATUS_CMD, 3, nullptr, 0, false, false, 0,
                             &finishCountdown);

  insertInCommandAndReplyMap(scex::Cmds::ALL_CELLS_CMD, 0, nullptr, 0, true, false,
                             scex::Cmds::ALL_CELLS_CMD, &finishCountdown);
  insertInCommandAndReplyMap(scex::Cmds::ONE_CELL, 0, nullptr, 0, true, false, scex::Cmds::ONE_CELL,
                             &finishCountdown);
  insertInCommandAndReplyMap(scex::Cmds::FRAM, 0, nullptr, 0, true, false, scex::Cmds::FRAM,
                             &finishCountdown);

  insertInReplyMap(scex::Cmds::ERROR_REPLY, 3);
}

ReturnValue_t ScexDeviceHandler::scanForReply(const uint8_t* start, size_t remainingSize,
                                              DeviceCommandId_t* foundId, size_t* foundLen) {
  size_t len = remainingSize;
  ReturnValue_t result = helper.deSerialize(&start, &len);

  if (result == ScexHelper::INVALID_CRC) {
    sif::warning << "ScexDeviceHandler::scanForReply: CRC invalid" << std::endl;
    *foundLen = remainingSize;
  } else {
    result = handleValidReply(remainingSize, foundId, foundLen);
  }
  return result;
}

ReturnValue_t ScexDeviceHandler::handleValidReply(size_t remSize, DeviceCommandId_t* foundId,
                                                  size_t* foundLen) {
  using namespace scex;
  ReturnValue_t result = OK;

  switch (helper.getCmd()) {
    case (FRAM): {
      if (debugMode) {
        uint32_t remainingMillis = finishCountdown.getRemainingMillis();

        sif::info << "ScexDeviceHandler::handleValidReply: RemMillis: " << remainingMillis
                  << std::endl;
      }
      result = APERIODIC_REPLY;
      break;
    }
    case (ONE_CELL): {
      result = APERIODIC_REPLY;
      break;
    }
    case (ALL_CELLS_CMD): {
      result = APERIODIC_REPLY;
      break;
    }
    default: {
      break;
    }
  }
  if (result == APERIODIC_REPLY and multiFileFinishOutstanding) {
    finishAction(true, helper.getCmd(), OK);
    multiFileFinishOutstanding = false;
  }

  *foundId = helper.getCmd();
  *foundLen = remSize;
  return result;
}

ReturnValue_t ScexDeviceHandler::interpretDeviceReply(DeviceCommandId_t id, const uint8_t* packet) {
  using namespace scex;

  ReturnValue_t status = OK;
  auto multiFileHandler = [&](const char* cmdName) {
    if ((helper.getPacketCounter() == 1) or (not fileNameSet)) {
      status = generateNewScexFile(cmdName);
      if (status != returnvalue::OK) {
        return status;
      }
      fileNameSet = true;
    } else {
      ofstream out(fileName,
                   ofstream::binary | ofstream::app);  // append
      if (out.bad()) {
        sif::error << "ScexDeviceHandler::handleValidReply: Could not open file " << fileName
                   << std::endl;
        return FAILED;
      }
      out << helper;
    }
    return OK;
  };
  id = helper.getCmd();
  switch (id) {
    case (PING): {
      status = generateNewScexFile(PING_IDLE_BASE_NAME);
      break;
    }
    case (ION_CMD): {
      status = generateNewScexFile(ION_BASE_NAME);
      break;
    }
    case (TEMP_CMD): {
      status = generateNewScexFile(TEMPERATURE_BASE_NAME);
      break;
    }
    case (EXP_STATUS_CMD): {
      status = generateNewScexFile(EXP_STATUS_BASE_NAME);
      break;
    }
    case (FRAM): {
      status = multiFileHandler(FRAM_BASE_NAME);
      break;
    }
    case (ONE_CELL): {
      status = multiFileHandler(ONE_CELL_BASE_NAME);
      break;
    }
    case (ALL_CELLS_CMD): {
      status = multiFileHandler(ALL_CELLS_BASE_NAME);
      break;
    }
    default:
      // Unknown DeviceCommand
      return DeviceHandlerIF::COMMAND_NOT_IMPLEMENTED;
  }
  if (helper.getPacketCounter() == helper.getTotalPacketCounter()) {
    reader.finish();
    commandActive = false;
    if (id != PING) {
      fileNameSet = false;
    }
    if (id == FRAM or id == ALL_CELLS_CMD or id == ONE_CELL) {
      triggerEvent(MULTI_PACKET_COMMAND_DONE, id);
      updatePeriodicReply(false, id);
    }
  }
  if (debugMode) {
    uint32_t remainingMillis = finishCountdown.getRemainingMillis();
    sif::info << __FILE__ << __func__ << "(" << __LINE__ << ") RemMillis: " << remainingMillis
              << std::endl;
  }
  return status;
}

void ScexDeviceHandler::performOperationHook() {
  auto mntPrefix = sdcMan.getCurrentMountPrefix();
  if (mntPrefix != nullptr) {
    std::filesystem::path fullFilePath = mntPrefix;
    std::error_code e;
    fullFilePath /= "scex";
    bool fileExists = std::filesystem::exists(fullFilePath, e);
    if (not fileExists) {
      bool created = std::filesystem::create_directory(fullFilePath, e);
      if (not created) {
        sif::error << "Could not create SCEX directory: " << e << std::endl;
      }
    }
  }
  uint32_t remainingMillis = finishCountdown.getRemainingMillis();
  if (commandActive and finishCountdown.hasTimedOut()) {
    triggerEvent(scex::EXPERIMENT_TIMEDOUT, currCmd, 0);
    reader.finish();
    sif::warning << "ScexDeviceHandler::scanForReply: Reader timeout; RemMillis: "
                 << remainingMillis << std::endl;
    fileNameSet = false;
    commandActive = false;
  }
}

uint32_t ScexDeviceHandler::getTransitionDelayMs(Mode_t modeFrom, Mode_t modeTo) { return OK; }

ReturnValue_t ScexDeviceHandler::getSwitches(const uint8_t** switches, uint8_t* numberOfSwitches) {
  if (switchId) {
    *numberOfSwitches = 1;
    *switches = &switchId.value();
  }
  return OK;
}

ReturnValue_t ScexDeviceHandler::initializeLocalDataPool(localpool::DataPool& localDataPoolMap,
                                                         LocalDataPoolManager& poolManager) {
  return OK;
}

ReturnValue_t ScexDeviceHandler::generateNewScexFile(const char* cmdName) {
  char timeString[64]{};
  auto activeSd = sdcMan.getActiveSdCard();
  if (not activeSd) {
    return HasFileSystemIF::FILESYSTEM_INACTIVE;
  }

  std::ostringstream oss;
  auto prefix = sdcMan.getCurrentMountPrefix();
  if (prefix == nullptr) {
    return returnvalue::FAILED;
  }
  timeval tv;
  Clock::getClock_timeval(&tv);
  time_t epoch = tv.tv_sec;
  struct tm* time = gmtime(&epoch);
  size_t writtenBytes = strftime(reinterpret_cast<char*>(timeString), sizeof(timeString),
                                 config::FILE_DATE_FORMAT, time);
  if (writtenBytes == 0) {
    sif::error << "PersistentTmStore::createMostRecentFile: Could not create file timestamp"
               << std::endl;
    return returnvalue::FAILED;
  }
  oss << prefix << "/scex/scex-" << cmdName << "-" << timeString << ".bin";
  fileName = oss.str();
  ofstream out(fileName, ofstream::binary);
  if (out.bad()) {
    sif::error << "ScexDeviceHandler::interpretDeviceReply: Could not open file " << fileName
               << std::endl;
    return FAILED;
  }
  out << helper;
  return OK;
}

void ScexDeviceHandler::setPowerSwitcher(PowerSwitchIF& powerSwitcher, power::Switch_t switchId) {
  DeviceHandlerBase::setPowerSwitcher(&powerSwitcher);
  this->switchId = switchId;
}

ReturnValue_t ScexDeviceHandler::initializeAfterTaskCreation() {
  return DeviceHandlerBase::initializeAfterTaskCreation();
}