%% generate_eml_horizontal
%    generate the embedded matlab code for grid1, seperated from
%    the code for grid2 since for a 2d table grid1 conditions will
%    be sub conditions of grid2 conditions
% inputs:
%   object:EMLGenerator - current object
%   g1:Grid - Grid 1
%   g2_cell:Cell - Cell for which grid1 is sub conditon of
%   depth:int - integer representing recursion dept for formating
%   use
% outputs:
%   code:string - string of eml code
% Author: Colin Eles elesc@mcmaster.ca
% Organization: McMaster Centre for Software Certification
function code = generate_eml_horizontal(object,g1,g2_cell_index,depth)
space = '';
for i=1:depth
    space = [space sprintf('  ')];
end
code = [];

elsecell = [];
for i=1:g1.length
    g1cond = g1.get_cell(i).get_matlab_string;
    % condition string of otherwise corresponds to an else
    % statement, we allow this statement to be in any order
    % in the cells of the grid, so we need to find where it
    % is, if it exists.
    if (strcmp(g1cond,'otherwise'))
        elsecell = g1.cells(i);
        found = 1;
        continue
    end
    if (i == 1)
        code = [code sprintf('%sif(%s)\n',space,strtrim(char(g1cond)))];
    else
        code = [code sprintf('%selseif(%s)\n',space,strtrim(char(g1cond)))];
    end
    resultcell = object.data.outputs_grid.get_cell(g2_cell_index, i);
    if(~isempty(resultcell))
        code = [code sprintf('%soutput = %s;\n',[space '  '],EMLGenerator.type_convert('output',object.datatype,strtrim(char(resultcell.get_matlab_string))))];
        
    else
    end
end
if(~isempty(elsecell))
    resultcell = object.data.Grid0.search_return(elsecell,g2_cell);
    code = [code sprintf('%selse\n%soutput = %s;\n',space,[space '  '],EMLGenerator.type_convert('output',object.datatype,strtrim(char(resultcell.result_text))))];
    
end
code = [code sprintf('%send\n',space)];

end

