%% generate_eml_cond
%    generate the embedded matlab for a conditon grid, recursive
%    function, will call horizontal generator if finds conditions
%    in that grid.
% inputs:
%   object:EMLGenerator - current object
%   g1:Grid - Grid 1
%   g2:Grid - Grid 2
%   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_cond(object,g1,g2,depth)




space = '';
for i=1:depth
    space = [space sprintf('  ')];
end
g1cond1 = g1.cells(1).cond_text;
g2cond1 = g2.cells(1).cond_text;
code = [];
%1D horizontal
if(~isempty(g1cond1) && isempty(g2cond1))
    if object.data.multi_mode == 0
        code = [code object.generate_eml_horizontal(g1,g2.cells(1),depth)];
    else
        code = [code object.generate_eml_multi(g1,g2.cells(1),depth)];
    end
    % something in vertical column
else
    
    elsecell = [];
    for i=1:size(g2.cells,2)
        g2cond = g2.cells(i).cond_text;
        % 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(g2cond,'otherwise'))
            elsecell = g2.cells(i);
            found = 1;
            continue
        end
        if (i == 1)
            code = [code sprintf('%sif(%s)\n',space,(strtrim(char(g2.cells(i).cond_text))))];
        else
            code = [code sprintf('%selseif(%s)\n',space,(strtrim(char(g2.cells(i).cond_text))))];
        end
        
        if(~isempty(g2.cells(i).subgrid))
            code = [code object.generate_eml_cond(g1,g2.cells(i).subgrid,depth+1)];
        else
            if (~isempty(g1cond1))
                if object.data.multi_mode == 0
                    code = [code object.generate_eml_horizontal(g1,g2.cells(i),depth+1)];
                else
                    code = [code object.generate_eml_multi(g1,g2.cells(i),depth+1)];
                end
                %code = [code object.generate_eml_horizontal(g1,g2.cells(i),depth+1)];
            else
                
                resultcell = object.data.Grid0.search_return(g1.cells(1),g2.cells(i));
                if(~isempty(resultcell))
                    code = [code sprintf('%soutput = %s;\n',[space '  '],EMLGenerator.type_convert('output',object.datatype,strtrim(char(resultcell.result_text))))];
                    
                else
                end
            end
        end
        
    end
    if(~isempty(elsecell))
        %resultcell = object.Grid0.search_return(elsecell,elsecell);
        code = [code sprintf('%selse\n',space)];
        
        if(~isempty(elsecell.subgrid))
            code = [code object.generate_eml_cond(g1,elsecell.subgrid,depth) sprintf('\n')];
        else
            if (~isempty(g1cond1))
                code = [code object.generate_eml_horizontal(g1,elsecell,depth) sprintf('\n')];
            else
                
                resultcell = object.data.Grid0.search_return(g1.cells(1),elsecell);
                if(~isempty(resultcell))
                    code = [code sprintf('%soutput = %s;\n',[space '  '],EMLGenerator.type_convert('output',object.datatype,strtrim(char(resultcell.result_text))))];
                    
                end
            end
        end
        
    end
    
    code = [code sprintf('%send\n',space)];
    
end
end