import copy


def apply_fusion_strategy(cfg, strategy='fuse_late'):
    """Apply the specific fusion strategy to a basic config

    Parameters
    ----------
    cfg
        The basic configuration
    strategy
        The type of the fusion strategy, can be
        - fuse_late:
            Use separate networks for extracting features of text, numerical and categorical data.
        - fuse_early:
            Keep the token embeddings and use an additional transformer to fuse the information.
        - all_text:
            Convert all categorical and numerical data to text and then use the text network

    Returns
    -------
    new_cfg
        The new configuration.
    """
    new_cfg = copy.deepcopy(cfg)
    search_space = new_cfg['models']['MultimodalTextModel']['search_space']
    if strategy == 'fuse_late':
        search_space['model.network.agg_net.agg_type'] = 'concat'
        search_space['model.network.aggregate_categorical'] = True
        search_space['preprocessing.categorical.convert_to_text'] = False
        search_space['preprocessing.numerical.convert_to_text'] = False
    elif strategy == 'fuse_early':
        search_space['model.network.agg_net.agg_type'] = 'attention_token'
        search_space['model.network.aggregate_categorical'] = False
        search_space['preprocessing.categorical.convert_to_text'] = False
        search_space['preprocessing.numerical.convert_to_text'] = False
    elif strategy == 'all_text':
        search_space['model.network.agg_net.agg_type'] = 'concat'
        search_space['model.network.aggregate_categorical'] = False
        search_space['preprocessing.categorical.convert_to_text'] = True
        search_space['preprocessing.numerical.convert_to_text'] = True
    else:
        raise NotImplementedError
    return new_cfg