# Path: lib/exception.py
# class MissingException(Exception):
# 
#     def __init__(self, message):
#         # Call the base class constructor with the parameters it needs
#         super(MissingException, self).__init__(message)
#         self.returncode = 4
# 
#         logg = Logger('MissingException')
#         logg.error(message)
# 
# Path: lib/sitkaAPI.py
# def latestMetricInstance(metricInstanceList):
#     """
#     get the single latest metric instance
#     :param metricInstanceList:
#     :return:
#     """
#     result = latestMetricInstances(metricInstanceList, True)
#     if result is not None and len(result) > 0:
#         return result[0]
#     else:
#         return None
# 
# Path: lib/exception.py
# class DataException(Exception):
# 
#     def __init__(self, message):
#         # Call the base class constructor with the parameters it needs
#         super(DataException, self).__init__(message)
#         self.returncode = 2
#         logg = Logger('DataException')
#         logg.error(message)
# 
# class MissingException(Exception):
# 
#     def __init__(self, message):
#         # Call the base class constructor with the parameters it needs
#         super(MissingException, self).__init__(message)
#         self.returncode = 4
# 
#         logg = Logger('MissingException')
#         logg.error(message)
# 
# Path: lib/metrics.py
# class CHaMPMetric(object):
# 
#     # This is the dictionary representation of the object
#     # in its null state.
#     TEMPLATE = {}
# 
#     def __init__(self, *args, **kwargs):
#         # First initialize our logger and our empty metrics
#         self.log = Logger(self.__module__)
#         self.metrics = deepcopy(self.TEMPLATE)
# 
#         # Now try a calculation
#         try:
#             self.calc(*args, **kwargs)
#         except (DataException, MissingException), e:
#             self.log.warning("Could not complete metric calculation")
#             self.metrics = deepcopy(self.TEMPLATE)
# 
#     def calc(self, *args, **kwargs):
#         """
#         Needs to be overloaded in the children
#         :return:
#         """
#         raise Exception("YOU MUST IMPLEMENT THIS")


from lib.exception import MissingException
from lib.sitkaAPI import latestMetricInstance
from lib.exception import DataException, MissingException
from lib.metrics import CHaMPMetric

class UndercutMetrics(CHaMPMetric):

    TEMPLATE = {
        'VisitMetrics': {
            'Length' : 0.0,
            'LengthPercent' : 0.0,
            'Area' :  0.0,
            'AreaPerecent' : 0.0
        }
    }

    def calc(self, apiData):
        """
        Calculate undercut metrics
        :param apiData: dictionary of API data. Key is API call name. Value is API data
        :return: metrics dictionary
        """

        self.log.info("Running UndercutMetrics")

        if 'UndercutBanks' not in apiData:
            raise MissingException("UndercutBanks missing from apiData")

        # Retrieve the undercut API data
        undercutVals = [val['value'] for val in apiData['UndercutBanks']['values'] ]

        # Retrieve the latest topo metrics
        metricInstance = latestMetricInstance(apiData['TopoVisitMetrics'])
        if metricInstance is None:

# Path: lib/exception.py
# class MissingException(Exception):
# 
#     def __init__(self, message):
#         # Call the base class constructor with the parameters it needs
#         super(MissingException, self).__init__(message)
#         self.returncode = 4
# 
#         logg = Logger('MissingException')
#         logg.error(message)
# 
# Path: lib/sitkaAPI.py
# def latestMetricInstance(metricInstanceList):
#     """
#     get the single latest metric instance
#     :param metricInstanceList:
#     :return:
#     """
#     result = latestMetricInstances(metricInstanceList, True)
#     if result is not None and len(result) > 0:
#         return result[0]
#     else:
#         return None
# 
# Path: lib/exception.py
# class DataException(Exception):
# 
#     def __init__(self, message):
#         # Call the base class constructor with the parameters it needs
#         super(DataException, self).__init__(message)
#         self.returncode = 2
#         logg = Logger('DataException')
#         logg.error(message)
# 
# class MissingException(Exception):
# 
#     def __init__(self, message):
#         # Call the base class constructor with the parameters it needs
#         super(MissingException, self).__init__(message)
#         self.returncode = 4
# 
#         logg = Logger('MissingException')
#         logg.error(message)
# 
# Path: lib/metrics.py
# class CHaMPMetric(object):
# 
#     # This is the dictionary representation of the object
#     # in its null state.
#     TEMPLATE = {}
# 
#     def __init__(self, *args, **kwargs):
#         # First initialize our logger and our empty metrics
#         self.log = Logger(self.__module__)
#         self.metrics = deepcopy(self.TEMPLATE)
# 
#         # Now try a calculation
#         try:
#             self.calc(*args, **kwargs)
#         except (DataException, MissingException), e:
#             self.log.warning("Could not complete metric calculation")
#             self.metrics = deepcopy(self.TEMPLATE)
# 
#     def calc(self, *args, **kwargs):
#         """
#         Needs to be overloaded in the children
#         :return:
#         """
#         raise Exception("YOU MUST IMPLEMENT THIS")


# Path: tools/topoauxmetrics/methods/undercut.py
from lib.exception import MissingException
from lib.sitkaAPI import latestMetricInstance
from lib.exception import DataException, MissingException
from lib.metrics import CHaMPMetric


class UndercutMetrics(CHaMPMetric):

    TEMPLATE = {
        'VisitMetrics': {
            'Length' : 0.0,
            'LengthPercent' : 0.0,
            'Area' :  0.0,
            'AreaPerecent' : 0.0
        }
    }

    def calc(self, apiData):
        """
        Calculate undercut metrics
        :param apiData: dictionary of API data. Key is API call name. Value is API data
        :return: metrics dictionary
        """

        self.log.info("Running UndercutMetrics")

        if 'UndercutBanks' not in apiData:
            raise MissingException("UndercutBanks missing from apiData")

        # Retrieve the undercut API data
        undercutVals = [val['value'] for val in apiData['UndercutBanks']['values'] ]

        # Retrieve the latest topo metrics
        metricInstance = latestMetricInstance(apiData['TopoVisitMetrics'])
        if metricInstance is None:

            raise MissingException('Missing topo visit metric instance')