/* $Header: https://svn.mpia.de/gulli/TwiceAsNice/branch/unstable/ln/laos-drot/test/Fixture.h 14191 2017-03-01 19:21:03Z mathar $
 */

#include <Nice/Application.h>
#include <Basda/DeviceFactory.h>
#include <Sharknir/Drot/SharknirDrotServiceWorkerInterface.h>
#include <Sharknir/Drot/PropertyName.h>

#include <SharknirDrotServiceWorkerMock.h>
//#include <Basda/Mocca/MPIA/NDeviceDummyMock.h>

namespace Basda
{
  class NiceInfrastructure :
    public virtual Nice::Application,
    public Nice::Shared
  {
  public:
    NiceInfrastructure()
    {
    }

    ~NiceInfrastructure()
    {
      end(0);
    }

    int run(int /* argc */, char** /* argv */)
    {
      return 42;
    }
  };
}

typedef IceUtil::Handle<Basda::NiceInfrastructure> NiceInfrastructurePtr;

static NiceInfrastructurePtr s_app;

static int startSingletons(const char* _serverFile)
{
  U1_LOG("startSigletons");
  std::string s = std::string("--CONFIG=") + _serverFile;
  const char *c = s.c_str();
  const char *fakeArguments[] =
    { "programName", c, 0 };

  int argc = 2;

  s_app = new Basda::NiceInfrastructure();
  int status = s_app->init(argc, const_cast<char **>(fakeArguments));
  assert(status == EXIT_SUCCESS);

  // log level is set in the unit tests
  // this is for the service
  LOG_LEVEL(Nice::LERROR);

  //    LOG_LEVEL(Nice::LWARNING);
      LOG_LEVEL(Nice::LUSER9);
  //    LOG_LEVEL(Nice::LDEBUG);

  std::string serverName = "track_ctrl"; //"laos.dx.gws.drot.dummy-svr";
  Nice::Endpoint adapter(
			 Nice::Endpoint::fromString(
						    "[NAME=" + serverName
						    + ",TCP=localhost:10215,TIMEOUT=10]"));
  s_app->config()->setAtNode<Nice::Endpoint>("ADAPTER", adapter);

  {
    Nice::MapStringString load;
    load.insert(std::make_pair("NAME", serverName));

    load.insert(std::make_pair("LIB", "SharknirDrotServiceMock"));

    s_app->config("TRACK_CTRL.DROT.SVC")->setAtNode<Nice::MapStringString>("LOAD", load);
  }

  {
    Nice::MapStringString load;
    load.insert(std::make_pair("NAME", "DROT"));
#ifdef NOREALTIME
    load.insert(std::make_pair("LIB", "BasdaMoccaMpiaNDeviceDummyMock_NoRealtime"));
#else
    load.insert(std::make_pair("LIB", "BasdaMoccaMpiaNDeviceDummyMock_Realtime"));
#endif
    s_app->config("TRACK_CTRL.DROT.DEV.1")->setAtNode<Nice::MapStringString>("LOAD", load);
    s_app->config("TRACK_CTRL.DROT.SVC")->setAtNode<std::string>("DEVICE", "DROT");
  }
  //    s_app->config()->setAtNode<std::string>("LOGGER.LEVEL", "ERROR");
  s_app->config()->setAtNode<std::string>("LOGGER.LEVEL", "DEBUG");

  //    I_LOG("Load all devices");
  Basda::Basdard::loadAllDevices(s_app->config());
  //    I_LOG("Load all services");
  Basda::Basdard::loadAllServices(s_app->config());
  //    I_LOG("Start all services");
  Basda::Basdard::startAllWorker();

  U1_LOG("end startSigletons")
  return status;
}

#undef LOG_NAME
#define LOG_NAME Fixture::s_name

struct Fixture
{
  static std::string s_name;
  //        Basda::MoConNDeviceDummyMockPtr m_nDeviceDummy;
  Sharknir::DrotServiceWorkerMockPtr  m_worker;
  Sharknir::DrotServiceWorkerInterfacePtr m_drot;

  Fixture()
  {
    assert(s_app != 0);
    U1_LOG("Fixture");
    std::string deviceName = s_app->config("TRACK_CTRL.DROT.SVC")->getAtNode<std::string>("DEVICE");
    //            m_nDeviceDummy = Basda::Basdard::device<Basda::MoConNDeviceDummyMock>(deviceName);

    std::string serviceName = s_app->config("TRACK_CTRL.DROT.SVC")->getAtNode<std::map<std::string, std::string> >("LOAD")["NAME"];
    U1_LOG("Creating DrotServiceWorkerMock..")
    m_worker = Basda::Basdard::worker<Sharknir::DrotServiceWorkerMock>(serviceName);
    U1_LOG("Created DrotServiceWorkerMock")
  }

  virtual ~Fixture()
  {
  }
};

std::string Fixture::s_name ( "Basdard" );
