# encoding: utf-8
from escposformatter import DefaultEscPosFormatter
from mwposreport import ReporterFactory
from msgbus import MBEasyContext

from reportsdata.deliveryreport import DeliveryGenerator, DeliveryPresenter
from deliveryreport.repository import DeliveryOrderRepository, DeliveryProductRepository


class ReportGenerator(object):
    @staticmethod
    def generate(
        mb_context,             # type: MBEasyContext
        pos_id,                 # type: str
        order_id,               # type: str
        pos_list,               # type: str
        show_customer_phone,    # type: str
    ):
        # type: (...) -> str

        if not pos_list:
            pos_list = [pos_id]
        else:
            pos_list = pos_list.split(",")

        order_repository = DeliveryOrderRepository(mb_context, pos_list)
        product_repository = DeliveryProductRepository(mb_context)
        report = ReporterFactory.build_reporter(
            pos_id=pos_id,
            generator=DeliveryGenerator(
                order_repository=order_repository,
                product_repository=product_repository,
                order_id=order_id,
                show_customer_phone=show_customer_phone.lower() == "true",
            ),
            presenter=DeliveryPresenter(56),
        ).create_base_report()
        return DefaultEscPosFormatter().format(report.get_parts())
