%% save_call
%    callback function that is called whenever the save button is
%    pressed. This function will generate code and store it in the
%    embedded matlab block, it will then generate the corresponding
%    input and output ports and connect them to the embedded matlab
%    block.
% inputs:
%   object:GUI - current GUI object
%   src:double - source of the callback calling
%   event:eventdata - event that triggered the callback
% outputs:
%   none
% Author: Colin Eles elesc@mcmaster.ca
% Organization: McMaster Centre for Software Certification
function eml_handle = save_call(object,src,event)
if(object.check_call([],[]) == 1)
    return;
end

function_names = EMLGenerator.parse_inputs(get(object.function_name_control,'String'));
function_name = char(function_names{1}(1));
object.save_data;
if object.mode == 0
    model = gcs;
    %make sure we are not looking at the library model
    %create a new model
    found = any(ismember(find_system('type', 'block_diagram'),'Table'));
    if found == 0
        
        
        new_system('Table','Model');
        
    end
    model = 'Table';
    open_system(model);
    load_system('TableLibrary');
    new_block = add_block('TableLibrary/Tabular Expression',[model '/' function_name]);
    set_param(new_block,'UserData',object.Data);
    set_param(new_block, 'UserDataPersistent', 'on');
    object.mode = 1;
    object.block_handle = new_block;
    object.save_call([],[]);
elseif object.mode == 1
    
    load_system('simulink');
    % generate the code
    redo = object.check_output_consistent;
    if redo
        object.EMLGen.set_datatype(object.output_data_type);
    else
        object.EMLGen.set_datatype([]);
    end
    code = object.EMLGen.generate_eml_code;
    
    
    eml_handle = TableBlock.set_code(object.block_handle,code,function_name);
    
    
    
    if (object.mode == 1)
        
        TableBlock.set_block_display(object.block_handle,object.pvs_checked);
    end
    
    if ~redo
        object.save_call([],[]);
    end
    %[inputs, outputs] = object.compiled_types
end
end



