/*
* This file is part of robotreality
*
* Copyright(c) sschulz <AT> techfak.uni-bielefeld.de
* http://opensource.cit-ec.de/projects/robotreality
*
* This file may be licensed under the terms of the
* GNU General Public License Version 3 (the ``GPL''),
* or (at your option) any later version.
*
* Software distributed under the License is distributed
* on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
* express or implied. See the GPL for the specific language
* governing rights and limitations.
*
* You should have received a copy of the GPL along with this
* program. If not, go to http://www.gnu.org/licenses/gpl.html
* or write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The development of this software was supported by the
* Excellence Cluster EXC 277 Cognitive Interaction Technology.
* The Excellence Cluster EXC 277 is a grant of the Deutsche
* Forschungsgemeinschaft (DFG) in the context of the German
* Excellence Initiative.
*
*/

#include "DataOutputCSV.h"
#define FORCE_ROTATE_180 1



//default is no angle output
DataOutputCSV::DataOutputCSV(ConfigOptions *c) : DataOutput(c){
	init(c);
}


void DataOutputCSV::init(ConfigOptions *c){
	printf("> init DataOutputCSV()\n");
	cfg = c;
	
	printf("> saving CSV data to '%s'\n", cfg->csv_output_filename.c_str());
	fh = fopen(cfg->csv_output_filename.c_str(), "w");
	if (!fh){
		printf("> failed to open output.\n");
		exit(1);
	}
}

void DataOutputCSV::reopen_file(){
	close_file();
	
	fh = fopen(cfg->csv_output_filename.c_str(), "w");
	if (!fh){
		printf("> failed to open output.\n");
		exit(1);
	}
}

void DataOutputCSV::close_file(){
	if (!fh){
		fclose(fh);
	}
	
}

DataOutputCSV::~DataOutputCSV(){
	close_file();
}

void DataOutputCSV::set_output_active(bool state){
	output_active = state;
	
	if (state){
		reopen_file();
	}else{
		close_file();
	}
}

void DataOutputCSV::process_new_data(int f){
	printf("> writing CSV frame %6d\n",f);
	
	fprintf(fh, "%d, ", file_frame_id);
	file_frame_id++;
	
	fprintf(fh, "%f, ", target_angle[EYES_TILT]);
	fprintf(fh, "%f, ", target_angle[EYE_LEFT_BROW]);
	fprintf(fh, "%f, ", target_angle[EYE_LEFT_LID_LOWER]);
	fprintf(fh, "%f, ", target_angle[EYE_LEFT_LID_UPPER]);
	fprintf(fh, "%f, ", target_angle[EYE_LEFT_PAN]);
	
	fprintf(fh, "%f, ", target_angle[EYE_RIGHT_BROW]);
	fprintf(fh, "%f, ", target_angle[EYE_RIGHT_LID_LOWER]);
	fprintf(fh, "%f, ", target_angle[EYE_RIGHT_LID_UPPER]);
	fprintf(fh, "%f, ", target_angle[EYE_RIGHT_PAN]);
	
	//mouth
	fprintf(fh, "%f, %f, ",target_angle[MOUTH_CENTER_LOWER],target_angle[MOUTH_CENTER_UPPER]);
	fprintf(fh, "%f, %f, ",target_angle[MOUTH_LEFT_LOWER],target_angle[MOUTH_LEFT_UPPER]);
	fprintf(fh, "%f, %f, ",target_angle[MOUTH_RIGHT_LOWER],target_angle[MOUTH_RIGHT_UPPER]);
	
	//head
	fprintf(fh, "%f, ", target_angle[NECK_PAN]);
	fprintf(fh, "%f, ", target_angle[NECK_ROLL]);
	fprintf(fh, "%f\n ", target_angle[NECK_TILT]);

}

void DataOutputCSV::add_input_gui(){
	//nothing to add
}


void DataOutputCSV::dump_dataset(double x, double y){
#if FORCE_ROTATE_180
	//for old capture files -> rotate 180°!
	x = 640.0 - x;
	y = 480.0 - y;
#endif
	fprintf(fh, "%f, %f, ",x,y);
}