#include <xavia/iSensor.h>
#include <xavia/logs.h>
#include <xavia/sensorFactory.h>
#include <xavia/sensorState.h>

#include <cstdlib>
#include <chrono>
#include <iostream>
#include <thread>

namespace xsdk = xavia::sdk;

/*  BASIC EXAMPLE
 * 
 * The following example is the most basic use case.
 * No detailed error handling, no data capture. Just connecting to a sensor, 
 * starting it, waiting for 5 seconds and stoping it again.
 * 
 * Once you understand this basic example, see the example with callback 
 * handling for handling more of the output formats of the sensor.
 */
int main()
{
  std::wcout << L"=== Starting Basic Example ===" << std::endl;

  xsdk::SetLogCallBack([](const xsdk::Severity& sev, const std::wstring& msg)
                       { std::wcout << xsdk::GetLogTypeString(sev) << L": " << msg << std::endl; });
  xsdk::SetLogSeverity(xsdk::Severity::XM_INFO);

  try
  {
    xsdk::SensorFactory factory;

    // these are optional, these values are the defaults.
    std::string ip = "10.10.100.11";
    int port = 4000;

    // build sensor
    auto sensor = factory.Build(ip, port);

    // run
    if(sensor->GetCurrentState()!= xsdk::SensorState::RUNNING)
    {
      sensor->Start();
    }
    std::this_thread::sleep_for(std::chrono::seconds(5));
    sensor->Stop();

  }
  catch(const std::exception& e)
  {
    std::wcerr << L"Error received: " << e.what() << std::endl;
    return EXIT_FAILURE;
  }

  std::wcout << L"=== Basic Example Completed ===" << std::endl;
  return EXIT_SUCCESS;
}
