from xml_utils import get_attribute, get_content_of


def fix_null(data):
    if data == 'null':
        return 0
    return data


class VulnerabilityExceptionStatus:
    UNDER_REVIEW = "Under Review"
    APPROVED = "Approved"
    REJECTED = "Rejected"
    DELETED = "Deleted"  # This state is also used for recalled exceptions!


class VulnerabilityExceptionReason:
    FALSE_POSITIVE = "False Positive"
    COMPENSATING_CONTROL = "Compensating Control"
    ACCEPTABLE_USE = "Acceptable Use"
    ACCEPTABLE_RISK = "Acceptable Risk"
    OTHER = "Other"


class VulnerabilityExceptionScope:
    ALL_INSTANCES = "All Instances"
    ALL_INSTANCES_SPECIFIC_ASSET = "All Instances on a Specific Asset"
    ALL_INSTANCES_SPECIFIC_SITE = "All Instances on a Specific Site"
    SPECIFIC_INSTANCE_SPECIFIC_ASSET = "Specific Instance of Specific Asset"


class SiloVulnerabilityExceptionDetails:
    @staticmethod
    def CreateFromXML(xml_data):
        details = SiloVulnerabilityExceptionDetails()
        details.silo_id = get_attribute(xml_data, 'siloId', details.silo_id)
        details.oldest_exception_creation_date = get_attribute(xml_data, 'oldestExceptionCreationDate', details.oldest_exception_creation_date)  # TODO: date object
        details.pending_exception_count = get_attribute(xml_data, 'pendingVulnExceptionsCount', details.pending_exception_count)
        return details

    def __init__(self):
        self.silo_id = ''
        self.oldest_exception_creation_date = 'N/A'  # TODO: date object
        self.pending_exception_count = 0


class VulnerabilityException:
    @staticmethod
    def CreateFromXML(xml_data):
        details = VulnerabilityException()
        details.id = int(get_attribute(xml_data, 'exception-id', details.id))
        details.vulnerability_id = get_attribute(xml_data, 'vuln-id', details.vulnerability_id)
        details.vulnerability_key = get_attribute(xml_data, 'vuln-key', details.vulnerability_key)
        details.expiration_date = get_attribute(xml_data, 'expiration-date', details.expiration_date)  # TODO: date object
        details.submitter = get_attribute(xml_data, 'submitter', details.submitter)
        details.submitter_comment = get_content_of(xml_data, 'submitter-comment', details.submitter_comment)
        details.reviewer = get_attribute(xml_data, 'reviewer', details.reviewer)
        details.reviewer_comment = get_content_of(xml_data, 'reviewer-comment', details.reviewer_comment)
        details.status = get_attribute(xml_data, 'status', details.status)
        details.reason = get_attribute(xml_data, 'reason', details.reason)
        details.scope = get_attribute(xml_data, 'scope', details.scope)
        details.asset_id = int(fix_null(get_attribute(xml_data, 'device-id', details.asset_id)))
        details.asset_port = int(fix_null(get_attribute(xml_data, 'port-no', details.asset_port)))
        return details

    def __init__(self):
        self.id = 0
        self.vulnerability_id = ''
        self.vulnerability_key = ''
        self.expiration_date = ''  # TODO: date object
        self.submitter = ''
        self.submitter_comment = ''
        self.reviewer = ''
        self.reviewer_comment = ''
        self.status = ''
        self.reason = ''
        self.scope = ''
        self.asset_id = 0
        self.asset_port = 0
