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"]
        return KdsView(config.name, self.message_bus, view_name, kds_manager, get_logger(config, self.loggers))
