import copy


def convert_sequence_to_motor_units(cycles, unit_converter):
    """ Converts a move sequence to motor units.

    Converts a move sequence to motor units using the provied converter.

    Parameters
    ----------
    cycles : iterable of dicts
        The iterable of cycles of motion to do one after another. See
        ``compile_sequence`` for format.
    unit_converter : UnitConverter, optional
        ``GeminiMotorDrive.utilities.UnitConverter`` to use to convert
        the units in `cycles` to motor units.

    Returns
    -------
    motor_cycles : list of dicts
        A deep copy of `cycles` with all units converted to motor units.

    See Also
    --------
    compile_sequence
    GeminiMotorDrive.utilities.UnitConverter

    """
    # Make a deep copy of cycles so that the conversions don't damage
    # the original one.
    cv_cycles = copy.deepcopy(cycles)

    # Go through each cycle and do the conversions.
    for cycle in cv_cycles:
        # Go through each of the moves and do the conversions.
        for move in cycle['moves']:
            move['A'] = unit_converter.to_motor_velocity_acceleration( \
                move['A'])
            move['AD'] = \
                unit_converter.to_motor_velocity_acceleration( \
                move['AD'])
            move['V'] = unit_converter.to_motor_velocity_acceleration( \
                move['V'])
            move['D'] = int(unit_converter.to_motor_distance(move['D']))

    # Now return the converted move sequence.
    return cv_cycles