#ifndef BOX_H
#define BOX_H

#include "futile_cst.h"
#include "AtDomain"

namespace Atlab {

class Cell
{
public:

  F_DEFINE_TYPE(cell);
  operator f90_cell*() { return F_TYPE(mCell); };
  operator const f90_cell*() const { return F_TYPE(mCell); };
  operator f90_cell_pointer*() { return &mCell; };
  Cell(f90_cell_pointer other) : mCell(other) {};

  Cell(const Cell& other);

  Cell(const Domain& dom,
    const double habc[3][3],
    const double hgrids[3],
    size_t ndim,
    const int ndims[3],
    double volume_element);

  ~Cell(void);

  Cell(void);

  void box_nbox_from_cutoff(int out_nbox[2][3],
    const double oxyz[3],
    double cutoff,
    const bool (*inner) = nullptr) const;

  Cell(const Domain& dom,
    const int ndims[3],
    const double hgrids[3]);

  double r(int i,
    int dim) const;

private:
  f90_cell_pointer mCell;

};

class BoxIterator
{
public:

  F_DEFINE_TYPE(box_iterator);
  operator f90_box_iterator*() { return F_TYPE(mBoxIterator); };
  operator const f90_box_iterator*() const { return F_TYPE(mBoxIterator); };
  operator f90_box_iterator_pointer*() { return &mBoxIterator; };
  BoxIterator(f90_box_iterator_pointer other) : mBoxIterator(other) {};

  BoxIterator(const BoxIterator& other);

  BoxIterator(int i,
    int j,
    int k,
    const int (*i23) = nullptr,
    const int (*i3e) = nullptr,
    const int (*i3s) = nullptr,
    const int (*ind) = nullptr,
    const int (*inext)[3] = nullptr,
    Cell (*mesh) = nullptr,
    const int (*nbox)[2][3] = nullptr,
    const double (*oxyz)[3] = nullptr,
    const double (*rxyz)[3] = nullptr,
    const double (*rxyz_nbox)[3] = nullptr,
    const int (*subbox)[2][3] = nullptr,
    const double (*tmp)[3] = nullptr,
    const bool (*whole) = nullptr);

  ~BoxIterator(void);

  BoxIterator(void);

  BoxIterator(const Cell& mesh,
    const int (*nbox)[2][3] = nullptr,
    const double (*origin)[3] = nullptr,
    const int (*i3s) = nullptr,
    const int (*n3p) = nullptr,
    const bool (*centered) = nullptr,
    const double (*cutoff) = nullptr);

  bool box_next_z(void);

  bool box_next_y(void);

  bool box_next_x(void);

  bool box_next_point(void);

  double box_iter_square_gd(void) const;

  void box_iter_closest_r(double out_r[3],
    const double rxyz[3],
    const bool (*orthorhombic) = nullptr) const;

  double box_iter_distance(const double rxyz0[3]) const;

  void nullify_box_iterator(void);

  void box_iter_set_nbox(const int (*nbox)[2][3] = nullptr,
    const double (*oxyz)[3] = nullptr,
    const double (*cutoff) = nullptr);

  void box_iter_expand_nbox(void);

  void box_iter_rewind(void);

  void box_iter_split(int ntasks,
    int itask);

  void box_iter_merge(void);

private:
  f90_box_iterator_pointer mBoxIterator;

};


}
#endif
