import logging

#import SimpleITK as sitk
import nrrd
import numpy as np

#def read(path):
#    return np.swapaxes(sitk.GetArrayFromImage(sitk.ReadImage(str(path))), 0, 2)    

def read(path):
    return np.ascontiguousarray(nrrd.read(path)[0])



def load_annotation(annotation_path, data_mask_path=None):
    '''Read data files segmenting the reference space into regions of valid 
    and invalid data, then further among brain structures
    '''
    
    logging.info('getting annotation')
    annotation = read(annotation_path)
    
    logging.info('casting to signed')
    #  It shouldn't matter now, but there may be future structures with ids 
    #  sufficiently large that we need that extra bit
    logging.debug('max annotated value: {0}'.format(np.amax(annotation)))
    annotation = annotation.astype(np.int32)
    logging.debug('max annotated value: {0}'.format(np.amax(annotation)))
    
    logging.info('negating left hemisphere')
    logging.debug('min annotated value: {0}'.format(np.amin(annotation)))
    lr_mid = int( np.round(annotation.shape[2] / 2) )
    annotation[:, :, :lr_mid] = annotation[:, :, :lr_mid] * -1
    logging.debug('min annotated value: {0}'.format(np.amin(annotation)))

    if data_mask_path is not None:
        logging.info('getting_data_mask')
        data_mask =read(data_mask_path)
        
        logging.info('applying data mask')
        annotation[np.logical_not(data_mask)] = 0
   
    return annotation


def get_sum_pixels(sum_pixels_path):    
    logging.info('getting sum_pixels')
    return {'sum_pixels': read(sum_pixels_path)}
    

def get_sum_pixel_intensities(sum_pixel_intensities_path, injection_sum_pixel_intensities_path):
    logging.info('getting sum pixel intensities')
    return {'sum_pixel_intensities': read(sum_pixel_intensities_path),
            'injection_sum_pixel_intensities': read(injection_sum_pixel_intensities_path)}


def get_cav_density(cav_density_path):    
    logging.info('getting cav density')
    return {'cav_density': read(cav_density_path)}


def get_injection_data(injection_fraction_path, injection_density_path, 
                       injection_energy_path):
    '''Read nrrd files containing injection signal data
    '''

    logging.info('getting injection_fraction')
    injection_fraction = read(injection_fraction_path)
    
    logging.info('getting injection_sum_projecting_pixels')
    injection_density =  read(injection_density_path)
    
    logging.info('getting injection_energy')
    injection_energy = read(injection_energy_path)
    
    return {'injection_fraction': injection_fraction, 
            'injection_density': injection_density, 
            'injection_energy': injection_energy}
            
            
def get_projection_data(projection_density_path, projection_energy_path, 
                        aav_exclusion_fraction_path=None):
    '''Read nrrd files containing global signal data
    '''
    
    logging.info('getting projection density')
    projection_density =  read(projection_density_path)
    
    logging.info('getting projection energy')
    projection_energy = read(projection_energy_path)
    
    try:
        logging.info('getting aav exclusion fraction')
        aav_exclusion_fraction = read(aav_exclusion_fraction_path)
        aav_exclusion_fraction[aav_exclusion_fraction > 0] = 1
        aav_exclusion_fraction = aav_exclusion_fraction.astype(np.bool_, order='C')
    
    except (IOError, OSError, RuntimeError):
        logging.info('skipping aav exclusion fraction')
        aav_exclusion_fraction = np.zeros(projection_density.shape, dtype=np.bool_, order='C')
    
    return {'projection_density': projection_density, 
            'projection_energy': projection_energy, 
            'aav_exclusion_fraction': aav_exclusion_fraction}
