# By: Riasat Ullah
# This class represents a routine rotation.

from utils import var_names


class RoutineRotation(object):

    def __init__(self, layer, start_period, end_period, assignee_name, display_name, assignee_policy_id):
        '''
        Constructor
        :param layer: the number of the layer the rotation is for
        :param start_period: the day in the full rotation period of the layer this rotation will apply from
        :param end_period: the day in the full rotation period (not inclusive) this rotation will apply till
        :param assignee_name: preferred username when in display mode | user id otherwise
        :param display_name: the full name of the user
        :param assignee_policy_id: policy ID of the assignee
        '''
        self.layer = layer
        self.start_period = start_period
        self.end_period = end_period
        self.assignee_name = assignee_name
        self.display_name = display_name
        self.assignee_policy_id = assignee_policy_id

    @staticmethod
    def create_rotation(layer, details, for_display=False):
        '''
        Creates a new rotations object from a dict of rotations info.
        :param layer: (int) the layer number
        :param details: (dict) rotations info
        :param for_display: (boolean) True if the Policy(s)/Routine(s) should be mapped on to their reference IDs
        :return: RoutineRotation object
        '''
        assig_name = details[var_names.preferred_username] if for_display and var_names.preferred_username in details\
            else details[var_names.assignee_name]
        assig_pol = details[var_names.policy_ref_id] if for_display and var_names.policy_ref_id in details\
            else details[var_names.assignee_policy_id]

        return RoutineRotation(layer, details[var_names.start_period], details[var_names.end_period],
                               assig_name, details[var_names.display_name], assig_pol)

    def equivalent_to(self, new_rotation):
        '''
        Checks if two PolicyRotations are the same or not
        :param new_rotation: the PolicyRotation to check against
        :return: (boolean) True if it is; False otherwise
        :errors: AssertionError
        '''
        assert isinstance(new_rotation, RoutineRotation)
        if self.to_dict() == new_rotation.to_dict():
            return True
        return False

    def to_dict(self):
        '''
        Gets the dict of a PolicyRotation object.
        :return: dict of PolicyRotation object
        '''
        data = {var_names.layer: self.layer,
                var_names.start_period: self.start_period,
                var_names.end_period: self.end_period,
                var_names.assignee_name: self.assignee_name,
                var_names.display_name: self.display_name,
                var_names.assignee_policy_id: self.assignee_policy_id}
        return data
