from model.loyalty import Action, Exclusions, Conditions
from typing import Dict, Tuple


class Benefit(object):
    def __init__(
        self,
        loyalty,    # type: Dict
    ):
        # type: (...) -> None

        self.id = loyalty.get("id")
        self.name = loyalty.get("name")
        self.user_description = loyalty.get("userDescription")
        self.priority = int(loyalty.get("priority")) if loyalty.get("priority") else None
        self.automatic = loyalty.get("automatic")
        self.tip_included = loyalty.get("tipIncluded", False)
        self.ask_value = bool(loyalty.get("askValue"))
        conditions = loyalty.get("conditions", {})
        self.conditions = Conditions(
            obj=conditions,
        )
        actions = [self._get_action(e) for e in loyalty.get("actions", {}).items()]
        self.actions = actions
        exclusions = loyalty.get("exclusions", {})
        self.exclusions = Exclusions(
            obj=exclusions,
        )
        self.times_to_apply = loyalty.get("timesToApply")

    @staticmethod
    def _get_action(
        e,   # type: Tuple
    ):
        # type: (...) -> Action

        return Action(
            obj={
                "order": e[0],
                "name": e[1].items()[0][0],
                "value": e[1].items()[0][1],
            },
        )
