function stopTime = loadSLDVData(sbBlkH, sldvData, fundTs, doAppend)
%

% Copyright 2015-2020 The MathWorks, Inc.
%
% Load sldvData into the Signal Builder source of the Test Harness

stopTime = 0;
if isequal(sldvData.AnalysisInformation.Options.Mode, 'TestGeneration')
    groupNameBase =  getString(message('Sldv:HarnessUtils:MakeSystemTestHarness:TestCase'));
else
    groupNameBase =  getString(message('Sldv:HarnessUtils:MakeSystemTestHarness:Counterexample'));
end
if isfield(sldvData, 'TestCases')
    dvTestCases = sldvData.TestCases;
else
    dvTestCases = sldvData.CounterExamples;
end

% Check if the input values can be precisely represented as double precision
% data. This is relevant in case of fixed-point objects. If it is not possible
% to represent them as double, log a warning about precision loss (g1583531).
message_object = warn_on_precision_loss(dvTestCases);
if ~isempty(message_object)
    warning(message_object);
end

tc = length(dvTestCases);
ts = fundTs;

groups = cell(1, tc);


for g = 1:tc
    groups{g} = [groupNameBase ' ' num2str(g)];
    nTimeSteps = length(dvTestCases(g).timeValues);
    t = zeros(1, 2 * nTimeSteps);
    t(1, 1) = dvTestCases(g).timeValues(1);
    t(1, 2:2:end-1) = dvTestCases(g).timeValues(2:end);
    t(1, 3:2:end-1) = dvTestCases(g).timeValues(2:end);
    t(1, end)       = dvTestCases(g).timeValues(end) + ts;
    s = 0; % s ==> signal counter
    numPorts = length(sldvData.AnalysisInformation.InputPortInfo);
    for i = 1:numPorts
        
        inputPortInfo = sldvData.AnalysisInformation.InputPortInfo{i};
        isPortBusArray = false;
        if iscell(inputPortInfo) && isfield(inputPortInfo{1}, 'Dimensions') && ...
                any(inputPortInfo{1}.Dimensions ~= 1)
            isPortBusArray = true;
        end
        
        
        [numData, rawData] = extractRawData(dvTestCases(g).dataValues{i}, inputPortInfo, isPortBusArray, nTimeSteps);
        
        
        v = zeros(1, 2 * nTimeSteps);
        for r = 1:numData
            v(1, 1:2:end) = rawData(r, :);
            v(1, 2:2:end) = rawData(r, :);
            s = s + 1;
            time{s, g} = t; %#ok<AGROW>
            data{s, g} = v; %#ok<AGROW>
            stopTime = max(stopTime, t(1, end));
        end
    end
    nSignals = s;
end

if nSignals > 0
    [~, ~, signames, oldGroupNames] = signalbuilder(sbBlkH);
    nOldGroups = length(oldGroupNames);
    signalbuilder(sbBlkH, 'append', time, data, signames, groups);
    if ~doAppend
        signalbuilder(sbBlkH, 'set', 1:1:nSignals, 1:1:nOldGroups, []);
    end
end

end



function [numData, rawData] = extractRawData(dataValues, inportInfo, isPortBusArray, nTimeSteps)
    numData = 0;
    rawData = [];
    
    if ~iscell(dataValues)
        if inportInfo.Used
            numData = prod(inportInfo.Dimensions);
            rawData = double(reshape(dataValues(:), [numData nTimeSteps]));
        else
            numData = 0;
            rawData = [];
        end
    else
        for i=1:numel(dataValues)
            if isPortBusArray
                portInfo = inportInfo;
                isChildBusArray = false;
            else
                portInfo = inportInfo{i+1};
                isChildBusArray = false;
                if iscell(portInfo) && isfield(portInfo{1}, 'Dimensions') && any(portInfo{1}.Dimensions ~= 1)
                    isChildBusArray = true;
                end
                
            end
            [numData_, rawData_] = extractRawData(dataValues{i}, portInfo, isChildBusArray, nTimeSteps);
            numData = numData + numData_;
            rawData = [rawData; rawData_]; %#ok<AGROW>
        end
    end
end
