function msgNoLinks = createExitMsg(basicMsgArgs,detailedMsgArgs,dispMsg,useDetailedMsg,varargin)
%

%createExitMsg create exit messages for Optimization Toolbox solvers.
%
% This utility creates and displays the exit messages (default and
% detailed) for all Optimization Toolbox solvers.
%
% INPUTS:
% - basicMsgArgs: cell array of arguments for constructing the basic message
% - detailedMsgArgs: cell array of arguments for constructing the basic message
% - dispMsg: a logical scalar which is true when the message is to be displayed in
% the Command Window.
% - useDetailedMsg: a logical scalar which is true when the detailed
% message is to be displayed
%

%   Copyright 2008-2022 The MathWorks, Inc.

% Flag indicating that we will return a message
returnMsg = nargout > 0;
if ischar(dispMsg)
    dispMsg = eval(dispMsg);
end

if ~(returnMsg || dispMsg)
    return
end

% grab basic
basicMsgArgs = msgArgs2Str(basicMsgArgs);
basicMsg = getString(message(basicMsgArgs{:}));
basicMsgNoLinks = regexprep(basicMsg,'</?(\w+).*?>','');

stopCriteriaDetails = '';
detailedMsgNoLinks = '';

% grab detailed (if one exists)
if ~isempty(detailedMsgArgs)
    detailedMsgArgs = msgArgs2Str(detailedMsgArgs);
    detailedMsg = getString(message(detailedMsgArgs{:}));
    detailedMsgNoLinks = regexprep(detailedMsg,'</?(\w+).*?>','');
end

if dispMsg                  % Display Message    
    enableLinks = feature('hotlinks') && ~isdeployed;
    if ~useDetailedMsg
        % Make "stopping criteria details" link if needed
        if enableLinks 
            if ~isempty(detailedMsgArgs)
                % Convert args into strings for the "details" link cmd
                basicStr = string(basicMsgArgs);
                detailStr = string(detailedMsgArgs);
                % Handle "missing" or NaN data
                detailStr(ismissing(detailStr)) = "NaN";
                detailedMsgCmd = char( "createExitMsg({'" + join(basicStr,"','") + "'},{'" + join(detailStr,"','") + "'},true,true);" );
                % Create html tag for the "<detail>" link
                stopCriteriaDetails = getString(message('optimlib:commonMsgs:StopCriteriaDetails',detailedMsgCmd));
                basicMsg = [basicMsg stopCriteriaDetails];
            end
            fprintf(basicMsg);
        else
            fprintf(basicMsgNoLinks);
        end
    else
        if enableLinks
            fprintf(detailedMsg);
        else
            fprintf(detailedMsgNoLinks);
        end
    end
end

% Create message for output structure.
if returnMsg
    % Fetch "<stopping criteria details>" string if not done already
    if ~isempty(detailedMsgNoLinks) && isempty(stopCriteriaDetails)
        stopCriteriaDetails = getString(message('optimlib:commonMsgs:StopCriteriaDetails',''));
    end
    % Make sure to remove extra newlines, which are unneeded for
    % the output structure.
    msgNoLinks = strip([basicMsgNoLinks regexprep(stopCriteriaDetails,'</?(\w+).*?>','') detailedMsgNoLinks],newline);
end

%--------------------------------------------------------------------------
function args = msgArgs2Str(args)

for k = 2:numel(args)
    if isnumeric(args{k})
        args{k} = sprintf('%e',args{k});
    end
end
