classdef LetStatmentsDraw < GUIBase
    %LETSTATEMENTSDRAW Summary of this function goes here
    %   Detailed explanation goes here
    
    properties
        my_data = [];
        
        my_controls = {};
        
        gui_params = [];
        
        pb_delete = -1;
        pb_new = -1;
    end
    
    methods
        function object = LetStatmentsDraw(data, gui_params)
            if nargin == 1
                gui_params = GUIParameters();
            end
            
            object.my_data = data;
            object.gui_params = gui_params;
            
            data.addlistener('StructureChanged', @(src, event)redraw_and_update_bb(object, src, event));
        end
        
        function draw_onto(object, parent_handle)
            [~, y] = object.get_bounding_box();
            
            for i = object.my_data.size()+1:size(object.my_controls, 2)
                delete(object.my_controls{object.my_data.size()+1}.n);
                delete(object.my_controls{object.my_data.size()+1}.a);
                object.my_controls(object.my_data.size()+1) = []
            end
            
            for i = 1:object.my_data.size()
                if size(object.my_controls, 2) < i
                    object.my_controls{i} = struct();
                    object.my_controls{i}.n = uicontrol(object.phandle, 'Style', 'edit');
                    object.my_controls{i}.a = uicontrol(object.phandle, 'Style', 'edit');
                elseif ~ishandle(object.my_controls{i}.n)
                    object.my_controls{i}.n = uicontrol(object.phandle, 'Style', 'edit');
                    object.my_controls{i}.a = uicontrol(object.phandle, 'Style', 'edit');
                end
                [n, a] = object.my_data.get_statement(i);
                c = object.my_controls{i}.n;
                set(c, 'Parent', object.phandle);
                set(c, 'String', n);
                set(c, 'Position', [0, y-((i)*object.gui_params.edit_height), object.gui_params.edit_width, object.gui_params.edit_height]);
                set(c, 'Callback', @(src, event)object.my_data.set_statement_name(i, get(src, 'String')));
                
                c = object.my_controls{i}.a
                set(c, 'Parent', object.phandle);
                set(c, 'String', a);
                set(c, 'Position', [object.gui_params.edit_width+20, y-((i)*object.gui_params.edit_height), object.gui_params.edit_width*2, object.gui_params.edit_height]);
                set(c, 'Callback', @(src, event)object.my_data.set_statement_assignment(i, get(src, 'String')));
                
            end
            
            if ishandle(object.pb_delete)
                pb_delete = object.pb_delete;
            else
                pb_delete = uicontrol(parent_handle, 'Style', 'pushbutton', 'String', 'Remove', 'Callback', @(src, event)object.my_data.delete_end());
                object.pb_delete = pb_delete;
            end

            if ishandle(object.pb_new)
                pb_new = object.pb_new;
            else
                pb_new = uicontrol(parent_handle, 'Style', 'pushbutton', 'String', 'Add', 'Callback', @(src, event)object.my_data.insert_space());
                object.pb_new = pb_new;
            end

            set(pb_delete, 'Position', [80, 0, 80, 20]);
            set(pb_new, 'Position', [0, 0, 80, 20]);
        end
        
        function [x, y] = get_bounding_box(object)
            x = object.gui_params.edit_width+20+object.gui_params.edit_width*2;
            
            y = object.gui_params.edit_height*object.my_data.size() + 20;
        end
    end
end

