% Author: Matthew Dawson matthew@mjdsystems.ca
% Organization: McMaster Centre for Software Certification

function delete_cell( object, beneath_cell, the_cell )
%% ADD_CELL Add a cell to the grid.
%   Adds a new cell to the grid, updates relevant information throughout,
%   and sends appropriate update signals.
%assert(false, 'BREAK');
    %% Find Subgrid
    if isempty(beneath_cell)
        subgrid = object;
    else
        subgrid = beneath_cell;
    end
    
    if true || isempty(beneath_cell) && isempty(the_cell.grid)
        cur_index = object.find_cell_in_list(subgrid.grid, the_cell);
        
        top_rmg = object.recurse_top_cell(the_cell);
        top_rmgi = object.find_cell_in_list(object.right_most_grid_cells, top_rmg);
        
        bottom_rmg = object.recurse_bottom_cell(the_cell);
        bottom_rmgi = object.find_cell_in_list(object.right_most_grid_cells, bottom_rmg);
        
        subgrid.grid = [subgrid.grid(1:cur_index-1) subgrid.grid(cur_index+1:size(subgrid.grid, 2))];
        
        %For readability, use rmgc.
        rmgc = object.right_most_grid_cells;
        object.right_most_grid_cells = [rmgc(1:top_rmgi-1) rmgc(bottom_rmgi+1:size(rmgc, 2))];
        
        if ~isempty(beneath_cell) && size(beneath_cell.grid, 2) == 0
            rmgc = object.right_most_grid_cells
            object.right_most_grid_cells = [rmgc(1:top_rmgi-1) {beneath_cell} rmgc(top_rmgi:size(rmgc, 2))];
            top_rmgi = top_rmgi + 1;
        end
        for i = 0:bottom_rmgi-top_rmgi
            notify(object, 'RemovedOuterCell', GridSingleCellEventDetails(top_rmgi))
        end
    else
        assert(false, 'Deal with sub-sub* grids');
    end

    notify(object, 'StructureChanged');

end

