import iso8601
from _StatisticBox import StatisticBox
from production.model import ProductionOrder
from typing import List, Any


class MeanTimeStatisticBox(StatisticBox):
    def __init__(self, name, sons, publish_scheduler, logger, get_config):
        super(MeanTimeStatisticBox, self).__init__(
            name,
            sons,
            publish_scheduler,
            logger,
            get_config
        )

        self.statistics = {}

    def get_order_value(self, order):
        # type: (ProductionOrder) -> Any

        start_time = iso8601.parse_date(order.tags[self.start_tag])
        end_time = iso8601.parse_date(order.tags[self.end_tag])
        return (end_time - start_time).total_seconds()

    def update_statistics(self, values):
        # type: (List) -> float

        return sum(values) / (len(values) or 1)
