//===-- rosa/agent/SystemStateDetector.hpp ----------------------*- C++ -*-===//
//
//                                 The RoSA Framework
//
//===----------------------------------------------------------------------===//
///
/// \file rosa/agent/SystemStateDetector.hpp
///
/// \author Maximilian Götzinger (maximilian.goetzinger@tuwien.ac.at)
///
/// \date 2019
///
/// \brief Definition of *system state detector* *functionality*.
///
//===----------------------------------------------------------------------===//

#ifndef ROSA_AGENT_SYSTEMSTATEDETECTOR_HPP
#define ROSA_AGENT_SYSTEMSTATEDETECTOR_HPP

#include "rosa/agent/Functionality.h"
#include "rosa/agent/StateDetector.hpp"
#include "rosa/agent/SystemState.hpp"

#include "rosa/support/debug.hpp"

namespace rosa {
namespace agent {

// todo: löschen , std::size_t NUMOFINPUTSIGNALS, std::size_t NUMOFOUTPUTSIGNALS
/// TODO: write description
template <typename INDATATYPE, typename CONFDATATYPE, typename PROCDATATYPE,
          HistoryPolicy HP>
class SystemStateDetector
    : public StateDetector<INDATATYPE, CONFDATATYPE, PROCDATATYPE, HP> {

  using StateDetector =
      StateDetector<INDATATYPE, CONFDATATYPE, PROCDATATYPE, HP>;
  using PartFuncPointer = typename StateDetector::PartFuncPointer;

private:
  // For the convinience to write a shorter data type name
  using SystemStatePtr =
      std::shared_ptr<SystemState<INDATATYPE, CONFDATATYPE, PROCDATATYPE>>;

  /// The CurrentSystemState is a pointer to the (saved) system state in which
  /// the actual state of the observed system is.
  SystemStatePtr CurrentSystemState;

  /// The DetectedSystemStates is a history in that all detected system states
  /// are saved.
  DynamicLengthHistory<SystemStatePtr, HP> DetectedSystemStates;

  /// The FuzzyFunctionDelayTimeToGetBroken is the fuzzy function that gives
  /// the confidence whether the system is Broken because of an input change
  /// without an output change or vice versa. A small time gap between the two
  /// shall be allowed.
  PartFuncPointer FuzzyFunctionDelayTimeToGetBroken;

  /// The FuzzyFunctionDelayTimeToBeWorking is the fuzzy function that gives
  /// the
  /// confidence whether the system is still OK allthough an input change
  /// without an output change or vice versa.
  PartFuncPointer FuzzyFunctionDelayTimeToBeWorking;

public:
  // todo zwei parameter für variablen anzahl
  /// TODO: write description
  SystemStateDetector(
      uint32_t MaximumNumberOfSystemStates,
      PartFuncPointer FuzzyFunctionDelayTimeToGetBroken,
      PartFuncPointer FuzzyFunctionDelayTimeToBeWorking) noexcept
      : CurrentSystemState(nullptr),
        DetectedSystemStates(MaximumNumberOfSystemStates),
        FuzzyFunctionDelayTimeToGetBroken(FuzzyFunctionDelayTimeToGetBroken),
        FuzzyFunctionDelayTimeToBeWorking(FuzzyFunctionDelayTimeToBeWorking) {
    //@Benedikt: if I write "NextStateID(1), StateHasChanged(false)" before the
    //{}-brackets, the compiler tells me: "SystemStateDetector.hpp:72:9: error:
    // member initializer 'NextStateID'/'StateHasChanged' does not name a
    // non-static data member or base class"
    this->NextStateID = 1;
    this->StateHasChanged = false;
  }

  /// Destroys \p this object.
  ~SystemStateDetector(void) = default;

  /// TODO: write description
  SystemStateInformation<CONFDATATYPE>
  detectSystemState(INDATATYPE Sample) noexcept {

    // dummy line
    Sample = 1;
  }

private:
  /// Creates a new signal state and adds it to the signal state vector in which
  /// all known states are saved.
  ///
  /// \return a pointer to the newly created signal state or NULL if no state
  /// could be created.
  SystemStatePtr createNewSignalState(void) noexcept {
    SystemStatePtr S(new SignalState<INDATATYPE, CONFDATATYPE, PROCDATATYPE>(
        this->NextStateID, this->NumberOfSignals));

    DetectedSystemStates.addEntry(S);

    return S;
  }
};

} // End namespace agent
} // End namespace rosa

#endif // ROSA_AGENT_SYSTEMSTATEDETECTOR_HPP
