/*  \file     test_viky.cpp
 *  \brief    mimic core test for data acquisition
 *            based on the static library
 *  \version  1.0.0
 *  \author   Silvan Murer
 *  \date     2022
 */

#include "Mimic.hpp"

#include <gtest/gtest.h>

#include <algorithm>
#include <iostream>

using namespace mimic;

/*
 * Configure the test producer and acquire a single image
 */
TEST(AcquisitionTest, SingleImage) {
	/* 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 (uint32_t 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);

	EXPECT_EQ(camera->nodeMap.getValue<int64_t>("TLParamsLocked"), 0);
	camera->nodeMap.setValue<int64_t>("Width", 200);
	camera->nodeMap.setValue<int64_t>("Height", 100);

	camera->nodeMap.setValue<std::string>("ComponentSelector", "Intensity");
	camera->nodeMap.setValue<std::string>("PixelFormat", "Mono8");
	EXPECT_EQ(camera->nodeMap.getValue<bool>("ComponentEnable"), true);
	EXPECT_EQ(camera->nodeMap.getValue<std::string>("PixelFormat"), "Mono8");
	EXPECT_EQ(camera->nodeMap.getValue<int64_t>("ComponentIDValue"), 1);

	camera->nodeMap.setValue<std::string>("ComponentSelector", "Range");
	camera->nodeMap.setValue<bool>("ComponentEnable", false);
	EXPECT_EQ(camera->nodeMap.getValue<bool>("ComponentEnable"), false);
	EXPECT_EQ(camera->nodeMap.getValue<std::string>("PixelFormat"), "Coord3D_ABC32f_Planar");
	EXPECT_EQ(camera->nodeMap.getValue<int64_t>("ComponentIDValue"), 4);

	camera->nodeMap.setValue<bool>("ChunkModeActive", false);
	EXPECT_EQ(camera->nodeMap.getValue<bool>("ChunkModeActive"), false);

	EXPECT_EQ(camera->nodeMap.getValue<std::string>("GenDCStreamingMode"), "Off");
	EXPECT_EQ(camera->nodeMap.getValue<int64_t>("PayloadSize"), 10240080);

	camera->startAcquisition();
	EXPECT_EQ(camera->nodeMap.getValue<int64_t>("TLParamsLocked"), 1);

	auto buffer = camera->getBuffer();
	EXPECT_EQ(buffer.getNumParts(), 1);
	EXPECT_EQ(buffer.getPartWidth(0), 200);
	EXPECT_EQ(buffer.getPartHeight(0), 100);
	
	EXPECT_EQ(buffer.getPartPixelformat(0), 0x01080001);
	EXPECT_EQ(buffer.getPartPixelformatName(0), "Mono8");

	auto image = buffer.getPartData<uint8_t>(0);
	EXPECT_EQ(20000, image.size());

	buffer.release();

	camera->stopAcquisition();

	camera->release();
}

