/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2016 Raytrix GmbH. All rights reserved.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/************************************************************************/
/*  This example demonstrates:                                          */
/*      - Working with a camera.                                        */
/*      - Calculating refocus image                                     */
/*      - Displaying result                                             */
/************************************************************************/


// Example heavily reworked into RxCamera class

#include "RxCamera.h"

#include <lcm/lcm-cpp.hpp>


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// 	Main entry-point for this application.
/// </summary>
///
/// <param name="argc"> Number of command-line arguments. </param>
/// <param name="argv"> Array of command-line argument strings. </param>
///
/// <returns> Exit-code for the process - 0 for success, else an error code. </returns>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



int main(int argc, char* argv[])
{

	// Load args 
	if (argc != 4) {
		std::cout << "Missing input args: EyeRISCameraControl [exptime] [framerate] [recordrate]" << std::endl;
		return 1;
	}

	lcm::LCM lcm;
	exlcm::recorder_status recStat;

	char* endptr;
	float exposureTime = strtof(argv[1], &endptr);
	float frameRate = strtof(argv[2], &endptr);
	float recordRate = strtof(argv[3], &endptr);

	std::cout << "Exposure Time: " << exposureTime << std::endl;
	std::cout << "Frame Rate: " << frameRate << std::endl;
	std::cout << "Record Rate: " << recordRate << std::endl;

	std::cout << "creating and starting camera..." << std::endl;

	RxCamera cam(exposureTime,frameRate,recordRate,30,1);

	lcm.subscribe("RECSTAT", &RxCamera::recStatMsg, &cam);


	if (!cam.haveCamera()) {
		std::cout << "No camera found, check power to camera." << std::endl;
		return 0;
	}

	cam.start();

	bool bEnd = false;

	while (!bEnd) {

		if (_kbhit()) {
			switch (_getch()) {
			case 'w':
				if (cam.isRecording())
					cam.stopRecording();
				else
					cam.startRecording();
				break;
			case 'q':
				bEnd = true;
				break;
			case 'r':
				cam.setDisplayType(RxProcessor::RAW);
				break;
			case 't':
				cam.setDisplayType(RxProcessor::TOTAL_FOCUS);
				break;
			case 'd':
				cam.setDisplayType(RxProcessor::DEPTH_MAP);
				break;
			case 'f':
				cam.setDisplayType(RxProcessor::REFOCUS);
				break;
			}

		}
		//_sleep(100);
		lcm.handleTimeout(100);
	}

	if (cam.isRecording())
		cam.stopRecording();
	cam.stop();

	// The example is finished
	printf("finished.\n");

	return 0;
}
