from logging import Logger

from production.model import (
    ProductionOrder,
)
from production.util import parse_constants
from production.view.printerviewservices.reportgenerator import PickListReportGenerator
from production.view.printerviewservices.reportprinter import PickListPrinter
from typing import List
from ._PrinterView import PrinterView


class PicklistPrinterView(PrinterView):
    def __init__(
        self,
        name,                   # type: str
        production_manager,     # type: ProductionManager
        report_generator,       # type: PickListReportGenerator
        picklist_printer,       # type: PickListPrinter
        tags_on_print=None,     # type: List[str]
        logger=None,            # type: Logger
    ):
        # type: (...) -> None

        super(PicklistPrinterView, self).__init__(
            name=name,
            tags_on_print=tags_on_print,
            logger=logger,
        )

        self.production_manager = production_manager
        self.report_generator = report_generator
        self.picklist_printer = picklist_printer

    def get_printer_name(
        self,       # type: PicklistPrinterView
    ):
        # type: (...) -> str

        return self.picklist_printer.get_name()

    def generate_report(
        self,       # type: PicklistPrinterView
        order,      # type: ProductionOrder
    ):
        # type: (...) -> str

        is_reprint = self.production_manager.get_picklist_id(order=order) is not None
        report = self.report_generator.production_pick_list(
            production_order=order,
            is_reprint=is_reprint,
        )
        return report

    def print_report(
        self,       # type: PicklistPrinterView
        report,     # type: str
        order,      # type: ProductionOrder
    ):
        # type: (...) -> None

        is_reprint = self.production_manager.get_picklist_id(order=order) is not None
        parsed_report = parse_constants(
            printer_text=report,
            pos_id=int(order.originator[-2:]),
            is_reprint=is_reprint,
        )
        return self.picklist_printer.print_report(text=parsed_report)

    def save_report(
        self,               # type: PrinterView
        report,             # type: str
        order,              # type: ProductionOrder
        print_status,       # type: bool
    ):
        # type: (...) -> None

        self.production_manager.save_pick_list_order(
            order=order,
            receipt=report,
            status=print_status,
        )

    def clear_printer_cache(
        self,       # type: PicklistPrinterView
    ):
        # type: (...) -> None

        self.picklist_printer.clear_printer_cache()
