#include"TrackManager.h"
#include<iostream>
#include<fstream>
#include"io.h"

using namespace std;
typedef std::complex<double> Creal;

TrackManager::TrackManager() {

}
TrackManager::~TrackManager() {

}

void TrackManager::init(string path, string filename) {
	active = 1;
	string track = path;
	track.append(filename);
	ofs.open(track, ios::out | ios::binary);
}

void TrackManager::add_variable_int(string name, int& value) {
	var_int[name] = &value;
}
void TrackManager::add_variable_double(string name, double& value) {
	var_double[name] = &value;
}
//void TrackManager::add_variable_creal(string name, Creal& value) {
//	var_creal[name] = value;
//}

void TrackManager::write_begin(int number_time_steps) {
	if (active == 1) {

		saveHeader(ofs, "1.0");

		int size = var_int.size() + var_double.size();
		saveInt(ofs, size);
		saveInt(ofs, number_time_steps);
		for (auto& var : var_int) {
			saveString(ofs, var.first);
		}
		for (auto& var : var_double) {
			saveString(ofs, var.first);
		}

	}
}

void TrackManager::write() {
	if(active == 1){
		for (auto& var : var_int) {
			double value = (double)*(var.second);
			saveDouble(ofs, value);
			//ofs.write((char*)&value, sizeof(value));
		}
		for (auto& var : var_double) {
			double value = (double)*(var.second);
			saveDouble(ofs, value);
			//ofs.write((char*)&value, sizeof(value));
		}
	}
}

void TrackManager::close() {
	if(active == 1) ofs.close();
}