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

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

namespace xsdk = xavia::sdk;

/*  LOGGING EXAMPLE
 *
 * This example demonstrates how to use the logging system to capture and handle different
 * levels of messages from the sensor.
 *
 * The example shows:
 * 1. Setting up a log callback to receive log messages
 * 2. Setting the log severity level to capture different message types
 * 3. Building and operating a sensor with various log messages
 * 4. Demonstrating how to handle different log levels
 *
 * This example shows how to effectively use the logging system to monitor sensor operations
 * and diagnose issues during runtime.
 */

int main()
{
  std::wcout << L"=== Starting Logging Example ===" << std::endl;

  // Note: we did not call SetLogOutputFolder, so the logs will be printed to the console.
  // so in this example, you should see all logs twice.

  // 1. Set up log callback to receive messages
  xsdk::SetLogCallBack([](const xsdk::Severity& sev, const std::wstring& msg)
                      {
                        // 4. Demonstrating how to handle different log levels
                        // either use "sev" in a switch case or use the helper function GetLogTypeString to print the severity as string
                        std::wcout << L"Log Received on client: " <<
                          xsdk::GetLogTypeString(sev) << L": " << msg << std::endl;
                      });

  // 2. Set log severity to capture levels error, warning and info
  xsdk::SetLogSeverity(xsdk::Severity::XM_INFO);

  try
  {
    // 3. Building and operating a sensor with various log messages
    xsdk::SensorFactory factory;
    auto sensor = factory.Build();

    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"=== Logging Example Completed ===" << std::endl;
  return EXIT_SUCCESS;
}
