import os
from igor2.binarywave import load as loadibw

def read_xps_ibw_file_as_dict(filename):
    """
    Reads IBW files from the Multiphase Chemistry Group, which contain XPS spectra and acquisition settings,
    and formats the data into a dictionary with the structure {datasets: list of datasets}. Each dataset in the
    list has the following structure:

    {
        'name': 'name',
        'data': data_array,
        'data_units': 'units',
        'shape': data_shape,
        'dtype': data_type
    }

    Parameters
    ----------
    filename : str
        The IBW filename from the Multiphase Chemistry Group beamline.

    Returns
    -------
    file_dict : dict
        A dictionary containing the datasets from the IBW file. 

    Raises
    ------
    ValueError
        If the input IBW file is not a valid IBW file.
        
    """


    file_obj = loadibw(filename)

    required_keys = ['wData','data_units','dimension_units','note'] 
    if sum([item in required_keys for item in file_obj['wave'].keys()]) < len(required_keys):
        raise ValueError('This is not a valid xps ibw file. It does not satisfy minimum adimissibility criteria.')
    
    file_dict = {}
    path_tail, path_head = os.path.split(filename)

    # Group name and attributes
    file_dict['name'] = path_head
    file_dict['attributes_dict'] = {}
 
    # Convert notes of bytes class to string class and split string into a list of elements separated by '\r'. 
    notes_list = file_obj['wave']['note'].decode("utf-8").split('\r')
    exclude_list = ['Excitation Energy']
    for item in notes_list:
        if '=' in item:
            key, value = tuple(item.split('='))
            # TODO: check if value can be converted into a numeric type. Now all values are string type
            if not key in exclude_list:
                file_dict['attributes_dict'][key] = value

    # TODO: talk to Thorsten to see if there is an easier way to access the below attributes
    dimension_labels = file_obj['wave']['dimension_units'].decode("utf-8").split(']')
    file_dict['attributes_dict']['dimension_units'] = [item+']' for item in dimension_labels[0:len(dimension_labels)-1]]

    # Datasets and their attributes

    file_dict['datasets'] = []

    dataset = {}
    dataset['name'] = 'spectrum'
    dataset['data'] = file_obj['wave']['wData']
    dataset['data_units'] = file_obj['wave']['data_units']
    dataset['shape'] = dataset['data'].shape
    dataset['dtype'] = type(dataset['data'])   

    # TODO: include energy axis dataset

    file_dict['datasets'].append(dataset)
    

    return file_dict