classdef AnalyzerController < handle
    % ANALYZERCONTROLLER communicates between Analyzer backend and FPT Web
    
    % Copyright 2018-2019 The MathWorks, Inc.
    
    properties(SetAccess = private, GetAccess=private)        
        AnalyzerUIReportPublishChannel = '/analyzer/getreport';
        AnalyzerRunAdvisorSubscribeChannel = '/analyzer/prepare';
        Subscriptions
        MsgServiceInterface
        DataManager
    end
    
    methods
        function this = AnalyzerController(uniqueID, msgServiceInterface)
            connector.ensureServiceOn;
            
            % Set the injected message service
            this.MsgServiceInterface = msgServiceInterface;
            
            % Set the data manager
            dataManager = fxptui.Web.Analyzer.AnalyzerDataManager();
            this.setDataManager(dataManager);
            
            % Setup channels 
            this.addIdToChannels(uniqueID);
            
            % Initialize subscriptions
            this.initializeSubscriptions();
        end
        
        % Methods that subscribe to the client messages
        handlePrepare(this, data);
        updateView(this, uiReport);
        uiReport = compileReportFromCache(this);
        publishUIReport(this, data);
        updateData(this);
        function delete(this)
            % Remove subscriptions when the object is destroyed
            for i = 1: numel(this.Subscriptions)
                this.MsgServiceInterface.unsubscribe(this.Subscriptions{i});
            end
            this.Subscriptions = [];
        end
    end
    
    % Setters and getters for properties
    methods(Hidden)        
        function dataManager = getDataManager(this)
            dataManager = this.DataManager;
        end
        
        function setDataManager(this, dataManager)
            this.DataManager = dataManager;
        end
        
        function setMsgServiceInterface(this, msgServiceInterface)
            this.MsgServiceInterface = msgServiceInterface;
        end
        
        function msgServiceInterface = getMsgServiceInterface(this)
            msgServiceInterface = this.MsgServiceInterface;
        end
    end
    
    methods(Access={?SharedSetupForTestingAnalyzerWithMocking})
        isRestorePointAvailable = getRestorePointStatus(this, analyzerReport);
    end
    methods(Access=private)
        initializeSubscriptions(this);
        addIdToChannels(this, uniqueID);
        updateModelHierarchy(this, data, fptInstance)
        updateRunBrowser(this, fptInstance, isError)
    end
end

% LocalWords: getreport FPTAnalyzer