from abc import ABCMeta, abstractmethod
from logging import Logger
from threading import Thread

from production.model import ProductionOrder
from typing import List


class ProductionView(object):
    __metaclass__ = ABCMeta

    def __init__(self, name, logger):
        # type: (str, Logger) -> None
        self.name = name
        self.logger = logger
        if self.logger is not None:
            self.log_level = {
                "debug": self.logger.debug,
                "warning": self.logger.warning,
                "error": self.logger.error,
                "exception": self.logger.exception
            }

    @abstractmethod
    def handle_order(self, order, synch=False):
        # type: (ProductionOrder, bool) -> Thread

        raise NotImplementedError()

    @abstractmethod
    def get_view_tags(self):
        # type: () -> List[str]

        raise NotImplementedError()

    def debug(self, msg, *args):
        self.log("debug", msg, *args)

    def warning(self, msg, *args):
        self.log("warning", msg, *args)

    def error(self, msg, *args):
        self.log("error", msg, *args)

    def exception(self, msg, *args):
        self.log("exception", msg, *args)

    def log(self, level, msg, *args):
        if self.logger is not None:
            func = self.log_level[level]
            msg = msg.format(*args)
            func(msg)
