%

%   Copyright 2016 The MathWorks, Inc.

function convertedData = convertFromSLDVData (this, data)
    tcFormat = this.extendOpts.inputTCformat;
    switch tcFormat
        case 'SLDVDATA'
            fileName = generateFileName(this.modelH, tcFormat);
            vname=@(x) inputname(1);
            data = vname (data);
            save(fileName,data);
            convertedData = fileName;
        case 'HARNESS'
            convertedData = sldvmakeharness(this.modelH, data);
        case 'COVDATA'
            runOpts = sldvruntestopts();
            runOpts.coverageEnabled = true;
            [~,data] = sldvruntest(this.modelH, data.sldvData, runOpts);
            fileName = generateFileName(this.modelH, tcFormat);
            cvsave(fileName,data);
            convertedData = fileName;
    end
end


function fileName = generateFileName(modelH, tcFormat)
    modelName = get_param(modelH, 'Name');
    options = sldvoptions(modelH);
    % read default output directory in which results will be produced
    % check if $ModelName$ is present in outdir, if so replace it with model
    % name
    dirName = strrep(options.OutputDir, '$ModelName$', modelName);
    dirName = strrep(dirName, '\', filesep);
    dirName = strrep(dirName, '/', filesep);
    if( ~exist(dirName, 'file'))
        mkdir(dirName);
    end
    if strcmp(tcFormat, 'COVDATA')
        fileName = Sldv.utils.settingsFilename('$ModelName$_output', 'on', '.cvt', modelH, true);
    else
        fileName = Sldv.utils.settingsFilename('$ModelName$_output', 'on', '.mat', modelH, true);
    end
end