#include "wheelNiceApp.hpp"

// wheelNiceApp::wheelNiceApp()
// {
// }

wheelNiceApp::wheelNiceApp(const std::string &svcPath)
{
  std::cout << "*** wheelNiceApp constructur ... ***" << std::endl;
  m_svcPath = svcPath;
  std::cout << m_svcPath << std::endl;
  std::cout << "*** wheelNiceApp constructur done. ***" << std::endl;
}

wheelNiceApp::~wheelNiceApp()
{
}


int wheelNiceApp::run(int /*argc*/, char* /*argv*/[])
{
  int status = 0;
  std::string unit = "STEPS";
  // std::string svcName1 = "DATA.WHEEL.SCI_WHEEL1.SVC";
  try
    {
      // this method allows to terminate the client using ctrl^C
      callbackOnInterrupt();
      
      D_LOG ( config());
      U1_LOG(config());
      U2_LOG("Wheel!");

      Basda::MoccaWheelServiceWorkerInterfacePtr wheelService;
      D_LOG("create");
      wheelService = Basda::MoccaWheelServiceWorkerInterface::create(config()->node(m_svcPath)->path());
      wheelService->moveToNamedPositionStart(2);
      return status;
    }

    catch ( const ::Nice::Exception & ex )
    {
        E_LOG ( ex );
        status = -1;
    }

    catch ( const IceUtil::Exception& ex )
    {
        E_LOG ( ex );
        status = -1;
    }

    catch ( const std::exception& ex )
    {
        E_LOG ( ex.what() );
        status = -1;
    }

    catch ( const char * ex )
    {
        E_LOG ( ex );
        status = -1;
    }

    catch ( ... )
    {
        E_LOG ( "Unknow Error" );
        status = -1;
    }

    return status;
 
}

int wheelNiceApp::runNamedPos (int namedPos)
{
  int status = 0;
  std::string unit = "STEPS";

  try
    {
      // this method allows to terminate the client using ctrl^C
      callbackOnInterrupt();
      
      D_LOG ( config());
      U1_LOG(config());
      I_LOG(m_svcPath + "moving!");

      Basda::MoccaWheelServiceWorkerInterfacePtr wheelService;
      D_LOG("create");
      wheelService = Basda::MoccaWheelServiceWorkerInterface::create(config()->node(m_svcPath)->path());
            
      MyPropertyBridgeMemberFunction* myCallback = new MyPropertyBridgeMemberFunction();
      Nice::PropertyPtr cmdProp = wheelService->config()->leaf("CMD.MOVETONAMEDPOSITION.EXEC");
      I_LOG(cmdProp->id());
      I_LOG(cmdProp->toStringFull());
      cmdProp->applyBridge(new Nice::PropertyBridgeMemFn <Nice::Completion, MyPropertyBridgeMemberFunction> (myCallback, &MyPropertyBridgeMemberFunction::execute), "moveToNamedPosition");

      wheelService->moveToNamedPositionStart(namedPos);
      I_LOG(cmdProp->toStringFull());
      return status;
    }

    catch ( const ::Nice::Exception & ex )
    {
        E_LOG ( ex );
        status = -1;
    }

    catch ( const IceUtil::Exception& ex )
    {
        E_LOG ( ex );
        status = -2;
    }

    catch ( const std::exception& ex )
    {
        E_LOG ( ex.what() );
        status = -3;
    }

    catch ( const char * ex )
    {
        E_LOG ( ex );
        status = -4;
    }

    catch ( ... )
    {
        E_LOG ( "Unknow Error" );
        status = -99;
    }
  
  return status;
    
}

void wheelNiceApp::interruptCallback (int _sig)
{
    N_LOG ( "Interrupt #" << _sig );
}

std::string wheelNiceApp::getSvcPath()
{
  return m_svcPath;
}

int wheelNiceApp::getPosition()
{
  int status = 0;
  Nice::Int position = -1;
  std::string unit = "STEPS";
  try
    {
      // this method allows to terminate the client using ctrl^C
      callbackOnInterrupt();
      
      D_LOG ( config());
      U1_LOG(config());
      U2_LOG(m_svcPath + "getting position!");

      Basda::MoccaWheelServiceWorkerInterfacePtr wheelService1;
      D_LOG("create");
      wheelService1 = Basda::MoccaWheelServiceWorkerInterface::create(config()->node(m_svcPath)->path());
      // position = wheelService->getPosition();
      return position;
    }

    catch ( const ::Nice::Exception & ex )
    {
        E_LOG ( ex );
        status = -1;
    }

    catch ( const IceUtil::Exception& ex )
    {
        E_LOG ( ex );
        status = -2;
    }

    catch ( const std::exception& ex )
    {
        E_LOG ( ex.what() );
        status = -3;
    }

    catch ( const char * ex )
    {
        E_LOG ( ex );
        status = -4;
    }

    catch ( ... )
    {
        E_LOG ( "Unknow Error" );
        status = -99;
    }
  
  return status;
    
}

