//===-- deluxe/DeluxeSensor.cpp ---------------------------------*- C++ -*-===//
//
//                                 The RoSA Framework
//
//===----------------------------------------------------------------------===//
///
/// \file deluxe/DeluxeSensor.cpp
///
/// \author David Juhasz (david.juhasz@tuwien.ac.at)
///
/// \date 2017-2019
///
/// \brief Implementation of rosa/deluxe/DeluxeSensor.hpp.
///
//===----------------------------------------------------------------------===//

#include "rosa/deluxe/DeluxeSensor.hpp"

#include "rosa/deluxe/DeluxeSystem.hpp"

namespace rosa {
namespace deluxe {

bool DeluxeSensor::inv(void) const noexcept {
  // Check execution policy.
  // \note The \c rosa::System the \c rosa::Unit is created with is a
  // \c rosa::DeluxeSystem.
  const DeluxeSystem &DS = static_cast<DeluxeSystem &>(Unit::system());
  if (!ExecutionPolicy || !ExecutionPolicy->canHandle(Self, DS)) {
    return false;
  }

  // All checks were successful, the invariant is held.
  return true;
}

DeluxeSensor::~DeluxeSensor(void) noexcept {
  ASSERT(inv());
  LOG_TRACE("Destroying DeluxeSensor...");

  // Make sure \p this object is not a registered *slave*.
  if (Master) {
    ASSERT(unwrapAgent(*Master).Kind == atoms::AgentKind); // Sanity check.
    DeluxeAgent &M = static_cast<DeluxeAgent&>(unwrapAgent(*Master));
    ASSERT(M.positionOfSlave(self()) != M.NumberOfInputs); // Sanity check.
    M.registerSlave(M.positionOfSlave(self()), {});
    Master = {};
  }
}

const DeluxeExecutionPolicy &DeluxeSensor::executionPolicy(void) const
    noexcept {
  ASSERT(inv());
  return *ExecutionPolicy;
}

bool DeluxeSensor::setExecutionPolicy(
    std::unique_ptr<DeluxeExecutionPolicy> &&EP) noexcept {
  ASSERT(inv());
  LOG_TRACE_STREAM << "DeluxeSensor " << FullName
                   << " setting execution policy " << *EP << std::endl;
  bool Success = false;
  // \note The \c rosa::System the \c rosa::Unit is created with is a
  // \c rosa::DeluxeSystem.
  const DeluxeSystem &DS = static_cast<DeluxeSystem &>(Unit::system());
  if (EP && EP->canHandle(self(), DS)) {
    ExecutionPolicy.swap(EP);
    Success = true;
  } else {
    LOG_TRACE_STREAM << "Execution policy " << *EP
                     << " cannot handle DeluxeSensor " << FullName << std::endl;
  }
  ASSERT(inv());
  return Success;
}

Optional<AgentHandle> DeluxeSensor::master(void) const noexcept {
  ASSERT(inv());
  return Master;
}

void DeluxeSensor::registerMaster(const Optional<AgentHandle> _Master) noexcept {
  ASSERT(inv() && (!_Master || unwrapAgent(*_Master).Kind == atoms::AgentKind));

  Master = _Master;

  ASSERT(inv());
}

void DeluxeSensor::clearSimulationDataSource(void) noexcept {
  ASSERT(inv());
  SFP = nullptr;
  ASSERT(inv());
}

bool DeluxeSensor::simulationDataSourceIsSet(void) const noexcept {
  ASSERT(inv());
  return SFP != nullptr;
}

void DeluxeSensor::handleTrigger(atoms::Trigger) noexcept {
  ASSERT(inv());

  // Use \c rosa::deluxe::DeluxeSensor::SFP if set, otherwise
  // \c rosa::deluxe::DeluxeSensor::FP.
  const H &F = SFP ? SFP : FP;
  F();

  ASSERT(inv());
}

} // End namespace deluxe
} // End namespace rosa
