classdef DeriveHandler < fxptui.Web.CallbackService.CallbackHandler
    % Class to handle the work for Derive Callback in FPT.
    
    % Copyright 2019 The MathWorks, Inc.
    
    methods(Hidden)
        
        function derive(this, runName)
            % DERIVE run Range Analysis in the Fixed-Point Tool
            % Prepares FPT for Derive Range action, executes
            % DataTypeWorkflow.Converter deriveMinMax method, handles
            % post-processing and errors
            
            % Call the CallbackHandler.prepareForIdealizedCallback
            % applies idealized setting, sets last shortcut and changes run name
            this.setIdealizedSettings(runName);
            
            % Disable code view
            this.FPTInstance.enableCodeView(false);
            
            % FPT only currently handles normal mode
            this.AccelModeHandler.switchToNormalMode;
            
            % Turn off fast restart before deriving ranges
            this.FastRestartHandler.turnOffFastRestart;
            
            try
                % Derive Min Max Using DataTypeWorkflow.Converter
                this.Converter.deriveMinMax();
                
            catch e
                % showdialog can throw an error in testing mode. catch this error, restore
                % the UI and then rethrow the error.
                this.cleanup();
                
                % Throw to diagnostic viewer with default Derived Range error header
                fxptui.showdialog('staticrangefailed', e);
                
                % g1993741 - refresh RunBrowser after failed callback
                this.refreshRunBrowser();
                
                % Publish to SimFailPublishChannel and re-enable code view to stop
                % the spinner
                this.FPTInstance.reEnableUI('');
                return;
            end
            
            % Clean up shortcut settings here
            this.cleanup();
            
            % re-enable code view
            this.FPTInstance.enableCodeView(true);
            
            % We need to enable codeview if MLFB results are present
            this.FPTInstance.getStartupController.publishEnableCodeView;
            
            this.FPTInstance.updateData('append', runName);
            
        end
        
        function cleanup(this)
            % perform the clean up tasks after derive
            
            this.FPTInstance.restoreSystemSettings;
            this.AccelModeHandler.restoreSimulationMode;
            
            % Restore fast restart settings on the model after derived range analysis
            this.FastRestartHandler.restoreFastRestart;
            
        end
        
    end
    
end

% LocalWords:  staticrangefailed
