from pos_model import Order
from typing import (
    Any,
    Dict,
)

from _ContextKeys import ContextKeys
from _NfceXmlPartBuilder import NfceXmlPartBuilder
from services import FeeService


class DetFeeBuilder(NfceXmlPartBuilder):

    def __init__(
        self,           # type: DetFeeBuilder
        det_builder,    # type: NfceXmlPartBuilder
        fee_service,    # type: FeeService
    ):
        # type: (...) -> None

        self.det_builder = det_builder
        self.fee_service = fee_service

    def build_xml(
        self,       # type: DetFeeBuilder
        order,      # type: Order
        context,    # type: Dict[unicode, Any]
    ):
        # type: (...) -> unicode

        xml = ""
        fee_items = self.fee_service.get_fee_sale_items(order=order)

        for item in fee_items:
            context[ContextKeys.current_sale_item] = item
            xml += self.det_builder.build_xml(
                order=order,
                context=context,
            )
            context[ContextKeys.item_no] += 1
        return xml
