#include "UpperTesterLayer.hh"
#include "UpperTesterTypes.hh"

#include "loggers.hh"

UpperTesterLayer::UpperTesterLayer(const std::string & p_type, const std::string & param) : TLayer<LibItsGeoNetworking__TestSystem::UpperTesterPort>(p_type), _params(), _codec() {
  loggers::get_instance().log(">>> UpperTesterLayer::UpperTesterLayer: %s, %s", to_string().c_str(), param.c_str());
  // Setup parameters
  Params::convert(_params, param);
}

void UpperTesterLayer::sendMsg(const LibItsGeoNetworking__TypesAndValues::UtGnInitialize& send_par, Params& params){
  loggers::get_instance().log_msg(">>> UpperTesterLayer::sendMsg", send_par);

  // Encode UpperTester PDU
  OCTETSTRING data;
  _codec.encode((const Record_Type &)send_par, data);
  // Update parameters
  Params par(params); // FIXME Review all const Param& in method declarations
  sendData(data, params);
}

void UpperTesterLayer::sendMsg(const LibItsGeoNetworking__TypesAndValues::UtGnChangePosition& send_par, Params& params)
{
  loggers::get_instance().log_msg(">>> UpperTesterLayer::sendMsg", send_par);

  // Encode UpperTester PDU
  OCTETSTRING data;
  _codec.encode((const Record_Type &)send_par, data);
  // Update parameters
  Params par(params); // FIXME Review all const Param& in method declarations
  sendData(data, params);
}

void UpperTesterLayer::sendMsg(const LibItsGeoNetworking__TypesAndValues::UtGnTrigger& send_par, Params& params)
{
  loggers::get_instance().log_msg(">>> UpperTesterLayer::sendMsg", send_par);

  // Encode UpperTester PDU
  OCTETSTRING data;
  _codec.encode((const Record_Type &)send_par, data);
  // Update parameters
  Params par(params); // FIXME Review all const Param& in method declarations
  sendData(data, params);
}

void UpperTesterLayer::sendMsg(const LibItsGeoNetworking__TypesAndValues::UtAutoInteropTrigger& send_par, Params& params)
{
  loggers::get_instance().log_msg(">>> UpperTesterLayer::sendMsg", send_par);

  // Encode UpperTester PDU
  OCTETSTRING data;
  _codec.encode((const Record_Type &)send_par, data);
  // Update parameters
  Params par(params); // FIXME Review all const Param& in method declarations
  sendData(data, params);
}

void UpperTesterLayer::sendData(OCTETSTRING& data, Params& params) {
  loggers::get_instance().log_msg(">>> UpperTesterLayer::sendData: ", data);
  //params.log();
  sendToAllLayers(data, params);
}

void UpperTesterLayer::receiveData(OCTETSTRING& data, Params& params)
{
  loggers::get_instance().log_msg(">>> UpperTesterLayer::receiveData: ", data);
 
  if (data.lengthof() == 2) {
    LibItsGeoNetworking__TypesAndValues::UtGnResults p;
    _codec.decode(data, p);
    // Pass it to the ports if amy
    toAllUpperPorts((Base_Type&)p, params);
  } else {// Decode the payload
    LibItsGeoNetworking__TypesAndValues::UtGnEventInd p;
    _codec.decode(data, p);
    // Pass it to the ports if amy
    toAllUpperPorts(p, params);
  }
}

class UpperTesterFactory : public LayerFactory {
  static UpperTesterFactory _f;
public:
  UpperTesterFactory();
  virtual Layer * createLayer(const std::string &  type, const std::string &  param);
};

UpperTesterFactory::UpperTesterFactory(){
  // Register factory
  loggers::get_instance().log(">>> UpperTesterFactory::UpperTesterFactory");
  LayerStackBuilder::RegisterLayerFactory("UT", this);
}

Layer * UpperTesterFactory::createLayer(const std::string & p_type, const std::string & param){
  return new UpperTesterLayer(p_type, param);
}

UpperTesterFactory UpperTesterFactory::_f;
