classdef ResultSummaryController < handle
    % ResultSummaryController Class definition to communicate with the web based
    % result summary in FPT
    
    %   Copyright 2019 The MathWorks, Inc.
    
    properties (Access = 'private')
        Result
        Subscriptions
        LastPublishedChannel
        LastPublishedData
        PublishSummaryResult = '/fpt/resultsummary/updateresult';
        PublishUpdateAppliedSolution = '/fpt/resultsummary/updateappliedsolution'
        LaunchSolutionsPlotSubscription = '/fpt/resultsummary/launchsolutionsplot';
    end
    
    methods
        function this = ResultSummaryController(uniqueID)
            this.addUniqueIDToChannels(uniqueID);
            this.initializeSubscriptions;
        end
    end
    
    methods (Hidden)
        
        function publishResultSummary(this, result)
            this.Result = result;
            
            this.updateRunBrowser();
            
            % Update the result summary document
            message.publish(this.PublishSummaryResult, this.Result);
            
            this.LastPublishedData = this.Result;
            this.LastPublishedChannel = this.PublishSummaryResult;
        end
        
        function unsubscribe(this)
            for index = 1:numel(this.Subscriptions)
                message.unsubscribe(this.Subscriptions{index});
            end
        end
        
        function delete(this)
            this.unsubscribe();
        end
        
        function launchPlot(~)
            % ToDo - Invoke FxpOpt API to launch plot
            % g2071404
            disp('compare solutions');
        end
        
        function updateData(~)
            %ToDo - Publish summary report from previous FPT session
            % g2081542
        end
        
        function updateRunBrowser(~)
            % UPDATERUNBROWSER updates the run browser with Result node
            fptInstance = fxptui.FixedPointTool.getExistingInstance;
            dc = fptInstance.getDataController();
            rbc = dc.getRunBrowserController();
            runInfo = dc.getRunBrowserData();
            rbc.updateRunBrowser(runInfo);
        end
        
        function updateAppliedSolution(this)
            data = struct;
            message.publish(this.PublishUpdateAppliedSolution, data);

            this.LastPublishedChannel = this.PublishUpdateAppliedSolution;
            this.LastPublishedData = data;
        end
    end
    
    methods(Access=private)
        function addUniqueIDToChannels(this, uniqueID)
            this.PublishSummaryResult = sprintf('%s/%s',this.PublishSummaryResult, uniqueID);            
            this.PublishUpdateAppliedSolution = sprintf('%s/%s',this.PublishUpdateAppliedSolution, uniqueID);
            
            % Subscriptions
            this.LaunchSolutionsPlotSubscription = sprintf('%s/%s',this.LaunchSolutionsPlotSubscription, uniqueID);
        end
        
        function initializeSubscriptions(this)
            this.Subscriptions{1} = message.subscribe(this.LaunchSolutionsPlotSubscription, @(data)this.launchPlot(data));
        end
    end
    
    methods(Access = {?WorkflowTestCase})
        
        function lastPublishedChannel = getLastPublishedChannel(this)
            lastPublishedChannel= this.LastPublishedChannel;
        end
        
        function lastPublishedData = getLastPublishedData(this)
            lastPublishedData = this.LastPublishedData;
        end
        
    end
end

% LocalWords:  resultsummary updateresult launchsolutionsplot
% LocalWords:  updateappliedsolution
