classdef Cell < handle
    
    
    properties
        subgrid = [];
        cond = [];
        cond_text = [];
        cell_index = 0;
        parent_grid = [];
        width = 0;
        height = 0;
        grid_pb = [];
        pb_flag = 0;
        color = [];
        
        condition_text_width = 200;
        condition_text_height = 60;
        condition_text_x = 10;
        condition_text_y = 10;
        condition_text_offset = 20;
        
        grid_push_width = 30;
    end
    
    methods
        %% Cell
        %    
        % inputs:
        %   index:integer - cells index
        %   p_grid:Grid - Parent grid of cell
        % outputs;
        %   obj:Cell - created cell
        function obj = Cell(index, p_grid)
            obj.cell_index = index;
            obj.parent_grid = p_grid;
            obj.pb_flag = 1;
            
        end
        
        %% new_Grid
        %    
        % inputs:
        %   obj:Cell - Current Cell Object
        % outputs;
        %   
        function [] = new_Grid(obj)
            obj.subgrid = Grid(obj.cell_index,obj);
            obj.subgrid.new_Cell;
            obj.subgrid.new_Cell;
            obj.subgrid.set_rGrid(obj.parent_grid.rGrid);
            obj.pb_flag = 0;
            delete(obj.grid_pb);
            if(~isempty(obj.parent_grid.rGrid))
                obj.parent_grid.rGrid.delete_g2s(obj);
            end
        end
        
        %% delete_Cell
        %    deletes the current cell, will recursively delete any subgrids
        %    if they exist.
        % inputs:
        %   obj:Cell - Current Cell Object         
        % outputs;
        %   none
        function [] = delete_Cell(obj)
            if(isempty(obj.subgrid))
                delete(obj.grid_pb);
                delete(obj.cond);
                obj.cond = [];
                obj.grid_pb = [];
                delete(obj);
            else
                % delete the subgrid
                obj.subgrid.deep_delete
                if(ishghandle(obj.grid_pb))
                    delete(obj.grid_pb);
                end
                delete(obj.cond);
                obj.cond = [];
                obj.grid_pb = [];
                delete(obj);
            end
        end
        
        %% set_pb
        %    create the new subgrid push button handle
        % inputs:
        %   obj:Cell - Current Cell Object  
        % outputs;
        %   none
        function [] = set_pb(obj,fig,pos)
            if(obj.pb_flag == 1 && (isempty(obj.grid_pb) || ~ishghandle(obj.grid_pb)))
            obj.grid_pb = uicontrol('style','push',...
                'units','pix',...
                'string','+',...
                'HorizontalAlign','left',...
                'Parent',fig,...
                'position',pos,...   
                'callback',@(src,event)pb_call(obj,src,event));
            elseif (obj.pb_flag == 1 && ~isempty(obj.grid_pb))
                    set(obj.grid_pb,'position',pos);
                else
                end
        end
        
        %% pb_call
        %    callback function for the new subgrid button
        % inputs:
        %   object:Cell - Current Cell Object
        %   src:double - Source of the callback
        %   event:eventdata - Data of the eventcall
        % outputs;
        %   none
        function [] = pb_call(object,src,event)
            gui = get(src,'userdata');
            object.new_Grid;
            gui.reset_wh();
            %gui.draw_grid2(gui.Grid2);
            gui.draw_allgrids(0);
        end
        
        %% cal_height
        %    calculates the height in number of cells of the current cell.
        %    if the cell does not have a subgrid the height will be 1, else
        %    it will be the height of the subgrid.
        % inputs:
        %   obj:Cell - Current Cell Object 
        %   edit:boolean - 0 when not in edit mode, 1 in edit mode
        % outputs;
        %   h:double - height in number of cells
        function h = cal_height(obj,edit)
            h = 0;
            if (isempty(obj.subgrid))
                h = 1;
            else
                for i=1:size(obj.subgrid.cells,2)
                    h = h + cal_height(obj.subgrid.cells(i),edit);
                end
                % edit button is half a cell high, if in edit mode increase
                % by 0.5
                if (edit == 1)
                    h = h + 0.5;
                end

            end
        end
        
        %% get_pos
        %    Returns the position of the edit box for the cell
        % inputs:
        %   obj:Cell - Current Cell Object  
        % outputs;
        %   pos:[double double double double] - position of the cell
        function pos = get_pos(obj)
            if (~isempty(obj.cond))
                pos = get(obj.cond,'position');
            end
        end
        
        %% set_pos
        %    allows accessor to set the position of the edit box for the
        %    cell.
        % inputs:
        %   obj:Cell - Current Cell Object 
        %   pos:[double double double double] - the new position of the
        %   cell
        % outputs;
        %   none
        function [] = set_pos(obj,pos)
            if (~isempty(obj.cond))
                set(obj.cond,'position',pos);
            end
        end
        
        %% flag_cell
        % mode
        %   0 - normal
        %   1 - red (error/false)
        %   2 - green (ok/true)
        function [] = flag_cell(obj,mode)
        
            if (isempty(obj.color))
                obj.color = get(obj.cond,'BackgroundColor');
            end
            
            if (mode == 0)
                set(obj.cond,'BackgroundColor',obj.color);
            elseif (mode == 1)
                set(obj.cond,'BackgroundColor',[0.92 0.65 0.65])
            elseif (mode == 2)
                set(obj.cond,'BackgroundColor',[0.03 1.0 0.32]);
            end
        
        end
        
        function new_cell = clone()
            
        end
    end
    
end

