from typing import Dict
from typing import List
from typing import Tuple
from typing import Optional


def deconstruct_entry(entry: Dict[str, str],
                      serial_fields: List[str],
                      vsys_fields: List[str],
                      sightings_fields: List[str],
                      source_ip_fields: List[str]) -> Tuple[Optional[str],
                                                            Optional[str],
                                                            Optional[str],
                                                            Optional[int]]:
    """
    deconstruct_entry
    Extracts device relevant fields from a log entry.

    :type entry: ``Dict[str, str]``
    :param entry: Log entry as dictionary of fields.

    :type serial_fields: ``List[str]``
    :param serial_fields: List of possible field names in log entry to be considered as serial numbers.

    :type vsys_fields: ``List[str]``
    :param vsys_fields: List of possible field names in log entry to be considered as vsys names.

    :type sightings_fields: ``List[str]``
    :param sightings_fields: List of possible field names in log entry to be considered as number of occurences.

    :type source_ip_fields: ``List[str]``
    :param source_ip_fields: List of possible field names in log entry to be considered as source IPs.

    :return: Tuple where the first element is the serial number or None, the second element is the
        vsys name or None, the third element is the source IP or None and the fourth element is the number of
        occurences of the event.
    :rtype: ``Tuple[Optional[str], Optional[str], Optional[str], Optional[int]]``
    """
    serial = next((entry[field] for field in serial_fields if field in entry), None)
    vsys = next((entry[field] for field in vsys_fields if field in entry), '')
    sightings = next((int(entry[field]) for field in sightings_fields if field in entry), 1)
    source_ip = next((entry[field] for field in source_ip_fields if field in entry), None)

    return serial, vsys, source_ip, sightings