function stopTime = createSourceBlockSignals(sigBlkH, harnessName, sldvData, fundts, isSignalBuilderSrc, appendMode)
% Utility to create new Signal Builder block groups or Signal Editor
% scenarios from sldvData
%

%   Copyright 2019-2021 The MathWorks, Inc.

usedSignals = {};
usedSignals = Simulink.harness.internal.populateUsedSignals(sldvData.AnalysisInformation.InputPortInfo, usedSignals);
usedSignals = usedSignals{:};

if isSignalBuilderSrc
    stopTime = sldvshareprivate('loadSLDVData', sigBlkH, sldvData, fundts, appendMode);
else
    [harnessSource, errorMsg] = Sldv.harnesssource.Source.getSource(harnessName);
    if isempty(errorMsg)
        harnessSource.addTestcases(sldvData,appendMode,usedSignals);
        if isfield(sldvData, 'TestCases')
            stopTime = computeSimStoptime(sldvData.TestCases) + fundts;
        else
            stopTime = computeSimStoptime(sldvData.CounterExamples) + fundts;
        end
        
    else
        stopTime = 0;
    end
    
    if ~isempty(harnessSource) && ~isempty(usedSignals)
        %Update the constant block in the Input Conversion subsystem with
        %values from sldvData min/max fields
        %See http://inside.mathworks.com/tl?RxKhlU3dFU
        
        inputConvSS = [harnessName,'/',sprintf('Input\nConversion\nSubsystem')];
	% @todo update the usage of edit-time filter filterOutInactiveVariantSubsystemChoices()
	% instead use the post-compile filter activeVariants() - g2603143
        constants = find_system(inputConvSS,'MatchFilter',@Simulink.match.internal.filterOutInactiveVariantSubsystemChoices, 'FindAll', 'on', 'BlockType', 'Constant' ); % look only inside active choice of VSS
        const_counter = 1;
        inputs = sldvData.AnalysisInformation.InputPortInfo;
        
        for id = 1:length(inputs)
            if isstruct(inputs{id}) && ...
                    inputs{id}.Used == 0
                
                if isfield(inputs{id},'Min')
                    const_value = inputs{id}.Min;
                elseif isfield(inputs{id},'Max')
                    const_value = inputs{id}.Max;
                else
                    const_value = get_param(constants(const_counter),'Value');
                end
                if strcmp(const_value,'[]')
                    %if no min/max is specified then come up with
                    %appropriate constant value
                    [isEnum, className] = sldvshareprivate('util_is_enum_type',...
                        inputs{id}.DataType);
                    if isEnum
                        data = sldvshareprivate('util_get_enum_defaultvalue', className);
                        const_value =  num2str(int32(data));
                    else
                        const_value = '0';
                    end
                end
                signal_label = inputs{id}.SignalLabels;
                set_param(constants(const_counter),'Value',const_value,...
                    'Name',signal_label);
                const_counter = const_counter + 1;
            end
        end
        
        %Update extrapolation setting on Signal Editor block (g1793245)
        curr_active_scenario = get_param(harnessSource.blockH,'ActiveScenario');
        curr_active_signal = get_param(harnessSource.blockH,'ActiveSignal');
        num_of_scenarios = str2double(get_param(harnessSource.blockH,'NumberOfScenarios'));
        for sc = 1:num_of_scenarios
            num_of_signals = str2double(get_param(harnessSource.blockH,'NumberOfSignals'));
            set_param(harnessSource.blockH,'ActiveScenario',sc);
            for sig = 1:num_of_signals
                set_param(harnessSource.blockH,'ActiveSignal',sig);
                OutputAfterFinalValue = get_param(harnessSource.blockH,'OutputAfterFinalValue');
                if ~strcmp(OutputAfterFinalValue,'Holding final value')
                    set_param(harnessSource.blockH,'OutputAfterFinalValue',...
                        'Holding final value');
                end
            end
        end
        set_param(harnessSource.blockH,'ActiveScenario',curr_active_scenario);
        set_param(harnessSource.blockH,'ActiveSignal',curr_active_signal);
    end
end
end

function stopTime = computeSimStoptime(dvTestCases)
tc = length(dvTestCases);
stopTime = 0;
for g = 1:tc
    stopTime = max(stopTime, dvTestCases(g).timeValues(end));
end
end
