function customAVTBlockIntervals = customAVTBlock(blockH, command, varargin)
%   This function updates pre-packed custom Simulink Design Verifier blocks based on the values of mask parameters.
%

%   Copyright 2001-2013 The MathWorks, Inc.

if (nargin < 2)
   customAVTBlock(0, 'help');
   error(message('Sldv:Blocks:ArgNum'));

end

switch lower(command)
    case  'help'
        help = sprintf('%s\n', 'customAVTBlock(gcbh, command, options)');
        help = sprintf('%s%s\n', help, 'e.g.:');
        help = sprintf('%s%s\n', help, 'customAVTBlock(gcbh, ''update'', ''{1,2,[3,4], [-inf,7],}'')');
        help = sprintf('%s%s\n', help, 'customAVTBlock(0, ''parse_params'', {1,2,[3,4], [-inf,7],})');
        help = sprintf('%s%s\n', help, 'customAVTBlock(gcbh, ''disable'')');
        display(help);

    case  'update'
        if (~isequal(nargin,3) || ~ ischar(varargin{1}))
          error(message('Sldv:Blocks:UnknownParam'));
        end
        set_param(blockH, 'intervals', varargin{1});
    case  'enable'
        set_param(blockH, 'enabled', 'on');
    case  'disable'
        set_param(blockH, 'enabled', 'off');
    case  'updatedisp'
        update_value_display(blockH);
    case  'update_from_mask'
        customAVTBlockIntervals = update_all(blockH, varargin{1});
    case  'parse_params'
        if ~isequal(nargin,3)
           error(message('Sldv:Blocks:UnknownParam'));
        end
        try
            [customAVTBlockIntervals errId] = checkSldvSpecification(varargin{1});
            if ~isempty(errId)
                 error('Sldv:Blocks:Specification', getString(message(errId)));
            end
        catch Mex   %#ok<NASGU>
        end 
    otherwise
      error(message('Sldv:Blocks:UnknownCommand'));
end

function customAVTBlockIntervals = update_all(blockH, intervals)

    update_type(blockH);

    customAVTBlockIntervals = [];
    if ~isempty(intervals)
        [customAVTBlockIntervals errId] = checkSldvSpecification(intervals);
        if ~isempty(errId) 
            error('Sldv:Blocks:Syntax', get_param(blockH, 'MaskType')); 
        end
    end
    update_icon(blockH);
    update_outport(blockH);
    update_value_display(blockH);

% if it is pass through show output port
function update_outport(blockH)

  hasOut = isequal(length(get_param(blockH,'Blocks')),3);

  bfN = getfullname(blockH);
  if isequal(get_param(blockH,'outEnabled'),'on')
      if(~hasOut)
          Sldv.load_system('simulink');
          nbh = add_block('simulink/Sinks/Out1', [bfN, '/Out1']);
          set_param(nbh, 'Position', [275    28   305    42]);

          iph = get_param([bfN, '/In1'], 'PortHandles');
          oph = get_param(nbh, 'PortHandles');
          add_line(blockH, iph.Outport, oph.Inport);

      end
  elseif (hasOut)
         iph = get_param([bfN, '/In1'], 'PortHandles');
         oph = get_param([bfN, '/Out1'], 'PortHandles');
         delete_line(blockH, iph.Outport, oph.Inport);
         delete_block([bfN, '/Out1']);
  end


% update icon, mask  text
function update_type(blockH)
    type =  get_param(blockH, 'customAVTBlockType');
    type = ['Design Verifier ' type];
    old_type = get_param(blockH, 'masktype');
    if strcmp(type, old_type)
         return;
    end
    set_param(blockH, 'masktype', type);
    helpText = '';
    descr = '';
    switch(type)
        case 'Design Verifier Test Condition'
            descr    = getString(message('Sldv:shared:sldvlibrary:TestCondDoc'));
            helpText = 'testcondition';
            
        case 'Design Verifier Assumption'
            descr    = getString(message('Sldv:shared:sldvlibrary:AssumptionDesc'));
            helpText = 'proofassumption';
            
        case 'Design Verifier Test Objective'
            descr    = getString(message('Sldv:shared:sldvlibrary:TestObjDoc'));
            helpText = 'testobjective';
            
        case  'Design Verifier Proof Objective'
            descr    = getString(message('Sldv:shared:sldvlibrary:ProofObjDoc'));
            helpText = 'proofobjective';
    end
    
    set_param(blockH, 'MaskDescription', descr);
    helpText = ['helpview(fullfile(docroot, ''toolbox'',''sldv'',''sldv.map''),''' helpText ''')'];
    set_param(blockH, 'MaskHelp', helpText);
%
% update icons
%
function update_icon(blockH)
    maskType = get_param(blockH, 'masktype');
    disabled = strcmp(get_param(blockH,'enabled'),'off');
    iconFileName = get_icon_name(maskType, disabled);

    position = get_param(blockH, 'position');
    size     = [position(3)-position(1), position(4)-position(2)];

    set_param(blockH, 'MaskIconUnits','pixels');

    imgPosStr = ['[3 3 ' num2str(size(1)-6) ' ' num2str(size(2)-6) ']'];

    iconStr = ['image(imread(''' iconFileName  ''',''BackGroundColor'',[1 1 1]),' ...
                imgPosStr '); hide_arrows(true);'];

    oldIconStr = get_param(blockH, 'MaskDisplay');
    newIconStr = sprintf(iconStr);
    if ~strcmp(oldIconStr,newIconStr)
        set_param(blockH, 'MaskDisplay', newIconStr);
    end

function update_value_display(blockH)
    % Add or remove the values parameter from the 
    % AttributesFormatString based on the dispValues
    % parameter.
    try
        dispValuesStr = get_param(blockH,'dispValues');
    catch Mex %#ok<NASGU>
        dispValuesStr = [];
    end

    if isempty(dispValuesStr)
        return;
    end

    dispValues = strcmp(dispValuesStr,'on');
    annotStr = get_param(blockH,'AttributesFormatString');
    hasDispValues = ~isempty(findstr(annotStr,'%<intervals>'));

    if (dispValues && ~hasDispValues)
        if isempty(annotStr)
            annotStr = '%<intervals>';
        else
            if (annotStr(1)==' ' || annotStr(1)==',')
                annotStr = ['%<intervals>' annotStr];
            else
                annotStr = ['%<intervals> ' annotStr];
            end
        end
        set_param(blockH,'AttributesFormatString',annotStr);
    elseif (~dispValues && hasDispValues);
        annotStr = strrep(annotStr,'%<intervals>','');
        set_param(blockH,'AttributesFormatString',annotStr);
    end

function iconFileName = get_icon_name(maskType, disabled)
    switch(lower(maskType))
    case 'design verifier test objective'
        root = 'sldvicon_o';
    case 'design verifier test condition';
        root = 'sldvicon_c';
    case 'design verifier proof objective'
        root = 'sldvicon_p';
    case 'design verifier assumption'
        root = 'sldvicon_a';
    otherwise
        error(message('Sldv:Blocks:MaskType'));
    end

    if disabled
        iconFileName = [root '_disabled.png'];
    else
        iconFileName = [root '_normal.png'];
    end

