# coding=utf-8

from .utils import multiton

@multiton
class Vulnerability(object):
    def __init__(self, nessus, plugin_id, severity):
        self.nessus = nessus
        self.plugin_id = int(plugin_id)
        self.severity = int(severity)

        self._name = None
        self._family = None

    @property
    def name(self):
        if self._name is None:
            self._set_name_and_family()
        return self._name

    @name.setter
    def name(self, value):
        self._name = value
    
    @property
    def family(self):
        if self._family is None:
            self._set_name_and_family()
        return self._family

    @family.setter
    def family(self, value):
        self._family = value

    def _set_name_and_family(self):
        data = self.nessus._request('plugins/attributes/pluginSearch', **{'filter.0.quality':'match', 'filter.0.value': self.id, 'filter.0.filter':'plugin_id'})['pluginList']['plugin']
        assert int(data['pluginID']) == self.plugin_id, "Recieved plugin ID doesn't match."
        self._name = data['pluginName']
        self._family = data['pluginFamily']

    def __str__(self):
        return "Vuln {0}: {1} [{2}]".format(self.id, self.name, self.severity)

# >>> request('plugins/attributes/pluginSearch', **{"filter.0.quality":"match", "filter.0.value":"80494","filter.0.filter":"plugin_id"}) 
# OrderedDict([(u'pluginList', OrderedDict([(u'plugin', OrderedDict([(u'pluginFileName', u'smb_nt_ms15-005.nasl'), (u'pluginID', u'80494'), (u'pluginName', u'MS15-005: Vulnerability in Network Location Awareness Service Could Allow Security Feature Bypass (3022777)'), (u'pluginFamily', u'Windows : Microsoft Bulletins')]))]))]) 
