import re
import logging


def check_manifest_file(parsed_apk, apk_detail):
    """
    Checks the manifest file for details surrounding the use of safetynet

    Args:
        parsed_apk {pyaxmlparser.core.APK}: object representing the file
            structure  of the APK
        apk_detail {ApkDetail}: SqlAlchemy object representing a row in the
            apk_detail table
    Returns:
        ApkDetail
    """

    metadata_key = '{http://schemas.android.com/apk/res/android}name'
    safetynet_attestation_api_key = 'com.google.android.safetynet.ATTEST_API_KEY'

    for element in parsed_apk.xml['AndroidManifest.xml'].iter():
        if metadata_key in element.attrib:
            if re.match(safetynet_attestation_api_key,
                        element.attrib[metadata_key]):
                apk_detail.attestation_in_manifest = True
                logging.info("Found the presence of safetynet "
                             "in the AndroidManifest.xml for {}".format(
                             apk_detail.app_name))
                break
    if not apk_detail.attestation_in_manifest:
        logging.info("{}'s Manifest file does not "
                     "have any details concerning safetynet".format(
            apk_detail.app_name))

    return apk_detail