/* LICENSE:
  =========================================================================
    CMPack'04 Source Code Release for OPEN-R SDK 1.1.5-r2 for ERS7
    Copyright (C) 2004 Multirobot Lab [Project Head: Manuela Veloso]
    School of Computer Science, Carnegie Mellon University
    All rights reserved.
  ========================================================================= */

#ifndef INCLUDED_SystemEvent_h
#define INCLUDED_SystemEvent_h

#include <string>
#include <vector>

namespace Motion {
  class MotionCommand;
  class MotionLocalizationUpdateQueue;
};

#include "../Behaviors/Behavior.h"
#include "../Motion/MotionInterface.h"
#include "Events.h"

class BehaviorManager : public EventProcessor {
private:
  struct BehaviorInfo {
    IndependentBehavior *beh;
  };

  std::vector<BehaviorInfo> behaviors;
  uint my_id;

  Motion::MotionCommand command;

  void addBehavior(const std::string &name);

public:
  static std::string name;

  static EventProcessor *create() {return new BehaviorManager;}
  BehaviorManager();
  virtual ~BehaviorManager();

  virtual bool initConnections();
  virtual bool update(ulong time,const EventList *events);

  virtual const Motion::MotionCommand *get(ulong time);
};

class CameraFrameSource : public EventProcessor {
public:
  struct CameraFrameInfo {
    uchar *bytes_y,*bytes_u,*bytes_v;
    int width,height,skip;
  };
private:

  CameraFrameInfo info;

public:
  static std::string name;

  void setFrameInfo(uchar *bytes_y,uchar *bytes_u,uchar *bytes_v,int width,int height,int skip);

  static EventProcessor *create() {return new CameraFrameSource;}
  CameraFrameSource() {};
  virtual ~CameraFrameSource() {};

  virtual bool update(ulong time,const EventList *events);
  const CameraFrameInfo *get(ulong time) const;
};

class MotionUpdateSource : public EventProcessor {
private:
  Motion::MotionLocalizationUpdateQueue *queue;
  ulong last_timestamp;
  int last_idx;

public:
  static std::string name;

  static EventProcessor *create() {return new MotionUpdateSource;}
  MotionUpdateSource();
  virtual ~MotionUpdateSource();

  virtual bool update(ulong time,const EventList *events);

  Motion::MotionLocalizationUpdateQueue *get(ulong time);
};

class MotionCommandSink : public EventProcessor {
private:
  BehaviorManager *beh_mgr;

public:
  static std::string name;

  static EventProcessor *create() {return new MotionCommandSink;}
  MotionCommandSink();
  virtual ~MotionCommandSink();

  virtual bool initConnections();
  virtual bool update(ulong time,const EventList *events);
};

#endif
