%% draw_grid2
%    Draws the grid on the left onto the figure.
% inputs:
%   obj:GUI - current GUI object
%   grid:Grid - grid to be drawn, should be grid on left.
%   load:tristate - 0 if grid is being refreshed, 1 if grid should reload
%                   data, 2 if the grid is being initialized.
% outputs:
%   none
% Author: Colin Eles elesc@mcmaster.ca
% Organization: McMaster Centre for Software Certification
function [] = draw_grid2(object,grid,load)
if (~isempty(grid))
    % generate a random pastel colour for the background, each
    % grid has a different colour to help with readability
    grid_color = [0.9+0.1*rand(1) 0.9+0.1*rand(1) 0.9+0.1*rand(1)];
    for i=1:size(grid.cells,2)
        % check if the grid is the root grid
        if (~isempty(grid.parent_cell))
            % grid is not the root grid location is derived
            % from its parent.
            parent_pos = grid.parent_cell.get_pos;
            if i == 1
                % if we are at the first cell, location is the
                % same as the parent but shifted to the right
                % by one cell
                pos(3) = grid.cells(i).width * grid.cells(i).condition_text_width;
                pos(4) = grid.cells(i).height * grid.cells(i).condition_text_height;
                pos(1) = parent_pos(1) + parent_pos(3);
                pos(2) = parent_pos(2) + parent_pos(4) - pos(4);
            else
                % if we are not at the first cell, location is
                % the same as the previous cell but we shift
                % down by one cell
                prev_pos = grid.cells(i-1).get_pos;
                pos(3) = grid.cells(i).width * grid.cells(i).condition_text_width;
                pos(4) = grid.cells(i).height * grid.cells(i).condition_text_height;
                pos(1) = parent_pos(1) + parent_pos(3);
                pos(2) = prev_pos(2) - pos(4);
                grid_color = grid.cells(i-1).color;
                
            end
        else
            % grid is the root grid.
            % position of the first cell is based on
            % condition_text_x, and condition_text_y, as well
            % as the height of the figure and header.
            figpos = get(object.fig,'position');
            pos = [grid.cells(i).condition_text_x figpos(4)-object.header_height-grid.cells(i).condition_text_y grid.cells(i).condition_text_width grid.cells(i).condition_text_height];
            if i == 1
                pos(1) = pos(1);
                pos(4) = grid.cells(i).height * grid.cells(i).condition_text_height;
                
                pos(2) = pos(2)-pos(4)-grid.cells(i).condition_text_height-grid.cells(i).condition_text_offset;
                pos(3) = grid.cells(i).width * grid.cells(i).condition_text_width;
                pos(4) = grid.cells(i).height * grid.cells(i).condition_text_height;
            else
                prev_pos = grid.cells(i-1).get_pos;
                pos(3) = grid.cells(i).width * grid.cells(i).condition_text_width;
                pos(4) = grid.cells(i).height * grid.cells(i).condition_text_height;
                pos(1) = pos(1);
                pos(2) = prev_pos(2) - pos(4);
                grid_color = grid.cells(i-1).color;
            end
        end
        
        
        % if the edit box does not exist, ie. it is just being
        % created or we are loading the figure
        if (load == 2 || isempty(grid.cells(i).cond) || ~ishandle(grid.cells(i).cond))
            % create the new edit box
            grid.cells(i).cond = object.create_std_text(object.fig,pos);
            % if cell is the first in the grid use the new
            % colour, else take the colour of the previous cell
            % in the grid.
            if(i==1)
                set(grid.cells(i).cond,'BackgroundColor',grid_color);
            else
                set(grid.cells(i).cond,'BackgroundColor',get(grid.cells(i-1).cond,'BackgroundColor'))
            end
            grid.cells(i).color = grid_color;
            % if we are loading the cell set the string of the
            % edit box to be the value that we saved as a
            % result of calling save_conditions
            if (load ~= 0)
                set(grid.cells(i).cond,'String',grid.cells(i).cond_text);
            end
        else
            % if the edit box is already created change is
            % position to newly determined position.
            grid.cells(i).set_pos(pos);
        end
        % determine if we want to draw the new subgrid buttons,
        % which is located to the right of each of the leaf
        % cells.
        if(grid.cells(i).pb_flag == 1 && object.edit == 1)
            pb_pos = pos;
            pb_pos(1) = pos(1)+pos(3);
            pb_pos(3) = grid.cells(i).grid_push_width;
            if (ishghandle(grid.cells(i).grid_pb))
                set(grid.cells(i).grid_pb,'Visible','on');
            end
            grid.cells(i).set_pb(object.fig,pb_pos);
            set(grid.cells(i).grid_pb,'UserData',object);
        elseif(grid.cells(i).pb_flag == 1 && object.edit ~= 1)
            if (ishghandle(grid.cells(i).grid_pb))
                set(grid.cells(i).grid_pb,'Visible','off');
            end
        end
        
        delete_pb_pos = [];
        
        
        % detemine if we want to draw the new cell buttons,
        % which are located at the botton of the set of cells.
        if (i == size(grid.cells,2) && object.edit == 1)
            new_pb_pos = pos;
            new_pb_pos(4) = grid.cells(i).condition_text_height/2;
            new_pb_pos(3) = pos(3)/2;
            new_pb_pos(2) = pos(2) - new_pb_pos(4);
            if (ishghandle(grid.new_cell_pb))
                set(grid.new_cell_pb,'Visible','on');
            end
            grid.set_pb(object.fig,new_pb_pos);
            set(grid.new_cell_pb,'userdata',object);
            
            
            delete_pb_pos = new_pb_pos;
            delete_pb_pos(1) = new_pb_pos(1) + new_pb_pos(3);
            if (ishghandle(grid.delete_cell_pb))
                set(grid.delete_cell_pb,'Visible','on');
            end
            grid.set_delete_pb(object.fig,delete_pb_pos);
            set(grid.delete_cell_pb,'userdata',object);
        elseif (i == size(grid.cells,2) && object.edit ~= 1)
            
            if (ishghandle(grid.new_cell_pb))
                set(grid.new_cell_pb,'Visible','off');
            end
            
            if (ishghandle(grid.delete_cell_pb))
                set(grid.delete_cell_pb,'Visible','off');
            end
        end
        
        
        
        % recursively draw the subgrid of the cell if it
        % exists.
        object.draw_grid2(grid.cells(i).subgrid,load);
        
        if(object.edit == 0)
            string = get(grid.cells(i).cond,'String');
            if(isempty(string))
                set(grid.cells(i).cond,'Visible','off');
            end
        else
            set(grid.cells(i).cond,'Visible','on');
        end
        
    end
    
    
end
end

