/**
 * @file example.cpp
 * @brief System Example (Enter Boot Mode) with Binho Supernova/Pulsar (Blocking C++ API)
 *
 * This example demonstrates basic system-level control with CosmicSDK C++ API.
 *
 * Workflow:
 * 1. Connect to the adapter and print host device information.
 * 2. Issue the system command to enter boot mode.
 * 3. Clean up SDK resources.
 */

#include <cstdint>
#include <iostream>
#include <memory>

#include "CosmicSDK.hpp"

static bool checkResult(uint16_t code, const char* operation) {
  if (code != SUCCESS) {
    std::cerr << operation << " failed. ERROR CODE: " << code << std::endl;
    return false;
  }
  return true;
}

int main() {
  bool boot_mode_requested = false;
  auto device = std::make_unique<CosmicSDK>(2000);

  if (!device->connect()) {
    std::cerr << "Failed to connect to a Binho device" << std::endl;
    return 1;
  }

  // Step 1: Connect to the adapter and print host device information.
  CosmicSDK::DeviceInfo deviceInformation;
  if (!checkResult(device->getDeviceInfo(deviceInformation), "getDeviceInfo")) {
    device->disconnect();
    return 1;
  }

  std::cout << "Manufacturer: " << deviceInformation.manufacturer << std::endl;
  std::cout << "Product name: " << deviceInformation.productName << std::endl;
  std::cout << "Serial number: " << deviceInformation.serialNumber << std::endl;
  std::cout << "FW version: " << deviceInformation.fwVersion << std::endl;
  std::cout << "HW version: " << deviceInformation.hwVersion << std::endl;

  // Step 2: Issue the system command to enter boot mode.
  if (!checkResult(device->enterBootMode(), "enterBootMode")) {
    device->disconnect();
    return 1;
  }
  boot_mode_requested = true;
  std::cout << "Boot mode command sent successfully" << std::endl;

  // Step 3: Clean up SDK resources.
  if (!boot_mode_requested) {
    device->disconnect();
  }
  return 0;
}
