%% generate_preamble
%    generates the preamble for the eml code, the preamble is
%    everything before the first if statement, this is seperated
%    out to make the code simpler.
% inputs:
%   object:EMLGenerator - current object
% outputs:
%   code:string - string of eml code
% Author: Colin Eles elesc@mcmaster.ca
% Organization: McMaster Centre for Software Certification
function code = generate_preamble(object)

code = [];
function_name = EMLGenerator.parse_inputs(object.data.function_name);
%generate input list
parsed_input = EMLGenerator.parse_inputs(object.data.function_inputs);
input = [];
for i= 1:size(parsed_input,2)
    input = [input char(parsed_input{i}(1))];
    if i ~= size(parsed_input,2)
        input = [input ','];
    end
end

output = [];
if (object.data.multi_mode == 1)
    for i=1:size(object.data.Grid1.cells,2)
        parsed_output = EMLGenerator.parse_inputs(strtrim(char(object.data.Grid1.cells(i).cond_text)));
        
        output = [output char(parsed_output{1}(1))];
        
        if i ~= size(object.data.Grid1.cells,2)
            output = [output ','];
        end
    end
else
    output = 'output';
end

code = sprintf('function [%s] = %s(%s)\n%s\n',output,char(function_name{1}(1)),input,'%%#eml');
% simulink forces you to have an output for all execution paths
% since it can't compute completness and disjointness we need
% to have a default value, if the user builds the table
% properly the default value will never be used. since
% different types might have a different default value, the
% temporary solution is just to use one of the outputs from our
% table, we will use the first cell because it is
% guaranteed to
% be filled in, regardless of the dimensionality of the table.

if (object.data.multi_mode == 1)
    for i=1:size(object.data.Grid1.cells,2)
        parsed_output = EMLGenerator.parse_inputs(strtrim(char(object.data.Grid1.cells(i).cond_text)));
        
        output_str = char(parsed_output{1}(1));
        
        code = [code sprintf('%s=%s;\n',output_str,EMLGenerator.type_convert(output_str,object.datatype,char(object.data.Grid0.Cells(i).result_text)))];
        
    end
    
else
    test  = 'output';
    output_string = EMLGenerator.type_convert(test,object.datatype,char(object.data.Grid0.Cells(1).result_text) );
    code = [code sprintf('output=%s;\n', output_string )];
    
end

end

