import numpy as np

from warp_md.analysis.fiximagedbonds import fiximagedbonds


class _DummySelection:
    def __init__(self, indices):
        self.indices = indices


class _DummySystem:
    def __init__(self, n_atoms):
        self._n_atoms = n_atoms

    def select(self, _mask):
        return _DummySelection(range(self._n_atoms))

    def select_indices(self, indices):
        return _DummySelection(indices)

    def n_atoms(self):
        return self._n_atoms


class _DummyTraj:
    def __init__(self, coords, box):
        self._coords = coords
        self._box = box
        self._used = False

    def read_chunk(self, _max_frames=128):
        if self._used:
            return None
        self._used = True
        return {"coords": self._coords, "box": self._box}


def test_fiximagedbonds_images_coords():
    coords = np.array([[[12.0, -1.0, 5.0], [0.0, 0.0, 0.0]]], dtype=np.float64)
    box = np.array([[10.0, 10.0, 10.0]], dtype=np.float64)
    traj = _DummyTraj(coords, box)
    system = _DummySystem(coords.shape[1])

    out = fiximagedbonds(traj, system, mask=[0])
    chunk = out.read_chunk()
    out_coords = chunk["coords"][0]
    np.testing.assert_allclose(out_coords[0], np.array([2.0, 9.0, 5.0], dtype=np.float32), atol=1e-5)
    np.testing.assert_allclose(out_coords[1], np.array([0.0, 0.0, 0.0], dtype=np.float32), atol=1e-5)
