#include <mpi.h>
#include <vector>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include "MpiSimulation.hpp"
#include "MpiBodyComm.hpp"
#include "Body.hpp"

typedef struct _A {
	double a[3];
	double b[3];
	double leap[3];
	double c;
	double d[2];
} A;

using namespace nbody;
using namespace std;

vector<Body> dubinskiParse(string filename) {
	vector<Body> result;
	string line;
	ifstream infile(filename.c_str(), ifstream::in);
	double mass, px, py, pz;

	while (infile >> mass >> px >> py >> pz) {
		Body b(px, py, pz, mass);

		result.push_back(b);
	}
	infile.close();
	return result;
}

int main(int argc, char** argv) {
	Body b(1.0, 2.0, 3.0);
	vector<Body> bds;

	MPI::Init(argc, argv);
	MpiSimulation simulation(argc, argv);
	MpiBodyComm comm(simulation.getDatatype());

	if (simulation.getProcessId() == 0) {
		bds = dubinskiParse(string("data/tab1024.dat"));
		comm.sendUnblocking(1, bds);
	} else if (simulation.getProcessId() == 1) {
		comm.recvBlocking(MPI::ANY_SOURCE, bds);
		for (unsigned int i = 0; i < bds.size(); i++) {
			bds[i].print();
		}
		//cout << bds.size() << endl;
	}

	MPI::Finalize();
	return 0;

}
