import requests


def calc_hash_checksum(file_p, remote_url, hash_type):
    """Calculating the tool hash value checksum

    :param file_p: The checked file full path ( fileDir+fileName )
    :type file_p: str/None
    :param remote_url: The main url to retrieve the tool file
    :type remote_url: str/None
    :param hash_type: One of the methods from hashlib.algorithms_guaranteed
    :type hash_type: str
    :return: hash value
    :rtype: str
    """

    global latest_tool_version  # set as global to avoid downloading in case hash compare results in a new available version.
    hash_calc = eval('hashlib.' + hash_type + '()')

    if remote_url is None:
        with open(file_p, 'rb') as fh:
            while True:
                data = fh.read()
                hash_calc.update(data)
                if not data:
                    break
    else:
        response = requests.get(remote_url, allow_redirects=True, headers={'Cache-Control': 'no-cache'})
        latest_tool_version = response.content
        hash_calc.update(latest_tool_version)

    if hash_type in ['shake_128', 'shake_256']:
        return hash_calc.hexdigest(64)
    else:
        return hash_calc.hexdigest()