%

%   Copyright 2016 The MathWorks, Inc.

function sldvData = convertToSldvData(this)

    tcFormat = this.extendOpts.inputTCformat;
    
    switch tcFormat
        case 'SLDVDATA'
              % Loading v/s file path
              if strcmp(this.extendOpts.mode,'COV_EXTN_MODE')
                  runOpts = sldvruntestopts;
                  runOpts.coverageEnabled = true;
                  inputData = load(this.extendOpts.inputTCfile);
                  inputData = inputData.sldvData;
                  [~, cov] = sldvruntest(this.modelH,inputData,runOpts);
                  fileName = generateFileName(this.modelH);
                  cvsave(fileName, cov);
                  sldvData = fileName;
              else
                sldvData = this.extendOpts.inputTCfile;
              end
        case 'SLDVHARNESS'
              sldvData = convertSLDVHarnessToSldvData(this);
        case 'COVDATA'
              sldvData = this.extendOpts.inputTCfile;
        otherwise 
               %add more data conversion cases
    end
end

function fileName = generateFileName(modelH)
    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
    fileName = Sldv.utils.settingsFilename('$ModelName$_temp_covdata', 'on', '.cvt', modelH, true);
end

function sldvData = convertSLDVHarnessToSldvData(this)
    % Currently, we simulate harness model to compute sldvData
    % Plan is to replace this with automatic conversion
    loadedModelHandle = load_system(this.extendOpts.inputTCfile);
    sldvData          = sldvlogsignals(loadedModelHandle);
end