function [topModelName, modelName, ownerModel, errmsg] = loadTopModelAndRefModels(covdata, covMode)
%

%   Copyright 2012-2016 The MathWorks, Inc.
try
    %for cvi.CvhtmlSettings
    topModelName = '';
    %for report title
    modelName = '';
    ownerModel = '';
    errmsg = '';

    [topModelcovId, topHarnessCovData] = getTopModel(covdata, covMode);
    if ~isempty(topHarnessCovData)
        if topHarnessCovData.isExternalMATLABFile %matlab script no model
            return;
        end
        
        [isHarnessData, tTopModelName, ~, errmsg, ownerModel] = cvi.ReportUtils.checkHarnessData(topHarnessCovData);
        if ~isempty(errmsg)
               return;
        end
        modelName = checkModelLoaded(topHarnessCovData);
        checkModelLoadedForCovdata(covdata, covMode);
        if isHarnessData
            topModelName  = tTopModelName;
        else
            topModelName  = modelName;
        end
    else
        modelName = checkModelLoadedForCovdata(covdata, covMode);
        topModelH = cvi.ReportUtils.checkModelLoaded(topModelcovId);       
        if topModelH  ~= 0
                topModelName = get_param(topModelH, 'name');
        end
    end
catch MEx
    rethrow(MEx);
end
%================================================  
function modelName = checkModelLoadedForCovdata(covdata, covMode)

 if isa(covdata,'cv.cvdatagroup')       
    allD = covdata.getAll(covMode);
    for idx = 1:length(allD)
        cvd = allD{idx};
        modelName = checkModelLoaded(cvd(1));
    end
else
    modelName = checkModelLoaded(covdata);
 end

%================================================  
function modelName = checkModelLoaded(cvd)

   [modelCovId, scriptName] = cvi.ReportUtils.getModelCovId(cvd);
   if ~isempty(modelCovId)
       [~, modelName] = cvi.ReportUtils.checkModelLoaded(modelCovId);
       cvi.ReportData.updateDataIdx(cvd);
       cv('CheckConsistency', modelCovId);
   else %external matalb script           
        modelName = scriptName;
   end
   
%================================================  
function res = isOpen(modelName)
    res  = true;
    try
         get_param(modelName, 'name');
    catch MEx %#ok<NASGU>
        res  = false;
    end

%================================================
function [topModelcovId, topCovData] = getTopModel(covdata, covMode)
  
    topModelcovId  = [];
    topCovData = [];

    if isa(covdata,'cv.cvdatagroup')
        allD = covdata.getAll(covMode);
        for idx = 1:numel(allD)
          cvd = allD{idx};
          cvd = cvd(1);
           
          %make sure only the harness referenced model gets ownerModel set
          %until then get the first one
          if isempty(topCovData) && ~isempty(cvd.modelinfo.ownerModel)
              topCovData = cvd;
          end
          modelcovId = cv('get', cvd.rootId,'.modelcov');
          topModelcovId = cv('get', modelcovId, '.topModelcovId');
          if ~isempty(topModelcovId) && topModelcovId ~= 0 && ... %can be after cvload
              cv('ishandle',topModelcovId) && ... % after harness close
                  ~isempty(cvi.ReportUtils.getModelCovId(cvd))

              break;
          end
        end
    else
        modelcovId = cv('get',covdata.rootId,'.modelcov');

        topModelcovId = cv('get', modelcovId, '.topModelcovId');
        if ~isempty(covdata.modelinfo.ownerModel)
            topCovData = covdata;
        end
       
        if isempty(topModelcovId) ... 
            || topModelcovId == 0  %can be after cvload
            topModelcovId = modelcovId;
        end
    end


        
        
        
    
