import logging

from msgbus import MBEasyContext

from typing import Optional, Dict
from xml.etree import cElementTree as eTree

from tef import PrintReceipt
from utilfunctions import get_sector_name


class PrinterInteractor(object):
    def __init__(
        self,           # type: PrinterInteractor
        pos_id,         # type: str
        mb_context,     # type: MBEasyContext
    ):
        # type: (...) -> None

        self.logger = logging.getLogger("PosActions")
        self.pos_id = pos_id
        self.printer = PrintReceipt(
            mb_context=mb_context,
            logger=self.logger,
        )

    def print_tef_tender_receipt(
        self,                       # type: PrinterInteractor
        is_mobile,                  # type: bool
        order,                      # type: eTree.XML
        tender_detail,              # type: Dict
        print_customer_receipt,     # type: Optional[bool]
    ):
        # type: (...) -> bool

        sector_name = get_sector_name(order=order)
        fiscalization_type = "APOS" if is_mobile else ""
        sector_name = None if is_mobile else sector_name

        print_customer_receipt_response = self.printer.print_tef_tender_receipt(
            pos_id=self.pos_id,
            tender_detail=tender_detail,
            fiscalization_type=fiscalization_type,
            print_customer_receipt=print_customer_receipt,
            sector=sector_name,
        )

        return print_customer_receipt_response
