%% check_grid
%    check_grid will recursively loop through all the cells of a
%    grid and call the check_matlab_syntax function for each of the
%    strings in the cells. if there is an error the cell will
%    change colours to alert the user where the problem is.
% inputs:
%   object:GUI - current GUI object
%   grid:Grid - Grid object being checked
% outputs:
%   none
% Author: Colin Eles elesc@mcmaster.ca
% Organization: McMaster Centre for Software Certification

%TODO: Merge this back into the grid itself
function msg = check_grid_condition(object,grid_container,grid)
if nargin == 2
    msg = check_grid_condition(object, grid_container, grid_container.grid);
    return ;
end

msg = [];
for i = 1:size(grid,2)
    
    error = '';
    string = grid{i}.get_matlab_string();
    
    % if the string is empty indicating that the table is 1
    % dimensional or the string is "otherwise" skip the syntax
    % checking.
    if(~isempty(string) && ~strcmp(string,'otherwise'))
        error = object.check_matlab_syntax_condition(char(string),0);
    end
    if isempty(error)
        if (strcmp(string,'') || isempty(string))
            error = 'Cell is empty';
        end
    end
    if ~isempty(error)
        % generate the message string
        msg = [msg sprintf('\n')];
        
        msg = [msg 'Condtion -> ' char(string) sprintf('\n')];
        msg = [msg error sprintf('\n')];
        
        % set tooltip string of cell to error msg
        % TODO Renenable
        % set(grid.cells(i).cond,'TooltipString',error);
        % change background colour
        % TODO Renenable
        % grid.cells(i).flag_cell(1);
    else
        % reset tooltip and colour if no error found
        % TODO Renenable
        % set(grid.cells(i).cond,'TooltipString','')
        % TODO Renenable
        % grid.cells(i).flag_cell(0);
        
        
    end
    % recurse through subgrid
    msg = [msg object.check_grid_condition(grid_container, grid{i}.grid)];
    
    
end
end
