import json

from typing import (
    List,
    Optional,
)

from messageprocessorutil import UuidMessageProcessor
from messagebus import NakMessage, AckMessage
from production.manager import ProductionManager


class PurgeOrdersProcessor(UuidMessageProcessor):
    def __init__(
        self,                   # type: PurgeOrdersProcessor
        production_manager,     # type: ProductionManager
    ):
        # type: (...) -> None

        super(PurgeOrdersProcessor, self).__init__()
        self.production_manager = production_manager
    
    def parse_data(
        self,           # type: PurgeOrdersProcessor
        data,           # type: bytes
        event_type,     # type: Optional[str]
    ):
        # type: (...) -> List[str]

        request = json.loads(data) if data else {}
        order_ids = request.get("orderIds") or []
        return order_ids

    def call_business(
        self,           # type: PurgeOrdersProcessor
        order_ids,      # type: List[str]
    ):
        # type: (...) -> None

        self.production_manager.purge_orders(
            order_ids=order_ids
        )

    def format_response(self, message_bus, message, event, input_obj, result):
        message_bus.reply_message(message, AckMessage())

    def format_exception(self, message_bus, message, event, input_obj, exception):
        message_bus.reply_message(message, NakMessage())

    def format_parse_exception(self, message_bus, message, event, data, exception):
        message_bus.reply_message(message, NakMessage())
