import copy


def make_scene_config(scene_template,
                      stats_path,
                      labels_uri,
                      image_uris,
                      channel_order=None):
    """Create a scene config for making predictions from a template.

    Args:
        scene_template: rastervision.protos.scene_pb2.Scene
        stats_path: path to raster stats file
            (output of compute_raster_stats command)
        labels_uri: URI of labels file to write predictions to
        image_uris: list of URIS of image files to make predictions on

    Returns:
        (rastervision.protos.scene_pb2.Scene) copy of scene_template with
            the other arguments of this function injected into it
    """
    scene = copy.deepcopy(scene_template)
    scene.ClearField('ground_truth_label_store')
    label_store = scene.prediction_label_store
    if label_store.HasField('object_detection_geojson_file'):
        label_store.object_detection_geojson_file.uri = \
            labels_uri
    elif label_store.HasField('classification_geojson_file'):
        label_store.classification_geojson_file.uri = \
            labels_uri
    elif label_store.HasField('segmentation_raster_file'):
        label_store.segmentation_raster_file.sink = \
            labels_uri

    del scene.raster_source.geotiff_files.uris[:]
    scene.raster_source.geotiff_files.uris.extend(image_uris)
    scene.raster_source.raster_transformer.stats_uri = stats_path

    if channel_order is not None:
        del scene.raster_source.raster_transformer.channel_order[:]
        scene.raster_source.raster_transformer.channel_order.extend(
            channel_order)

    return scene