import logging


def _check_downsampling_mismatch(downsample, num_pooled_features, output_layer_size):
    """
    If downsample is flagged True, but no downsampling size is given, then automatically
    downsample model. If downsample flagged false, but there is a size given, set downsample
    to true.

    Parameters:
    ----------
    downsample : bool
        Boolean flagging whether model is being downsampled
    num_pooled_features : int
        the desired number of features to downsample to
    output_layer_size : int
        number of nodes in the output layer being downsampled
    Returns:
    -------
    downsample : boolean
        Updated boolean flagging whether model is being downsampled
    num_pooled_features : int
        Updated number of features model output is being downsample to
    """
    # If num_pooled_features left uninitialized, and they want to downsample,
    # perform automatic downsampling
    if num_pooled_features == 0 and downsample:
        if output_layer_size % 2 == 0:
            num_pooled_features = output_layer_size // 2
            logging.warning('Automatic downsampling to {}. If you would like to set custom '
                            'downsampling, pass in an integer divisor of {} to '
                            'num_pooled_features.'.format(num_pooled_features, output_layer_size))
        else:
            raise ValueError('Sorry, no automatic downsampling available for this model.')

    # If they have initialized num_pooled_features, but not turned on
    # downsampling, downsample to what they entered
    elif num_pooled_features != 0 and not downsample:
        logging.info('Downsampling to {}.'.format(num_pooled_features))
        downsample = True

    return downsample, num_pooled_features