from production.box import ReprocessOrdersBox
from production.model import ReprocessTimeNotFoundException
from production.treebuilder import BoxConfiguration
from production.treebuilder.boxfactory.builder._BoxBuilder import BoxBuilder


class ReprocessOrdersBoxBuilder(BoxBuilder):
    def __init__(self, publish_scheduler, loggers):
        super(ReprocessOrdersBoxBuilder, self).__init__(loggers)
        self.publish_scheduler = publish_scheduler
        self.loggers = loggers

    def build(self, config):
        # type: (BoxConfiguration) -> ReprocessOrdersBox

        reprocess_time = self.get_extra(config, "ReprocessTimeMinutes", None)
        reprocess_display_time = self.get_extra(config, "ReprocessDisplayTimeMinutes", None)

        if reprocess_time is None and reprocess_display_time is None:
            raise ReprocessTimeNotFoundException("ReprocessOrdersBoxBuilder without any time in configuration")

        reprocess_time_secs = int(reprocess_time) * 60 if reprocess_time is not None else None
        reprocess_display_time_sec = int(reprocess_display_time) * 60 if reprocess_display_time is not None else None
        logger = self.get_logger(config)

        return ReprocessOrdersBox(
            config.name,
            config.sons,
            self.publish_scheduler,
            reprocess_time_secs,
            reprocess_display_time_sec,
            logger
        )
