/*  \file     test_viky.cpp
 *  \brief    mimic core test for device connection
 *            based on the static library, the test tries
 *            to connect to a viky device.
 *  \version  1.0.0
 *  \author   Silvan Murer
 *  \date     2022
 */

#include "Mimic.hpp"

#include <gtest/gtest.h>

#include <iostream>

using namespace mimic;

/* open test producer viky and check available devices */
TEST(ConnectionTest, DeviceDetection) {
	/* create mimic object, print version information and scan for available producers */
	Mimic& mime = Mimic::getInstance();
	EXPECT_EQ(mime.getVersion(), "0.0.1");
	uint32_t nofProducers = mime.update();
	EXPECT_EQ(nofProducers, 1);

	/* at least one should be viky, used for this test */
	Mimic_Producer producer;
	for (uint32_t i = 0; i < nofProducers; i++) {
		if (!mime.getProducerName(i).compare("viky")) {
			producer = mime.openProducer(i);
		}
	}

	std::string vikyLocation = producer.getLocation(); // incl. file name
	EXPECT_EQ(vikyLocation, "C:\\Tools\\viky\\viky.cti");
	std::string vikyVersion = producer.getVersion();
	EXPECT_EQ(vikyVersion, "3.2");
	std::string fileName = vikyLocation.substr(vikyLocation.size() - 8);
	ASSERT_EQ(fileName, "viky.cti");

	uint32_t nofDevices = producer.update(1800);
	EXPECT_EQ(nofDevices, 3);

	EXPECT_EQ(producer.getCameraName(0), "VikyTL_DEV_red");
	EXPECT_EQ(producer.getCameraName(1), "VikyTL_DEV_green");
	EXPECT_EQ(producer.getCameraName(2), "VikyTL_DEV_blue");
}

/* open a connection to the "VikyTL_DEV_red" device */
TEST(ConnectionTest, OpenDevice) {
	/* create mimic object, print version information and scan for available producers */
	Mimic& mime = Mimic::getInstance();
	uint32_t nofProducers = mime.update();

	/* at least one should be viky, used for this test */
	Mimic_Producer producer;
	for (uint32_t i = 0; i < nofProducers; i++) {
		if (!mime.getProducerName(i).compare("viky")) {
			producer = mime.openProducer(i);
		}
	}

	std::string vikyLocation = producer.getLocation(); // incl. file name
	std::string fileName = vikyLocation.substr(vikyLocation.size() - 8);
	ASSERT_EQ(fileName, "viky.cti");

	uint32_t nofDevices = producer.update(1800);
	std::string testDevice = "VikyTL_DEV_red";

	std::shared_ptr<Mimic_Camera> camera;
	for (int i = 0; i < nofDevices; i++) {
		if (!producer.getCameraName(i).compare(testDevice)) {
			camera = std::make_shared<Mimic_Camera>(producer.openCamera(i));
			break;
		}
	}
	ASSERT_NE(camera, nullptr);
	ASSERT_EQ(camera->getDeviceName(), testDevice);

	camera->release();
}
