function desc = getTestcaseDesc(sldvData, index, existHarnMdlH)
%

%   Copyright 2008-2021 The MathWorks, Inc.

    desc = '';
    if nargin<3
        existHarnMdlH = [];
    end
   
    [tc, title] = Sldv.DataUtils.getSimData(sldvData, index);
    if isempty(tc)
        return;
    end
    
    % When there is PathObjectives field, take the results of these
    % objectives into consideration
    if isfield(sldvData, 'PathObjectives')
        objLength = length(sldvData.Objectives);
        totLength = objLength + length(sldvData.PathObjectives);
    else
        objLength = length(sldvData.Objectives);
        totLength = objLength;
    end
    atTime = zeros(totLength, 1);
    for k=1:length(tc.objectives)
        obj = tc.objectives(k);
        atTime(obj.objectiveIdx) = obj.atTime;
    end
    % Get the PathObjectives atTime if any
    if isfield(tc, 'pathobjectives')
        for k = 1:length(tc.pathobjectives)
            obj = tc.pathobjectives(k);
            atTime(objLength+obj.objectiveIdx) = obj.atTime;
        end
    end

    if ~isempty(tc.objectives)
        % collect the indices of all satisfied objectives
        satObjIndices = [tc.objectives.objectiveIdx];
    else
        satObjIndices = [];
    end
    if isfield(tc, 'pathobjectives') && ...
            ~isempty(tc.pathobjectives)
        % collect the indices of all satisfied pathobjectives
        satObjIndices = [satObjIndices, [tc.pathobjectives.objectiveIdx] + objLength];
    end
    
    isXIL = Sldv.DataUtils.isXilSldvData(sldvData);
    nobjs = 0;
    desc = '';
    for j=1:totLength
        if j <= objLength
            obj = sldvData.Objectives(j);
            % XIL objective can be mapped to many model objects then keep the first one
            modelObjectIdx = obj.modelObjectIdx;
            if isXIL && numel(modelObjectIdx) > 1
                modelObjectIdx = modelObjectIdx(1);
            end
            blk = sldvData.ModelObjects(modelObjectIdx);
            objDescr = obj.descr;
        else
            obj = sldvData.PathObjectives(j-objLength);
            blk = sldvData.ModelObjects(obj.source);
            objDescr = ['Path:' obj.label];
        end
        if any(j == satObjIndices)
            nobjs = nobjs+1;
            desc = sprintf('%s\n\t%d. %s - %s @ T=%02.02f', ...
                           desc, nobjs, ...
                           blk.descr, ... 
                           objDescr, atTime(j));
        end
    end
    
    pdesc = '';
    if isfield(tc, 'paramValues')
        for m=1:length(tc.paramValues)
           p = tc.paramValues(m);
           if slavteng('feature','BusParameterTuning') && isstruct(p.value)
               pdesc = recPrint(pdesc, p.name, p.value);
           else
               pdesc = sprintf('%s\n\t\t%s = %s', ...
                   pdesc, p.name, sldvshareprivate('util_matrix2str', p.value));
           end
        end
        pdesc = sprintf(['\n\t' getString(message('Sldv:DataUtils:getTestcaseDesc:ParamValues')) ':%s\n'], pdesc);
    end
    
    if ~isempty(existHarnMdlH)
        % Here, we do not include the test case name in 'desc' as we
        % prefix the desc with test case name while appending the doc later.
        desc = sprintf(['(%d ' getString(message('Sldv:DataUtils:getTestcaseDesc:Objectives')) ')%s%s'], ...
                   nobjs, pdesc, desc);  
    else
        %Here we include the test name in 'desc'
        desc = sprintf(['%s %d (%d ' getString(message('Sldv:DataUtils:getTestcaseDesc:Objectives')) ')%s%s'], ...
                   title, index, nobjs, pdesc, desc);
    end
end

function pdesc = recPrint(pdesc, paramName, paramValue)
    % Recursively add the statements to print for leaves inside the structure
    % parameter with name as 'paramName' and Value as 'paramValue'
    % For example, paramName could be 'EngineParam'and paramValue could be a
    % strut('speed', 100.92012, 'acceleration', 0)
    if ~isstruct(paramValue)
        % Leafs - print to pdesc
        pdesc = sprintf('%s\n\t\t%s = %s', ...
                   pdesc, paramName, sldvshareprivate('util_matrix2str', paramValue));
    else
        paramFields = fieldnames(paramValue);
        for idx = 1:length(paramFields)
            fieldName = [paramName, '.', paramFields{idx}];
            pdesc = recPrint(pdesc, fieldName, paramValue.(paramFields{idx}));
        end
    end
end

% LocalWords:  pathobjectives EngineParam'and pdesc
