# -*- coding: utf-8 -*-

import os
import traceback

from ge.bpmc.business.image.processing import IDIProcessor
from ge.bpmc.business.translator import to_processing_data, to_science_image
from ge.bpmc.exceptions.processing import (BPMProcessingException,
                                           BPMProcessingInvalidException)


def process_image(logger, metadata):
    """
    Runs the image processing algorithm

    :logger: a logging.Logger instance
    :metadata: a dict based on the ge.bpmc.api.schemas.bpm.ModalityImageModel

    returns a tuple of 3 items:
    :criteria: a dict based on the ge.bpmc.api.schemas.bpm.CriteriaModel
    :overlay: a dict based on the ge.bpmc.api.schemas.bpm.OverlayModel
    :image_bytes: similar to bytes_, a list of lists containing images bytes
    generated by the processing algorithm
    """
    try:
        processing_mixin = to_science_image(metadata)
        image = IDIProcessor(logger, processing_mixin)
        image.image_processing()
        return to_processing_data(image)
    except BPMProcessingInvalidException as err:
        raise err
    except Exception as err:
        raise BPMProcessingException(traceback.format_exc())
