classdef Guide < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        RootFigure            matlab.ui.Figure
        help_textarea         matlab.ui.control.TextArea
        ContextualguideLabel  matlab.ui.control.Label
        ManualButton          matlab.ui.control.Button
    end

    
    properties (Access = private)
        application % Description
        manager
        
        data_change_listener
    end
    
    methods (Access = private)
        
        function handle_view_changed(app, src, ev)
            % update when data changes so we can check for changes to guide
            % text:
            app.manager = ev.viewmanager;
            if isempty(app.manager)
                return
            end
            
            delete(app.data_change_listener);
            app.data_change_listener = ...
                addlistener(app.manager.data, "on_data_changed", @(~,~) app.handle_data_changed());
        end
        
        function handle_data_changed(app)
            app.help_textarea.Value = app.manager.data.read("guide_text");
        end
    end
    

    % Callbacks that handle component events
    methods (Access = private)

        % Code that executes after component creation
        function startupFcn(app, application)
            app.application = application;
            addlistener(application, "on_view_changed", @app.handle_view_changed);
        end

        % Close request function: RootFigure
        function handle_close(app, event)
            app.RootFigure.Visible = false;
        end
    end

    % Component initialization
    methods (Access = private)

        % Create UIFigure and components
        function createComponents(app)

            % Create RootFigure and hide until all components are created
            app.RootFigure = uifigure('Visible', 'off');
            app.RootFigure.Color = [1 1 1];
            app.RootFigure.Position = [100 100 308 374];
            app.RootFigure.Name = 'MATLAB App';
            app.RootFigure.CloseRequestFcn = createCallbackFcn(app, @handle_close, true);

            % Create help_textarea
            app.help_textarea = uitextarea(app.RootFigure);
            app.help_textarea.Position = [9 7 295 330];

            % Create ContextualguideLabel
            app.ContextualguideLabel = uilabel(app.RootFigure);
            app.ContextualguideLabel.FontSize = 16;
            app.ContextualguideLabel.FontWeight = 'bold';
            app.ContextualguideLabel.Position = [9 345 135 22];
            app.ContextualguideLabel.Text = 'Contextual guide';

            % Create ManualButton
            app.ManualButton = uibutton(app.RootFigure, 'push');
            app.ManualButton.Position = [197 345 100 22];
            app.ManualButton.Text = 'Manual';

            % Show the figure after all components are created
            app.RootFigure.Visible = 'on';
        end
    end

    % App creation and deletion
    methods (Access = public)

        % Construct app
        function app = Guide(varargin)

            % Create UIFigure and components
            createComponents(app)

            % Register the app with App Designer
            registerApp(app, app.RootFigure)

            % Execute the startup function
            runStartupFcn(app, @(app)startupFcn(app, varargin{:}))

            if nargout == 0
                clear app
            end
        end

        % Code that executes before app deletion
        function delete(app)

            % Delete UIFigure when app is deleted
            delete(app.RootFigure)
        end
    end
end