classdef IterativeProposeHandler < fxptui.Web.CallbackService.ActionsHandler
    % Class to handle the work Propose in iterative conversion workflow.
    
    % Copyright 2019 The MathWorks, Inc.
    
    properties(Constant)
        ActionID = fxptui.internal.ActionFactory.ProposeID;
    end
    
    methods
        
        function prepare(~, varargin)
            fptInstance = varargin{1};
            fptInstance.enableCodeView(false);
        end
        
        function handleSuccess(this, varargin)
            fptInstance = varargin{1};
            workflowSetting = varargin{2};
            
            % g1986173 - switch document view in UI
            dataController = fptInstance.getDataController();
            switchIntent = fxptui.message('setupConversion');
            dataController.sendFPTUIDocumentSelection(switchIntent);
            
            collectionSetting = workflowSetting.getSettingValue('CollectionSetting');
            runName = collectionSetting.RunName;
            designSetting = workflowSetting.getSettingValue('DesignSetting');
            selectedSystemToScale = designSetting.SelectedSystemToScale;
            
            fptInstance.updateData('append', runName);
            this.showDialogForAlerts(fptInstance, runName);
            
            sudObj = get_param(selectedSystemToScale,'Object');
            fptInstance.getExternalViewer.typesProposed(sudObj);
            
            % Restore the settings that was applied earlier.
            fptInstance.getExternalViewer.restoreModelSettings;
            
            % Re-enable code view
            fptInstance.enableCodeView(true);
        end
        
        function handleError(this, varargin)
            e = varargin{1};
            fptInstance = varargin{2};
            workflowSetting = varargin{3};
            
            % showdialog can throw an error in testing mode. catch this error, restore
            % the UI and then rethrow the error.
            % Restore the settings that was applied earlier.
            fptInstance.getExternalViewer.restoreModelSettings;
            % Re-enable code view
            fptInstance.enableCodeView(true);
            
            % g1993741 - refresh RunBrowser after failed callback
            isError = true;
            this.refreshRunBrowser(fptInstance, isError);
            
            % get the type from workflow setting
            proposalSetting = workflowSetting.getSettingValue('ProposalSetting');
            isWordlengthSelection = proposalSetting.ProposeWordLength;
            
            fxptui.showdialog('scaleproposefailed', e, isWordlengthSelection);
        end
        
        function cleanup(~, varargin)
            % no op
        end
        
    end
    
    methods(Hidden)
        
        function showDialogForAlerts(~, fptInstance, runName)
            % alerts the user when a the results of a proposal contain
            % results that have a 'red' value
            
            results = fptInstance.Converter.results(runName);
            
            if ~isempty(results)
                alerts = {results.Alert};
                hasRedAlert = ismember('red', alerts);
                
                if hasRedAlert
                    fxptui.showdialog('scaleproposeattention');
                end
            end
            
        end
    end
    
end

