//-*-c++-*-
#ifndef _RemoteButtonEvent_h_
#define _RemoteButtonEvent_h_

#include <iostream>

#include "Behaviors/StateMachine.h"
#include "Shared/string_util.h"

#nodeclass RemoteButtonEvent : StateNode

  #shortnodeclass DisplayInstructions : StateNode : DoStart
    sndman->speak("Message the I-P address of the remote robot");
    std::cout << "Type 'msg <ip-address>'" << std::endl;

  #nodeclass SetupListener : StateNode : DoStartEvent
      const TextMsgEvent &textev = dynamic_cast<const TextMsgEvent&>(event);
      unsigned int x;
      int const result = sscanf(textev.getText().c_str(), "%u.%u.%u.%u", &x, &x, &x, &x);
      if ( result != 4 ) {
	std::cout << "Invalid IP address" << std::endl;
        postStateFailure();
        return;
      }
      int ip = string_util::stringToIntIP(textev.getText());
      erouter->addRemoteListener(this, ip, EventBase::buttonEGID);
      sndman->speak("Listening for remote events");
    #nodemethod processEvent
      std::cout << "Remote event: " << event.getDescription() << std::endl;
      erouter->postEvent(EventBase(EventBase::userEGID, event.getSourceID(), EventBase::activateETID));
  #endnodeclass

  #nodemethod setup
    #statemachine
      startnode: StateNode =N=> {instructions, listen}
      instructions: DisplayInstructions =TM=> SetupListener =F=> instructions
      listen: StateNode =E(userEGID)=> SoundNode($,"ping.wav") =N=> listen
    #endstatemachine

#endnodeclass

#endif
