function sldvDataExp = repeat_last_step(sldvData, forTimeSeries)
%

%   Copyright 2008-2014 The MathWorks, Inc.

    if nargin<2
        forTimeSeries = false;
    end
    sldvDataExp = sldvData;
    simData = Sldv.DataUtils.getSimData(sldvData);
    if ~isempty(simData)        
        fundamentalSampleTime = sldvshareprivate('mdl_derive_sampletime_for_sldvdata',sldvDataExp.AnalysisInformation.SampleTimes);
        InportInfo = sldvDataExp.AnalysisInformation.InputPortInfo;
        hasMatrixInput = Sldv.DataUtils.checkRootInportDimensions(InportInfo);
        for i=1:length(simData)
            numberTimeSteps = length(simData(i).timeValues);
            if forTimeSeries && ~(numberTimeSteps==1 && hasMatrixInput)
                continue;
            end
            lastTimeStep = simData(i).timeValues(end);            
            simData(i).timeValues = [simData(i).timeValues lastTimeStep+fundamentalSampleTime];
            
            for j=1:length(simData(i).dataValues) % For each inport
                isPortBusArray = false;
                if iscell(simData(i).dataValues{j}) && isfield(InportInfo{j}{1}, 'Dimensions') && ...
                                                                      any(InportInfo{j}{1}.Dimensions ~= 1)
                    isPortBusArray = true;
                end
                
                simData(i).dataValues{j} = ...
                    expandTestCaseData(simData(i).dataValues{j}, InportInfo{j}, numberTimeSteps, isPortBusArray);
            end
            
            for j=1:length(simData(i).dataNoEffect) % For each inport
                isPortBusArray = false;
                if iscell(simData(i).dataNoEffect{j}) && isfield(InportInfo{j}{1}, 'Dimensions') && ...
                                                                      any(InportInfo{j}{1}.Dimensions ~= 1)
                    isPortBusArray = true;
                end
                
                simData(i).dataNoEffect{j} = ...
                    expandTestCaseData(simData(i).dataNoEffect{j}, InportInfo{j}, numberTimeSteps, isPortBusArray);                
            end              
            sldvDataExp = Sldv.DataUtils.setSimData(sldvDataExp,i,simData(i));
        end
    end 
end

function dataValues = expandTestCaseData(inportTestData, inportInfo, numberTimeSteps, isBusArray)    
    if ~iscell(inportTestData)
        % Extend inportTestData by one more time-step, by duplicating the data-values 
        % from the last time-step
        dt = Sldv.DataUtils.flattenData(numberTimeSteps, inportInfo.Dimensions, inportTestData);
        dt = [dt dt(:,end)];
        dataValues = Sldv.DataUtils.reshapeData(numberTimeSteps+1, inportInfo.Dimensions, dt);
    else
        % inportTestData corresponds to a bus or a bus array
        % IMP (rajeevac): in case of bus, dimensions of dataValues have changed to [numElems, 1] from [1, numElems]
        dataValues = cell(size(inportTestData));
        for i=1:numel(inportTestData)
            if isBusArray
                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
            dataValues{i} = expandTestCaseData( ...
                                        inportTestData{i}, ...
                                        portInfo, ...
                                        numberTimeSteps, isChildBusArray);
        end
    end    
end
