from production.model import ViewNameFoundException
from production.treebuilder import ViewConfiguration
from ._CommonViewBuilder import get_logger
from production.view import KdsView


class KdsViewBuilder(object):
    def __init__(self, message_bus, kds_manager, loggers):
        self.message_bus = message_bus
        self.kds_manager = kds_manager
        self.loggers = loggers

    def build(self, config):
        # type: (ViewConfiguration) -> KdsView
        if "ViewName" not in config.extra_config:
            raise ViewNameFoundException("KdsView without ViewName: {}".format(config.name))

        kds_manager = config.extra_config["KdsManager"] if "KdsManager" in config.extra_config else None
        if not kds_manager:
            kds_manager = self.kds_manager

        view_name = config.extra_config["ViewName"]
        tag_on_show = config.extra_config["TagOnShow"] if "TagOnShow" in config.extra_config else None
        return KdsView(
            name=config.name,
            message_bus=self.message_bus,
            view_name=view_name,
            kds_manager=kds_manager,
            tag_on_show=tag_on_show,
            logger=get_logger(config, self.loggers)
        )
