import math


def calc_max_intake(inner_animal_trait_dict):
    """Calculate maximum daily forage intake for a single animal type.

    An animal's maximum intake is the maximum potential daily intake of dry
    matter (kg) and depends on the size, condition, and reproductive stage of
    the animal.  This trait is "non-spatial", i.e. it does not vary according
    to the context of the animal but is dictated solely by inherent animal
    traits.

    Parameters:
        inner_animal_trait_dict (dict): dictionary of key, value pairs
            representing input and derived traits for this animal type.

    Returns:
        updated_trait_table, a dictionary of key, value pairs where values
        indicate input and derived traits for this animal type, including the
        following updated animal trait:
            - max_intake, maximum daily forage intake in kg dry matter

    """
    updated_trait_table = inner_animal_trait_dict.copy()
    if updated_trait_table['BC'] > 1.:
        CF = (
            updated_trait_table['BC'] *
            (updated_trait_table['CI20'] - updated_trait_table['BC']) /
            (updated_trait_table['CI20'] - 1.))
    else:
        CF = 1.
    YF = 1.  # eq 4 gives a different value for unweaned animals
    TF = 1.  # ignore effect of temperature on intake
    LF = 1.  # assume any lactating animals are suckling young (eq 8)
    if updated_trait_table['reproductive_status_int'] == 2:
        BCpart = updated_trait_table['BC']  # body condition at parturition
        Mi = updated_trait_table['A_y'] / updated_trait_table['CI8']
        LA = (
            1. - updated_trait_table['CI15'] +
            updated_trait_table['CI15'] * BCpart)
        LF = (
            1. + updated_trait_table['CI19'] * Mi **
            updated_trait_table['CI9'] * math.exp(
                updated_trait_table['CI9'] * (1 - Mi)) * LA)
    updated_trait_table['max_intake'] = (
        updated_trait_table['CI1'] * updated_trait_table['SRW_modified'] *
        updated_trait_table['Z'] * (
            updated_trait_table['CI2'] - updated_trait_table['Z']) *
        CF * YF * TF * LF)  # eq 2
    return updated_trait_table