% Author: Matthew Dawson matthew@mjdsystems.ca
% Organization: McMaster Centre for Software Certification
classdef HierarchicalGrid < handle
    % This class stores a grid containing conditions for the table.
    % It can be used either for the left of top grid.
    
    properties
        grid = {}
        right_most_grid_cells = {}
    end
    
    events
        AddedOuterCell
        RemovedOuterCell
        StructureChanged
    end
    
    methods
        
        %% Data
        %   constructor
        % inputs:
        %   none
        % outputs:
        %   object:Data - created object
        function object = HierarchicalGrid()
            
        end
        
        function width = max_width(object)
            width = object.max_width_impl(object.grid, 1);
        end
        
        function length = get_children_count(object, cell)
            if isempty(cell)
                grid = object.grid;
            else
                grid = cell.grid;
            end
            
            length = size(grid, 2);
        end
        
        function count = subgrid_count(object)
            count = object.subgrid_count_impl(object.grid);
        end
        
        function count = subgrid_count_impl(object, grid)
            count = 0;
            
            for i = 1:size(grid, 2)
                ecount = object.subgrid_count_impl(grid{i}.grid);
                count = count + ecount;
            end
            
            if size(grid, 2) ~= 0
                count = count + 1;
            end
        end
        
        function cell = get_cell(object, cell_index)
            cell = object.get_child_cell([], cell_index);
        end
        
        function cell = get_child_cell(object, parent, cell_index)
            if isempty(parent)
                grid = object.grid
            else
                grid = parent.grid
            end
            
            cell = grid{cell_index}
        end
        
        function cell = recurse_top_cell(object, the_cell)
            cell = the_cell
            while size(cell.grid, 2) ~= 0
                cell = cell.grid{1};
            end
        end
        
        function count = children_count(object, parent)
            if isempty(parent)
                parent = object
            end
            
            count = size(parent.grid, 2);
        end
        
        function cell = recurse_bottom_cell(object, the_cell)
            cell = the_cell
            while size(cell.grid, 2) ~= 0
                cell = cell.grid{size(cell.grid, 2)};
            end
        end
        
    end
    
    
end
