from logging import Logger
from typing import Dict

from production.box import NonValidJitAddTagBox
from production.model import (
    TagNotFoundException,
    JitNotFoundException,
)
from production.treebuilder import BoxConfiguration

from ._BoxBuilder import BoxBuilder


class NonValidJitAddTagBoxBuilder(BoxBuilder):
    def __init__(
        self,           # type: NonValidJitAddTagBoxBuilder
        loggers,        # type: Dict[str, Logger]
    ):
        # type: (...) -> None

        super(NonValidJitAddTagBoxBuilder, self).__init__(loggers)

    def build(
        self,       # type: NonValidJitAddTagBoxBuilder
        config,     # type: BoxConfiguration
    ):
        # type: (...) -> NonValidJitAddTagBox

        if u"Tag" not in config.extra_config:
            raise TagNotFoundException("NonValidJitAddTagBoxBuilder without tag: {}".format(config.name))

        if u"ValidJitLines" not in config.extra_config:
            raise JitNotFoundException("NonValidJitAddTagBoxBuilder without ValidJitLines: {}".format(config.name))

        return NonValidJitAddTagBox(
            name=config.name,
            sons=config.sons,
            tag=self.get_extra(config, "Tag", None),
            valid_jits=self.get_extra(config, "ValidJitLines", []),
            logger=self.get_logger(config),
        )
