addpath(genpath('/p/spoclab/tools/EEGlab/eeglab/'));
addpath(genpath('/p/spoclab/tools/HCRF2.0b/'))
addpath(genpath('/p/spoclab/tools/Straight/'));
addpath(genpath('/p/spoclab/tools/SilenceRemoval/'));
addpath(genpath('/p/spoclab/tools/NeuralNetworks/DeepLearnToolbox/'));
addpath(genpath('/p/spoclab/tools/rpde/'));

addpath(genpath('/w/148/spoclab/users/szhao/EEG/kara/shunan_project/'));

% The folders for which we want to compute the features.
folders = {'P02', 'MM05', 'MM08', 'MM09', 'MM10', 'MM11', 'MM12', 'MM14'};
folders = [folders {'MM15', 'MM16', 'MM18', 'MM19', 'MM20', 'MM21'}];
data_path = '/w/148/spoclab/users/szhao/EEG/data';
misc_folder = [data_path '/misc'];
ICA = false;

% whether we are just using simple descriptive features (a much smaller feature
% set).
simple = true;

for j=1:length(folders)
    
    disp(['Computing features for folder ' folders{j}]);
    folder = [data_path '/' folders{j}];
    D = dir([folder '/*.set']);
    % there should only be 1 set file.
    set_fn = [folder '/' D(1).name];
    kinect_folder = [folder '/kinect_data'];
    labels_fn = [kinect_folder '/labels.txt'];

    % split the file, create the epochs, and save.
    EEG = pop_loadset(set_fn);
    data = EEG.data;
    
    % Applying the ICA decomposition.
    if ICA
        W = EEG.icaweights * EEG.icasphere;
    else
        W = eye(62, 62);
    end
    
    disp('Splitting the data');
    load([folder '/epoch_inds.mat']);
    % splitting the data itself.
    clearing_mats = split_data(clearing_inds, data);
    speaking_mats = split_data(speaking_inds, data);
    thinking_mats = split_data(thinking_inds, data);
    num_e = length(clearing_mats);

    s = 1:2:(num_e*2);
    stimuli_mats = speaking_mats(s);
    s = 2:2:(num_e*2);      
    spk_mats = speaking_mats(s);
    speaking_mats = spk_mats;

    % trimming the data.
    speaking_mats = trim_speaking_mats(speaking_mats, kinect_folder);

    epoch_data.thinking_mats = thinking_mats;
    epoch_data.clearing_mats = clearing_mats;
    epoch_data.stimuli_mats = stimuli_mats;
    epoch_data.speaking_mats = speaking_mats;

    % computing the EEG features.
    disp('Computing the various features');
    [eeg_features, feature_labels] = get_eeg_features(epoch_data, W, simple);

    % Getting the audio features.
    [wav_features, feature_labels] = get_acoustic_features(kinect_folder, simple);

    % Getting the facial features.
    face_features = get_face_features(kinect_folder);

    % Getting the prompts.
    prompts = get_prompts(labels_fn);

    % Creating and saving the struct.
    disp('saving the struct');
    all_features = struct();
    all_features.eeg_features = eeg_features;
    all_features.wav_features = wav_features;
    all_features.face_features = face_features;
    all_features.feature_labels = feature_labels;
    all_features.face_labels = extract_face_labels();
    all_features.prompts = prompts;
    if ICA && ~simple
        save([folder '/all_features_ICA.mat'], 'all_features');
    elseif ~simple
        save([folder '/all_features_noICA.mat'], 'all_features');
    else
        save([folder '/all_features_simple.mat'], 'all_features');
    end
end
