"""Vulnerability mixin for WS-Discovery-related issues.

Detects WS-Discovery responses and records device/network-level findings.
"""
from __future__ import annotations


class WSMixin:
    def store_ws_vulnerability(self) -> None:
        role_dict = self.read_role_dict(self.role_file)
        if not role_dict:
            return

        ipver_values = self._get_ipver_values()
        if not ipver_values:
            return

        def make_query(ipver_value):
            return lambda p, m, sm: f"version = {ipver_value}" in p and "dport = 3702" in p and m == sm

        def make_response(ipver_value):
            return lambda p, m: f"version = {ipver_value}" in p and "sport = 3702" in p

        for ipver_value in ipver_values:
            if ipver_value == "4":
                device_code = "PTV-NET-IDENT-4-WSDEV"
                device_desc = "Device responds to IPv4 WS-Discovery packets"
                net_code = "PTV-NET-IDENT-4-WS"
                net_desc = "Network allows devices discovery with IPv4 WS-Discovery"
            else:
                device_code = "PTV-NET-IDENT-6-WSDEV"
                device_desc = "Device responds to IPv6 WS-Discovery packets"
                net_code = "PTV-NET-IDENT-6-WS"
                net_desc = "Network allows devices discovery with IPv6 WS-Discovery"

            self._process_query_response_vulnerability(
                make_query(ipver_value),
                make_response(ipver_value),
                role_dict,
                [ipver_value],
                device_code,
                device_desc,
                net_code,
                net_desc,
            )
