function sldv_patternlib_cb(blk,callback)
% Implements the callbacks for the temporal patterns for property 
% specification and analysis. 

% Copyright 2009-2021 The MathWorks, Inc.
% NO_L10N -> strings that don't require localization

    switch callback
        case 'init' %NO_L10N
            init_cb(blk);
        otherwise
            disp(getString(message('Sldv:shared:top:DetectorCallBackNotImpl')));
    end   
    
function init_cb(blk)
    switch get_param(blk, 'enable')
        case 'on'
            if strcmp(get_param([blk '/enable'], 'BlockType'), 'Constant')
                replace([blk '/enable'], 'built-in/Inport');
                set_param([blk '/enable'],'OutDataTypeStr','boolean')
            end
        case 'off'
             % Check for inport
            if strcmp(get_param([blk '/enable'],'BlockType'),'Inport')
                replace([blk '/enable'],'built-in/Constant')
                set_param([blk '/enable'],'Value','true')
                set_param([blk '/enable'],'OutDataTypeStr','boolean')
            end
    end
    switch get_param(blk, 'abort')
        case 'on'
            if strcmp(get_param([blk '/abort'], 'BlockType'), 'Constant')
                replace([blk '/abort'], 'built-in/Inport');
                set_param([blk '/abort'],'OutDataTypeStr','boolean')
            end
        case 'off'
             % Check for inport
            if strcmp(get_param([blk '/abort'],'BlockType'),'Inport')
                replace([blk '/abort'],'built-in/Constant')
                set_param([blk '/abort'],'Value','false')
                set_param([blk '/abort'],'OutDataTypeStr','boolean')
            end
    end
    switch get_param(blk, 'invariant')
        case 'on'
            if strcmp(get_param([blk '/invariant'], 'BlockType'), 'Constant')
                replace([blk '/invariant'], 'built-in/Inport');
                set_param([blk '/invariant'],'OutDataTypeStr','boolean')
            end
        case 'off'
             % Check for inport
            if strcmp(get_param([blk '/invariant'],'BlockType'),'Inport')
                replace([blk '/invariant'],'built-in/Constant')
                set_param([blk '/invariant'],'Value','true')
                set_param([blk '/invariant'],'OutDataTypeStr','boolean')
            end
    end
    switch get_param(blk,'assert_t')
        case 'on'
            if strcmp(get_param([blk '/Vacuously true'],'enabled'),'off')
                set_param([blk '/Vacuously true'],'enabled','on');
            end
            if strcmp(get_param([blk '/Never falsified'],'enabled'),'off')
                set_param([blk '/Never falsified'],'enabled','on');
            end
            if (strcmp(get_param([blk '/status'], 'BlockType'), 'Outport')&& strcmp(get_param(blk,'pre_cond'),'off'))
                replace([blk '/status'], 'built-in/Constant');
            end
            if (strcmp(get_param([blk '/status'], 'BlockType'), 'Constant')&& strcmp(get_param(blk,'pre_cond'),'on'))
                replace([blk '/status'], 'built-in/Outport');
            end
            if strcmp(get_param([blk '/Never satisfied'],'enabled'),'off')
                set_param([blk '/Never satisfied'],'enabled','on');
            end
        case 'off'
            if strcmp(get_param([blk '/Vacuously true'],'enabled'),'on')
                set_param([blk '/Vacuously true'],'enabled','off');
            end
            if strcmp(get_param([blk '/Never falsified'],'enabled'),'on')
                set_param([blk '/Never falsified'],'enabled','off');
            end
            if strcmp(get_param([blk '/status'], 'BlockType'), 'Constant')
                replace([blk '/status'], 'built-in/Outport');
            end
            if strcmp(get_param([blk '/Never satisfied'],'enabled'),'on')
                set_param([blk '/Never satisfied'],'enabled','off');
            end
    end
    switch get_param(blk,'pre_cond')
        case 'on'
            if strcmp(get_param([blk '/Compare To Constant2'],'relop'),'~=') && strcmp(get_param([blk '/Compare To Constant2'],'const'),'0')
                set_param([blk '/Compare To Constant2'],'relop','==');
                set_param([blk '/Compare To Constant2'],'const','1');
            end
            if strcmp(get_param([blk '/Never satisfied'],'enabled'),'off')
                set_param([blk '/Never satisfied'],'enabled','on');
            end
        case 'off'
            if strcmp(get_param([blk '/Compare To Constant2'],'relop'),'==') && strcmp(get_param([blk '/Compare To Constant2'],'const'),'1')
                set_param([blk '/Compare To Constant2'],'relop','~=');
                set_param([blk '/Compare To Constant2'],'const','0');
            end
    end
    switch get_param(blk,'pstate')
        case 'on'
            if strcmp(get_param([blk '/pattern_state'], 'BlockType'), 'Constant')
                replace([blk '/pattern_state'], 'built-in/Outport');
            end
        case 'off'
            if strcmp(get_param([blk '/pattern_state'], 'BlockType'), 'Outport')
                replace([blk '/pattern_state'], 'built-in/Constant');
            end
    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');  