/************************************************************/
/*    NAME: Thomas                                              */
/*    ORGN: MIT                                             */
/*    FILE: SimModem.cpp                                        */
/*    DATE:                                                 */
/************************************************************/

#include <iterator>
#include "MBUtils.h"
#include "ACTable.h"
#include "SimModem.h"
#include <math.h>

using namespace std;

//---------------------------------------------------------
// Constructor

SimModem::SimModem()
{
  nav_x = 0.0;
  nav_y = 0.0;
  nav_z = 0.0;
  range = 0.0;
}

//---------------------------------------------------------
// Procedure: OnNewMail

bool SimModem::OnNewMail(MOOSMSG_LIST &NewMail)
{
  AppCastingMOOSApp::OnNewMail(NewMail);

  MOOSMSG_LIST::iterator p;
  for(p=NewMail.begin(); p!=NewMail.end(); p++) {
    CMOOSMsg &msg = *p;
    string key    = msg.GetKey();

#if 0 // Keep these around just for template
    string comm  = msg.GetCommunity();
    double dval  = msg.GetDouble();
    string sval  = msg.GetString(); 
    string msrc  = msg.GetSource();
    double mtime = msg.GetTime();
    bool   mdbl  = msg.IsDouble();
    bool   mstr  = msg.IsString();
#endif

     if(key == "FOO") 
       cout << "great!";
     else if(key == "NAV_X"){
        nav_x=msg.GetDouble();
     }
     else if(key=="NAV_Y"){
        nav_y=msg.GetDouble();
     }
     else if(key =="NAV_Z"){
        nav_z=msg.GetDouble();
     }
     else{
        reportRunWarning("Unhandled Mail: " + key);
     }
   }
	
   return(true);
}

//---------------------------------------------------------
// Procedure: OnConnectToServer

bool SimModem::OnConnectToServer()
{
   registerVariables();
   return(true);
}

//---------------------------------------------------------
// Procedure: Iterate()
//            happens AppTick times per second

bool SimModem::Iterate()
{
  AppCastingMOOSApp::Iterate();
  // Do your thing here!
  range = sqrt(pow(nav_x-balise_x, 2)+pow(nav_y-balise_y, 2)+pow(nav_y-balise_y, 2));
  printf("range = %f\n", range);

  Notify("RANGE", range);
  AppCastingMOOSApp::PostReport();
  return(true);
}

//---------------------------------------------------------
// Procedure: OnStartUp()
//            happens before connection is open

bool SimModem::OnStartUp()
{
  AppCastingMOOSApp::OnStartUp();

  STRING_LIST sParams;
  m_MissionReader.EnableVerbatimQuoting(false);
  if(!m_MissionReader.GetConfiguration(GetAppName(), sParams))
    reportConfigWarning("No config block found for " + GetAppName());

  STRING_LIST::iterator p;
  for(p=sParams.begin(); p!=sParams.end(); p++) {
    string orig  = *p;
    string line  = *p;
    string param = toupper(biteStringX(line, '='));
    string value = line;

    if(param == "BALISE_X") {
      balise_x = atof(value.c_str());
    }
    else if(param == "BALISE_Y") {
      balise_y = atof(value.c_str());
    }
    else if(param == "BALISE_Z") {
      balise_z = atof(value.c_str());
    }
    else{
      reportUnhandledConfigWarning(orig);
    }

  }


  
  registerVariables();	
  return(true);
}

//---------------------------------------------------------
// Procedure: registerVariables

void SimModem::registerVariables()
{
  AppCastingMOOSApp::RegisterVariables();
  // Register("FOOBAR", 0);
  Register("NAV_X");
  Register("NAV_Y");
  Register("NAV_Z");
}


//------------------------------------------------------------
// Procedure: buildReport()

bool SimModem::buildReport() 
{
  m_msgs << "============================================ \n";
  m_msgs << "File:                                        \n";
  m_msgs << "============================================ \n";

  ACTable actab(4);
  actab << "Alpha | Bravo | Charlie | Delta";
  actab.addHeaderLines();
  actab << "one" << "two" << "three" << "four";
  m_msgs << actab.getFormattedString();

  return(true);
}




