classdef DataManager < handle
    % DATAMANAGER Manages the data to be sent to the client. It works with the
    % ViewDataset to identify the requested data to send.
    
    % Copyright 2017-2018 The MathWorks, Inc.
    
    properties(Constant)
        SpreadsheetProperties = {'id','Class','Name','Run','objectClass','CompiledDT','SpecifiedDT','ProposedDT',...
            'DTGroup','Accept','SimMin_S','SimMax_S','DerivedMin_S','DerivedMax_S','DesignMin_S','DesignMax_S', ...
            'ProposedMin_S','ProposedMax_S', 'OverflowSaturation',...
            'OverflowWrap', 'hasProposedDT', 'hasDTGroup', 'hasOverflows', 'hiliteRowsWithIssues',...
            'IsReadOnly'};
        VisualizerProperties = {'HistogramVisualizationInfo'};
        NumericalProperties = {'SimMin','SimMax','DerivedMin','DerivedMax','DesignMin','DesignMax','ProposedMin','ProposedMax'};
        OtherProperties = {'hasInterestingInformation', 'runHasSimRange', 'runHasDeriveRange','runHasProposals',...
            'ExecutionOrder', 'DynamicRange', 'Magnitude',...
            'WordLength', 'IntegerLength', 'FractionLength'};
        HeaderNumberSpace = {'NumberSpace'};
        HeaderNumericalIssues = {'NumericalIssues'};
        HeaderSignedness = {'Signedness'};
        HeaderDataTypeForest = {'DataTypeForest'};
    end
    
    properties (SetAccess = private, GetAccess = private)
        ResultDatabase
        TableProperties = {};
        LastSortIndices = [];
        LastScopedTable = [];
        ExecutionOrderMap
        TableHelperDirector
        TableHelperBuilder
        ExecutionOrderRange
    end
    
    methods
        function this = DataManager
            this.TableProperties = [ ...
                this.SpreadsheetProperties ...
                this.NumericalProperties ...
                this.OtherProperties ...
                this.HeaderNumberSpace ...
                this.HeaderNumericalIssues ...
                this.HeaderSignedness ...
                this.HeaderDataTypeForest ...
                this.VisualizerProperties ...
                ];
            initData = double.empty(0, length(this.TableProperties));
            this.ResultDatabase = array2table(initData, 'VariableNames',this.TableProperties);
            this.ExecutionOrderRange = fxptui.Web.ExecutionOrderRange;
        end
        
        addResultsToTable(this, results);
        data = getData(this, query, subsystemIds);
        removeRunFromTable(this, runName);
        clearTable(this);
        flag = setExploreTabVisible(this, scopedTable, query);
        
        % Filter Table
        filteredTable = applyFilters(this, originalTable, query);
        selectedIndices = vectorizedFilter(this, originalTable, query, currentIndicies, activeHeaders);
        
    end
    
    % For testing purposes only.
    methods (Hidden)
        % Get the table object. This is a test API.
        table = getTable(this);
        
        % Get the index of the rowId given the last known sort order
        idx = getIndexOfRowId(this, rowId);
        
    end
    
end
