import sympy


def offset_to_direction(discrete_hamiltonian, discrete_coordinates):
    """Translate hopping keys from offsets to directions.

    Parameters:
    -----------
    discrete_hamiltonian: dict
        Discretized hamiltonian, key should be an offset of a hopping and value
        corresponds to symbolic hopping.
    discrete_coordinates: set
        Set of discrete coordinates.

    Returns:
    --------
    discrete_hamiltonian: dict
        Discretized hamiltonian, key is a direction of a hopping and value
        corresponds to symbolic hopping.

    Note:
    -----
    Coordinates (x,y,z) in output stands for a position of a source of the
    hopping.
    """
    coordinates = sorted(list(discrete_coordinates))
    coordinates = [sympy.Symbol(s, commutative=False) for s in coordinates]
    a = sympy.Symbol('a')

    onsite_zeros = (0,)*len(discrete_coordinates)
    output = {onsite_zeros: discrete_hamiltonian.pop(onsite_zeros)}
    for offset, hopping in discrete_hamiltonian.items():
        direction = tuple(-c for c in offset)
        subs = {c: c + d*a for c, d in zip(coordinates, direction)}
        output[direction] = hopping.subs(subs)

    return output