%

%   Copyright 2016 The MathWorks, Inc.

function [status, outData]  = TopItOff(this) 

   try
        % First check the consistency of object configuration
        [errStatus] = checkConsistencyOfInput(this);
        % Error code should be an output. The corresponding message is
        % subsequently picked up from an XML file and printed.
        if errStatus == -1
            status = 0;
            outData = [];
            return;
        end
        % So, all input data is consistent now
        this.sldvData = convertToSldvData(this);
        
        [status, outData] = extendSLDVData(this);
        if status
            outData = convertFromSLDVData(this,outData);
        end

    catch Mex
         status = 0;
         outData = [];
         throw(Mex);
    end
end

function [errStatus] = checkConsistencyOfInput(this)
    % check if design model is already loaded or not
    errStatus = checkModelHandleValidity(this);
    if errStatus
        return;
    end
    % check if all extendOpts fields are consistent
    errStatus = checkExtendOptionsConsistency(this);
end

function status = checkModelHandleValidity(this)
    status = bdIsLoaded(get_param(this.modelH,'Name'));
end

function status = checkExtendOptionsConsistency(this)
   if (exist(this.extendOpts.inputTCfile, 'file') == 2) && ...
       (strcmp(this.extendOpts.mode,'COV_EXTN_MODE') || ...
       strcmp(this.extendOpts.mode,'TEST_EXTN_MODE')) && ...
       ~(strcmp(this.extendOpts.mode,'TEST_EXTN_MODE') && ...
       strcmp(extendOpts.inputTCformat,'COVDATA')) && ...
       ~(strcmp(this.extendOpts.mode,'COV_EXTN_MODE') && ...
       strcmp(extendOpts.inputTCformat,'SLDVHARNESS'))
        status = true;
   else
       status = false;
   end
end