function sldv_extender_cb(blk,callback)
% Implements the callbacks for the Simulink Design Verifier Extender block

%   Copyright 2009-2011 The MathWorks, Inc.

    switch callback
        case 'init'
            init_cb(blk)
        case 'redraw'
            exttype_cb(blk)
        otherwise
            disp( getString(message('Sldv:shared:top:ExtenderCallBackNotImpl')) )
    end
   

function exttype_cb(blk)
switch get_param(blk,'ext_type')
    case 'Finite'
        set_param([blk '/Infinite'],'Value','false')
        set_param(blk,'MaskVisibilities',{'on';'on';'on'});
        set_param(blk,'MaskEnables',{'on';'on';'on'});
        draw_icon(blk, false);
    case 'Infinite'
        set_param([blk '/Infinite'],'Value','true')
        set_param(blk,'MaskVisibilities',{'on';'on';'off'});
        set_param(blk,'MaskEnables',{'on';'on';'off'});
        draw_icon(blk, true);
end
   
 
function init_cb(blk)
    switch get_param(blk, 'reset')
        case 'on'
            if strcmp(get_param([blk '/Reset'], 'BlockType'), 'Constant')
                replace([blk '/Reset'], 'built-in/Inport');
            end
        case 'off'
             % Check for inport
            if strcmp(get_param([blk '/Reset'],'BlockType'),'Inport')
                replace([blk '/Reset'],'built-in/Constant')
                set_param([blk '/Reset'],'Value','false')
            end
    end
    
    if(strcmp(get_param(blk,'ext_type'), 'Finite'))
        errorcheck(blk);  
    end
    
    exttype_cb(blk);
    
function errorcheck(blk)

param_in = get_param(blk, 'out_hold');
errStr = ''; 

try
     param = evalin('base',param_in);

    if(~isscalar(param))
       errStr = message('Sldv:TemporalBlks:ExtNonScalar'); 
    elseif(~isnumeric(param))
       errStr = message('Sldv:TemporalBlks:ExtNonNumeric');
    elseif(param < 1) 
       errStr = message('Sldv:TemporalBlks:ExtMinNumber');
    elseif(param > 10000) 
       errStr = message('Sldv:TemporalBlks:ExtMaxParam');
    end

    % Edit time error checking
    if ~isempty(errStr) && strcmp(get_param(bdroot(blk),'SimulationStatus'),'stopped')
        errordlg(errStr.getString);
    end
    
catch Mex  %#ok<NASGU>
end

% Compile/Simulation time errorchecking
if ~isempty(errStr) && ~strcmp(get_param(bdroot(blk),'SimulationStatus'),'stopped') 
    error(errStr);
end    
     
% Local replace function
function replace(oldblock,newblock)
pos = get_param(oldblock,'Position');
orient = get_param(oldblock,'Orientation');
delete_block(oldblock);
add_block(newblock,oldblock,'Position',pos,...
    'Orientation',orient, 'ForegroundColor', 'gray');

function draw_icon(blk, inf)
  iconStr = '';
  
  pstable1 = 0.4; %Extension period starts here.
  if(inf)
    pstable2 = 0.75;
  else
    pstable2 = 0.65;
  end
  
  sigheight = 0.15;
  y1 = 0.7;
  y1h = y1 + sigheight;
  y2 = 0.33;
  y2h = y2 + sigheight;
  
  iconStr = [iconStr, sprintf('\n color(''gray'');')]; %NO_L10N,
  iconStr = [iconStr, sprintf('\n plot([0.15 0.2 0.2 %d %d 0.75],[ 0.7 0.7 %d %d 0.7 0.7]);', pstable1,pstable1,y1h,y1h)]; %NO_L10N,
  
  iconStr = [iconStr, sprintf('\n color(''black'');')]; %NO_L10N,
  iconStr = [iconStr, sprintf('\n plot([0.15 0.2 0.2 %d], [%d, %d %d %d]);', pstable2,y2,y2,y2h,y2h)]; %NO_L10N,
  
  if(~inf)
      iconStr = [iconStr, sprintf('\n plot([%d %d 0.75],[%d %d %d]);',pstable2,pstable2,y2h,y2,y2)]; %NO_L10N,
  end
  
  set_param(blk, 'MaskDisplay', sprintf(iconStr));        
