classdef InstrumentationVisualizer < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        InstrumentVisualizerUIFigure    matlab.ui.Figure
        VisualizePanel                  matlab.ui.container.Panel
        NameFilterEditField             matlab.ui.control.EditField
        NameFilterEditFieldLabel        matlab.ui.control.Label
        SortbyDropDown                  matlab.ui.control.DropDown
        SortbyDropDownLabel             matlab.ui.control.Label
        minDurationEditField            matlab.ui.control.NumericEditField
        minDurationEditFieldLabel       matlab.ui.control.Label
        ResettoDefaultButton            matlab.ui.control.Button
        DropDown                        matlab.ui.control.DropDown
        PlotButton                      matlab.ui.control.Button
        ProfilerLabel                   matlab.ui.control.Label
        ClearDataButton                 matlab.ui.control.Button
        StopButton                      matlab.ui.control.Button
        StartButton                     matlab.ui.control.Button
        DataSourceDropDown              matlab.ui.control.DropDown
        DataSourceDropDownLabel         matlab.ui.control.Label
        InstrumentationVisualizerLabel  matlab.ui.control.Label
    end

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

        % Button pushed function: PlotButton
        function PlotButtonPushed(app, event)
            instrumentationvisualizer.stopAndVisualize('sortBy', app.SortbyDropDown.Value,...
                                 'sortOrder', app.DropDown.Value,...
                                 'minDuration', app.minDurationEditField.Value,...
                                 'nameFilter', app.NameFilterEditField.Value);
        end

        % Button pushed function: StartButton
        function StartButtonPushed(app, event)
            if(strcmp(app.DataSourceDropDown.Value,"Recorder"))
                app.StartButton.Enable = false;
                instrumentationvisualizer.start;
                app.StopButton.Enable = true;
            end
        end

        % Button pushed function: StopButton
        function StopButtonPushed(app, event)
            if(strcmp(app.DataSourceDropDown.Value,"Recorder"))
                app.StopButton.Enable = false;
                instrumentationvisualizer.stop;
                app.StartButton.Enable = true;
            end
        end

        % Button pushed function: ClearDataButton
        function ClearDataButtonPushed(app, event)
            instrumentationvisualizer.clearData;
        end

        % Button pushed function: ResettoDefaultButton
        function ResettoDefaultButtonPushed(app, event)
            app.minDurationEditField.Value = 0.001;
            app.SortbyDropDown.Value = "Start time";
            app.DropDown.Value = "Asc";
            app.NameFilterEditField.Value = "";
            
        end
    end

    % Component initialization
    methods (Access = private)

        % Create UIFigure and components
        function createComponents(app)

            % Create InstrumentVisualizerUIFigure and hide until all components are created
            app.InstrumentVisualizerUIFigure = uifigure('Visible', 'off');
            app.InstrumentVisualizerUIFigure.Position = [100 100 442 363];
            app.InstrumentVisualizerUIFigure.Name = 'Instrument Visualizer';

            % Create InstrumentationVisualizerLabel
            app.InstrumentationVisualizerLabel = uilabel(app.InstrumentVisualizerUIFigure);
            app.InstrumentationVisualizerLabel.Position = [150 325 143 22];
            app.InstrumentationVisualizerLabel.Text = 'Instrumentation Visualizer';

            % Create DataSourceDropDownLabel
            app.DataSourceDropDownLabel = uilabel(app.InstrumentVisualizerUIFigure);
            app.DataSourceDropDownLabel.HorizontalAlignment = 'right';
            app.DataSourceDropDownLabel.Position = [40 286 72 22];
            app.DataSourceDropDownLabel.Text = 'Data Source';

            % Create DataSourceDropDown
            app.DataSourceDropDown = uidropdown(app.InstrumentVisualizerUIFigure);
            app.DataSourceDropDown.Items = {'Recorder'};
            app.DataSourceDropDown.Position = [127 286 140 22];
            app.DataSourceDropDown.Value = 'Recorder';

            % Create StartButton
            app.StartButton = uibutton(app.InstrumentVisualizerUIFigure, 'push');
            app.StartButton.ButtonPushedFcn = createCallbackFcn(app, @StartButtonPushed, true);
            app.StartButton.Position = [127 243 67 22];
            app.StartButton.Text = 'Start';

            % Create StopButton
            app.StopButton = uibutton(app.InstrumentVisualizerUIFigure, 'push');
            app.StopButton.ButtonPushedFcn = createCallbackFcn(app, @StopButtonPushed, true);
            app.StopButton.Enable = 'off';
            app.StopButton.Position = [205 243 64 22];
            app.StopButton.Text = 'Stop';

            % Create ClearDataButton
            app.ClearDataButton = uibutton(app.InstrumentVisualizerUIFigure, 'push');
            app.ClearDataButton.ButtonPushedFcn = createCallbackFcn(app, @ClearDataButtonPushed, true);
            app.ClearDataButton.Position = [286 243 102 22];
            app.ClearDataButton.Text = 'Clear Data';

            % Create ProfilerLabel
            app.ProfilerLabel = uilabel(app.InstrumentVisualizerUIFigure);
            app.ProfilerLabel.HorizontalAlignment = 'right';
            app.ProfilerLabel.Position = [65 243 47 22];
            app.ProfilerLabel.Text = 'Profiler';

            % Create VisualizePanel
            app.VisualizePanel = uipanel(app.InstrumentVisualizerUIFigure);
            app.VisualizePanel.Title = 'Visualize';
            app.VisualizePanel.Position = [28 38 382 195];

            % Create PlotButton
            app.PlotButton = uibutton(app.VisualizePanel, 'push');
            app.PlotButton.ButtonPushedFcn = createCallbackFcn(app, @PlotButtonPushed, true);
            app.PlotButton.Position = [101 13 75 22];
            app.PlotButton.Text = 'Plot';

            % Create DropDown
            app.DropDown = uidropdown(app.VisualizePanel);
            app.DropDown.Items = {'Asc', 'Desc'};
            app.DropDown.Position = [193 88 46 22];
            app.DropDown.Value = 'Asc';

            % Create ResettoDefaultButton
            app.ResettoDefaultButton = uibutton(app.VisualizePanel, 'push');
            app.ResettoDefaultButton.ButtonPushedFcn = createCallbackFcn(app, @ResettoDefaultButtonPushed, true);
            app.ResettoDefaultButton.Position = [256 46 102 22];
            app.ResettoDefaultButton.Text = 'Reset to Default';

            % Create minDurationEditFieldLabel
            app.minDurationEditFieldLabel = uilabel(app.VisualizePanel);
            app.minDurationEditFieldLabel.HorizontalAlignment = 'right';
            app.minDurationEditFieldLabel.Position = [17 130 70 22];
            app.minDurationEditFieldLabel.Text = 'minDuration';

            % Create minDurationEditField
            app.minDurationEditField = uieditfield(app.VisualizePanel, 'numeric');
            app.minDurationEditField.Position = [102 130 137 22];
            app.minDurationEditField.Value = 0.001;

            % Create SortbyDropDownLabel
            app.SortbyDropDownLabel = uilabel(app.VisualizePanel);
            app.SortbyDropDownLabel.HorizontalAlignment = 'right';
            app.SortbyDropDownLabel.Position = [41 88 44 22];
            app.SortbyDropDownLabel.Text = 'Sort by';

            % Create SortbyDropDown
            app.SortbyDropDown = uidropdown(app.VisualizePanel);
            app.SortbyDropDown.Items = {'Start time', 'Stop time', 'Duration', 'Source Name', 'Name'};
            app.SortbyDropDown.Position = [100 88 76 22];
            app.SortbyDropDown.Value = 'Start time';

            % Create NameFilterEditFieldLabel
            app.NameFilterEditFieldLabel = uilabel(app.VisualizePanel);
            app.NameFilterEditFieldLabel.HorizontalAlignment = 'right';
            app.NameFilterEditFieldLabel.Position = [18 46 68 22];
            app.NameFilterEditFieldLabel.Text = 'Name Filter';

            % Create NameFilterEditField
            app.NameFilterEditField = uieditfield(app.VisualizePanel, 'text');
            app.NameFilterEditField.Position = [101 46 138 22];

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

    % App creation and deletion
    methods (Access = public)

        % Construct app
        function app = InstrumentationVisualizer

            % Create UIFigure and components
            createComponents(app)

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

            if nargout == 0
                clear app
            end
        end

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

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