from xml.etree import cElementTree as eTree

from application.interactor import OrderModifiedPaidInteractor
from application.model import AsyncUuidMessageProcessor
from application.repository.pos.utils import get_originator_pos_id
from messagebus import DefaultToken, DataType, Message, MessageBus, Event
from typing import Optional, Any


class OrderModifiedPaidProcessor(AsyncUuidMessageProcessor):
    
    def __init__(self, interactor):
        # type: (OrderModifiedPaidInteractor) -> None

        super(OrderModifiedPaidProcessor, self).__init__()
        self.interactor = interactor

    def parse_data(self, data):
        # type: (str) -> (int, int, int, eTree)

        order_picture = eTree.XML(data).find(".//Order")
        current_pos_id = int(order_picture.get("posId"))
        originator_pos_id = get_originator_pos_id(order_picture) or current_pos_id
        order_id = int(order_picture.get("orderId"))

        return current_pos_id, originator_pos_id, order_id, order_picture

    def call_business(self, params):
        # type: ((int, int, int, eTree)) -> None

        current_pos_id, originator_pos_id, order_id, order_picture = params
        return self.interactor.burn_paid_benefit(current_pos_id, originator_pos_id, order_id, order_picture)

    def format_response(self, message_bus, message, event, request, response):
        # type: (MessageBus, Optional[Message], Optional[Event], Any, Any) -> None
        return

    def format_exception(self, message_bus, message, event, input_obj, exception):
        # type: (MessageBus, Optional[Message], Optional[Event], Any, BaseException) -> None
        return

    def format_parse_exception(self, message_bus, message, event, data, exception):
        # type: (MessageBus, Optional[Message], Optional[Event], bytes, BaseException) -> None
        return
