#include <dv-processing/core/utils.hpp>
#include <dv-processing/io/camera/discovery.hpp>

#include "CLI/CLI.hpp"

#include <boost/nowide/iostream.hpp>

#include <iostream>

int main(int argc, char **argv) {
	CLI::App app{"Utility to find iniVation camera devices available in the system."};

	bool verbose = false;
	app.add_flag("--verbose,-v", verbose, "Print more information about devices");

	try {
		app.parse(argc, argv);
	}
	catch (const CLI::ParseError &e) {
		return app.exit(e);
	}

	const auto devices = dv::io::camera::discover();

	if (devices.empty()) {
		boost::nowide::cout << "No devices found." << std::endl;
		return EXIT_SUCCESS;
	}

	boost::nowide::cout << "Device discovery: found " << devices.size() << " devices." << std::endl;

	for (const auto &device : devices) {
		fmt::print("Detected device [{} {}]\n", device.cameraModel, device.serialNumber);

		if (!verbose) {
			continue;
		}

		fmt::print("- {}\n", device.cameraModel);
		fmt::print("\t- USB busNumber:devAddress: {}:{}\n", device.busNumber, device.devAddress);
		fmt::print("\t- USB Device Type: {}\n", device.deviceType);
		fmt::print("\t- Serial Number: {}\n", device.serialNumber);
		fmt::print("\t- Firmware Version: {}\n", device.firmwareVersion);
	}

	boost::nowide::cout << std::endl;

	return EXIT_SUCCESS;
}
