classdef instrumentationvisualizer
    %

    % Copyright 2018-2023 The MathWorks, Inc.

    methods (Access=private, Static)
        function visualize(data, varargs)
            p = inputParser;
            addParameter(p, 'sortBy', "Start time")
            addParameter(p, 'sortOrder', "Asc");
            addParameter(p, 'minDuration', 0.001);
            addParameter(p, 'nameFilter', "");
            parse(p,varargs{:});
            %sorting must be done firs
            sortBy = p.Results.sortBy;
            sortOrder = p.Results.sortOrder;
            minDuration = p.Results.minDuration;
            nameLikeFilter = p.Results.nameFilter;
            displayData = {data.display};
            duration = [data.duration];
            %Create the filter.
            filter = duration > minDuration;
            if(~strcmp(nameLikeFilter , ""))
                filter = filter & contains(displayData, nameLikeFilter);
            end
            data = data(filter);
            if(strcmp(sortOrder, "Asc"))
                if(strcmp(sortBy, "Name"))
                    [~,index] = sortrows({data.display}.'); data = data(index); clear index;
                elseif (strcmp(sortBy, "Start time"))
                    [~,index] = sortrows([data.start_time_point].'); data = data(index); clear index;
                elseif (strcmp(sortBy, "Stop time"))
                    [~,index] = sortrows([data.stop_time_point].'); data = data(index); clear index;
                elseif (strcmp(sortBy, "Duration"))
                    [~,index] = sortrows([data.duration].'); data = data(index); clear index;
                elseif (strcmp(sortBy, "Source Name"))
                    [~,index] = sortrows([data.source_name].'); data = data(index); clear index;
                end
            else
                if (strcmp(sortBy, "Name"))
                    [~,index] = sortrows({data.display}.'); data = data(index(end:-1:1)); clear index;
                elseif (strcmp(sortBy, "Start time"))
                    [~,index] = sortrows([data.start_time_point].'); data = data(index(end:-1:1)); clear index;
                elseif (strcmp(sortBy, "Stop time"))
                    [~,index] = sortrows([data.stop_time_point].'); data = data(index(end:-1:1)); clear index;
                elseif (strcmp(sortBy, "Duration"))
                    [~,index] = sortrows([data.duration].'); data = data(index(end:-1:1)); clear index;
                elseif (strcmp(sortBy, "Source Name"))
                    [~,index] = sortrows([data.source_name].'); data = data(index(end:-1:1)); clear index;
                end
            end
            generateTimelineGraph(data);
        end
    end
    methods (Static)
        function start()
            performance.instrument.controls.enableAll;
            performance.instrument.recorder.start;
            performance.instrument.externalEventsAggregator.start;
            performance.instrument.externalEventsAggregator.refreshActiveClients;
        end
        function stop()
            performance.instrument.externalEventsAggregator.collectEvents;
            performance.instrument.externalEventsAggregator.stop;
            performance.instrument.recorder.stop;
            performance.instrument.controls.disableAll;
        end
        function stopAndVisualize(varargin)
            instrumentationvisualizer.stop();
            data = performance.instrument.recorder.getAllEvents;
            instrumentationvisualizer.visualize(data,varargin);
        end
        function data = StopAndSave()
            instrumentationvisualizer.stop();
            data = performance.instrument.recorder.getAllEvents;
        end
        function loadAndVisualize(data, varargin)
            instrumentationvisualizer.visulaize(data, varargin);
        end
        function clearData()
            performance.instrument.recorder.clearAllEventData;
        end
        
    end
end
