import copy
import six


def param_dict_cleaner(parameter, element_list):
    """
    Make sure param only contains element from element_list.

    Parameters
    ----------
    parameter : dict
        fitting parameters
    element_list : list
        list of elemental lines

    Returns
    -------
    dict :
        new param dict containing given elements
    """
    param = copy.deepcopy(parameter)
    param_new = {}

    elist_lower = [e.lower() for e in element_list if len(e) <= 4]
    pileup_list = [e for e in element_list if '-' in e]
    userpeak_list = [e for e in element_list if 'user' in e.lower()]

    for k, v in six.iteritems(param):
        if k == 'non_fitting_values' or k == k.lower():
            param_new.update({k: v})
        elif 'pileup' in k:
            for p in pileup_list:
                if p.replace('-', '_') in k:
                    param_new.update({k: v})
        elif 'user' in k.lower():
            for p in userpeak_list:
                if p in k:
                    param_new.update({k: v})
        elif (k[:3].lower() in elist_lower) or (k[:4].lower() in elist_lower):
            param_new.update({k: v})

    return param_new