# By: Riasat Ullah
# This class represents an assignee.

from utils import times, var_names


class Assignee(object):

    def __init__(self, for_policy_id, user_policy_id, assignee_level, for_policy_name=None, display_name=None,
                 minutes=None, ack_code=None, res_code=None, esc_code=None, last_notification_timestamp=None,
                 free_notification_count=0, paid_notification_count=0):
        # free_notification_count is the aggregate of the number of notifications that were sent out by EMAIL and APP
        # paid_notification_count is the aggregate of the number of notifications that were sent our by TEXT and CALL

        self.for_policy_id = for_policy_id
        self.user_policy_id = user_policy_id
        self.assignee_level = assignee_level
        self.policy_name = for_policy_name
        self.display_name = display_name
        self.minutes = minutes
        self.acknowledgement_code = ack_code
        self.resolution_code = res_code
        self.escalation_code = esc_code
        self.last_notification_timestamp = last_notification_timestamp
        self.free_notification_count = free_notification_count
        self.paid_notification_count = paid_notification_count

    @staticmethod
    def create_assignee(details):
        '''
        Creates an Assignee object from assignee details in a dict.
        :param details: (dict) of assignee details
        :return:
        '''
        return Assignee(
            details[var_names.for_policyid], details[var_names.user_policyid], details[var_names.assignee_level],
            for_policy_name=details[var_names.policy_name] if var_names.policy_name in details else None,
            display_name=details[var_names.display_name] if var_names.display_name in details else None,
            minutes=details[var_names.level_minutes] if var_names.level_minutes in details else None,
            ack_code=details[var_names.acknowledgement_code] if var_names.acknowledgement_code in details else None,
            res_code=details[var_names.resolution_code] if var_names.resolution_code in details else None,
            esc_code=details[var_names.escalation_code] if var_names.escalation_code in details else None,
            last_notification_timestamp=times.get_timestamp_from_string(details[var_names.last_notification_timestamp])
            if var_names.last_notification_timestamp in details
            and details[var_names.last_notification_timestamp] is not None else None,
            free_notification_count=details[var_names.free_notification_count]
            if var_names.free_notification_count in details else 0,
            paid_notification_count=details[var_names.paid_notification_count]
            if var_names.paid_notification_count in details else 0
        )

    def to_dispatch_tuple(self):
        '''
        Get a tuple of user_id and policy id that is needed to book dispatch events.
        :return: (tuple) -> (user_policyid, for_policyid)
        '''
        return self.user_policy_id, self.for_policy_id

    def to_dict(self):
        '''
        Gets the dict of the Assignee object.
        :return: dict of Assignee object
        '''
        data = {var_names.for_policyid: self.for_policy_id,
                var_names.user_policyid: self.user_policy_id,
                var_names.assignee_level: self.assignee_level,
                var_names.policy_name: self.policy_name,
                var_names.display_name: self.display_name,
                var_names.level_minutes: self.minutes,
                var_names.acknowledgement_code: self.acknowledgement_code,
                var_names.resolution_code: self.resolution_code,
                var_names.escalation_code: self.escalation_code,
                var_names.last_notification_timestamp: self.last_notification_timestamp,
                var_names.free_notification_count: self.free_notification_count,
                var_names.paid_notification_count: self.paid_notification_count}
        return data
