%% genereate_pvs
%   function will generate the pvs conditional code for a given
%   table
% inputs:
%   object:PVS_checker - PVS_checker object
%   g1:Grid - left grid
%   g2:Grid - top grid
%   depth:int - number representing depth inside the table
% outputs:
%   code:string - pvs code
% Author: Colin Eles elesc@mcmaster.ca
% Organization: McMaster Centre for Software Certification
function code = generate_pvs(object,g1,g2,depth)
g1cond1 = g1.cells(1).cond_text;
g2cond1 = g2.cells(1).cond_text;
code = [];
%1D horizontal
if(~isempty(g1cond1) && isempty(g2cond1))
    code = [code object.generate_pvs_horizontal(g1,g2.cells(1),depth)];
    % something in vertical column
else
    
    code = [code sprintf('COND\n')];
    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
        code = [code sprintf('%s->',object.matlab_to_pvs_syntax_translation(strtrim(char(g2.cells(i).cond_text))))];
        
        if(~isempty(g2.cells(i).subgrid))
            code = [code object.generate_pvs(g1,g2.cells(i).subgrid,depth)];
        else
            if (~isempty(g1cond1))
                if (object.data.multi_mode == 0)
                    code = [code object.generate_pvs_horizontal(g1,g2.cells(i),depth)];
                else
                    code = [code object.generate_pvs_multi_output(g1,g2.cells(i),depth)];
                end
            else
                
                resultcell = object.data.Grid0.search_return(g1.cells(1),g2.cells(i));
                if(~isempty(resultcell))
                    code = [code object.matlab_to_pvs_syntax_translation(strtrim(char(resultcell.result_text)))];
                    
                    
                else
                end
            end
        end
        if(isempty(elsecell))
            if(i~= size(g2.cells,2))
                code = [code sprintf(',\n')];
            else
                code = [code sprintf('\n')];
            end
        else
            code = [code sprintf(',\n')];
        end
        
    end
    if(~isempty(elsecell))
        %resultcell = object.Grid0.search_return(elsecell,elsecell);
        code = [code sprintf('ELSE->')];
        
        if(~isempty(elsecell.subgrid))
            code = [code object.generate_pvs(g1,elsecell.subgrid,depth) sprintf('\n')];
        else
            if (~isempty(g1cond1))
                if (object.data.multi_mode == 0)
                    code = [code object.generate_pvs_horizontal(g1,elsecell,depth) sprintf('\n')];
                else
                    code = [code object.generate_pvs_multi_output(g1,elsecell,depth) sprintf('\n')];
                    
                end
            else
                
                resultcell = object.data.Grid0.search_return(g1.cells(1),elsecell);
                if(~isempty(resultcell))
                    code = [code object.matlab_to_pvs_syntax_translation(strtrim(char(resultcell.result_text))) sprintf('\n')];
                    
                end
            end
        end
        
    end
    
    code = [code sprintf('ENDCOND')];
    
end
end


