#ifndef MPI_SIMULATION_HPP
#define MPI_SIMULATION_HPP

#include <cstddef>
#include <memory>
#include <mpi.h>
#include <vector>

#include "BarnesHutTree.hpp"
#include "Simulation.hpp"

namespace nbody {
	struct SendStore {
		std::vector<Body> bodies;
		MPI_Request request{ MPI_REQUEST_NULL };
		SendStore(std::vector<Body> b):bodies(std::move(b)) {};
	};

	//MPI simulation
	class MpiSimulation : public Simulation {
	protected:
		MPI_Datatype vec3Type{MPI_DATATYPE_NULL};
		MPI_Datatype bodyType{MPI_DATATYPE_NULL};
		MPI_Datatype boxType{MPI_DATATYPE_NULL};
		std::vector<Box> domains;
		Box overallDomain;
		std::vector<SendStore> sendStores;

		void flushSendStore();
		void send(std::vector<Body> bodies, int target);
		int recv(std::vector<Body>& bodies, int source);
	public:
		MpiSimulation(const std::string& inputFile);
		~MpiSimulation();
		std::size_t getNumberOfProcesses() const;
		std::size_t getProcessId() const;
		bool stateCorrect();
		void distributeBodies();
		void distributeDomains(const std::vector<Body>& localBodies);
		void distributeDomains(Box localDomain);
		void distributeDomains();
		void distributeLETs();
		void rebuildTree();
		void buildTree();
		void runStep();
		MPI_Datatype* getDatatype();
	};
} // namespace nbody

#endif

