from .attckobject import AttckObject

class AttckMalware(AttckObject):
    """A child class of AttckObject
       Creates objects which have been categorized as malware used in attacks
    
    Arguments:
        AttckObject (dict) -- Takes the Mitre ATT&CK Json object as a kwargs values
    """

    def __init__(self, attck_obj = None, **kwargs):
        """Creates an AttckMalware object.  
           The AttckMalware object is based on malware which have been categorized as software used in attacks
        """

        self.attck_obj = attck_obj

        self.id = super(AttckMalware, self)._set_id(kwargs)
        self.created_by_ref = super(AttckMalware, self)._set_attribute(kwargs, 'created_by_ref')
        self.name = super(AttckMalware, self)._set_attribute(kwargs, 'name')
        self.aliases = super(AttckMalware, self)._set_list_items(kwargs, 'x_mitre_aliases')
        self.platforms = super(AttckMalware, self)._set_list_items(kwargs, 'x_mitre_platforms')
        self.labels = super(AttckMalware, self)._set_list_items(kwargs, 'labels')
        self.description = super(AttckMalware, self)._set_attribute(kwargs, 'description')
        self.external_references = super(AttckMalware, self)._set_reference(kwargs)
        self.created = super(AttckMalware, self)._set_attribute(kwargs, 'created')
        self.modified = super(AttckMalware, self)._set_attribute(kwargs, 'modified')
        self.stix = super(AttckMalware, self)._set_attribute(kwargs, 'id')
        self.type = super(AttckMalware, self)._set_attribute(kwargs, 'type')
        self.wiki = super(AttckMalware, self)._set_wiki(kwargs)
        self.contributor = super(AttckMalware, self)._set_list_items(kwargs, 'x_mitre_contributors')
        self.revoked = super(AttckMalware, self)._set_attribute(kwargs, 'revoked')

    @property
    def actors(self):
        '''Returns all actor objects as a list that are documented as using this malware'''
        from actor import AttckActor
        actor_list = []
        for item in self.attck_obj['objects']:
            if 'source_ref' in item:
                if self.stix in item['target_ref']:
                    for o in self.attck_obj['objects']:
                        if item['source_ref'] in o['id']:
                            actor_list.append(AttckActor(**o))
        return actor_list


    @property
    def techniques(self):
        '''Returns all technique objects as a list that are documented as using this malware'''
        from technique import AttckTechnique
        technique_list = []
        for item in self.attck_obj['objects']:
            if 'source_ref' in item:
                if self.stix in item['source_ref']:
                    for o in self.attck_obj['objects']:
                        if item['target_ref'] in o['id']:
                            technique_list.append(AttckTechnique(**o))
        return technique_list