from typing import (
    Dict,
    List,
    Optional,
)


class ActionResult(object):
    def __init__(
        self,
        discounts_id,                   # type: List
        items_to_burn,                  # type: Dict
        order_tender_ids,               # type: List
        discount_item_values,           # type: Dict
        benefit_applied,                # type: bool
        pending_to_apply,               # type: bool
        benefit_ids_to_remove=None,     # type: Optional[List[str]]
    ):
        # type: (...) -> None

        self.discounts_id = discounts_id
        self.items_to_burn = items_to_burn
        self.order_tenders = order_tender_ids
        self.discount_item_values = discount_item_values
        self.benefit_applied = benefit_applied
        self.pending_to_apply = pending_to_apply
        self.benefit_ids_to_remove = benefit_ids_to_remove if benefit_ids_to_remove else []

    def __add__(self, action_result):
        # type: (ActionResult) -> ActionResult

        return self._merge_action_result(self, action_result)

    def __radd__(self, action_result):
        # type: (ActionResult) -> ActionResult

        return self._merge_action_result(self, action_result)

    def __nonzero__(self):
        # type: () -> bool
        return bool(self.discounts_id) or bool(self.items_to_burn) or bool(self.order_tenders) or self.pending_to_apply

    @staticmethod
    def _merge_action_result(current, new):
        current.discounts_id.extend(new.discounts_id)
        current.order_tenders.extend(new.order_tenders)
        current.discount_item_values.update(new.discount_item_values)
        current.benefit_applied = current.benefit_applied and new.benefit_applied
        current.pending_to_apply = current.pending_to_apply or new.pending_to_apply
        current.benefit_ids_to_remove.extend(new.benefit_ids_to_remove)

        return ActionResult(
            discounts_id=current.discounts_id,
            items_to_burn=current.items_to_burn,
            order_tender_ids=current.order_tenders,
            discount_item_values=current.discount_item_values,
            benefit_applied=current.benefit_applied,
            pending_to_apply=current.pending_to_apply,
            benefit_ids_to_remove=current.benefit_ids_to_remove,
        )

