#ifndef _DISPATCH_H_
#define _DISPATCH_H_

//#include "envelope.h"
//#include "../elementary/elementary.h"
//#include "../game_of_life/gol.h"
#include "../lattice_boltzmann/lbm.h"
#include "chunk_info.h"

#define NB_SIDES 6
#define NB_CHUNKS 9

#define INDEX_SIDE_BACK 0
#define INDEX_SIDE_BOTTOM 1
#define INDEX_SIDE_FRONT 2
#define INDEX_SIDE_LEFT 3
#define INDEX_SIDE_RIGHT 4
#define INDEX_SIDE_TOP 5

#define INDEX_CHUNK_NORTH_WEST 0
#define INDEX_CHUNK_WEST 1
#define INDEX_CHUNK_SOUTH_WEST 2
#define INDEX_CHUNK_SOUTH 3
#define INDEX_CHUNK_SOUTH_EAST 4
#define INDEX_CHUNK_EAST 5
#define INDEX_CHUNK_NORTH_EAST 6
#define INDEX_CHUNK_NORTH 7
#define INDEX_CHUNK_SURFACE 8

struct dispatch_context;

typedef struct side_envelope {
    chunk_info_t chunks[NB_CHUNKS];
    chunk_info_t *north_west;
    chunk_info_t *north;
    chunk_info_t *north_east;
    chunk_info_t *east;
    chunk_info_t *south;
    chunk_info_t *south_east;
    chunk_info_t *west;
    chunk_info_t *south_west;
    chunk_info_t *surface;
} side_envelope_t;

typedef struct envelope {
    side_envelope_t sides[NB_SIDES];
    side_envelope_t *back;
    side_envelope_t *bottom;
    side_envelope_t *front;
    side_envelope_t *left;
    side_envelope_t *right;
    side_envelope_t *top;
} envelope_t;

extern MPI_Datatype create_type(int count, int *block_lengths, MPI_Aint *displacements, MPI_Datatype *types);

extern struct dispatch_context *dispatch_context_new(const int *dimensions, MPI_Datatype datatype, int n_dimensions);

extern void dispatch_context_print(struct dispatch_context *dc);

extern envelope_t *get_inner_envelope(struct dispatch_context *dc, struct futhark_context *fc, int thickness);

extern envelope_t *get_outer_envelope(struct dispatch_context *dc, struct futhark_context *fc, int thickness);

extern chunk_info_t get_chunk_info(struct dispatch_context *dc);

extern void *get_chunk_with_envelope(struct dispatch_context *dc, struct futhark_context *fc, int thickness,
        void *f(struct futhark_context*, const void *, ...));

extern void *get_data(struct dispatch_context *dc);


extern void dispatch_context_free(struct dispatch_context *dc);

extern void envelope_free(envelope_t *envelope);

#endif //_DISPATCH_H_