function out = plcladderoption(varargin)
%PLCLADDEROPTION set or get parameters associated with Ladder Diagram models.
%   CURRENTSTATE = PLCLADDEROPTION('MDLNAME','PARAM',VALUE) sets or gets one or 
%   more comma-separated parameter name/value pairs for the model specified by 
%   using MDLNAME. For a list of parameters and values, see "Parameter Options."
%
%   Parameter Options:
%
%        Parameter      Value                               	Default
%        ---------      -----                               	-------
%   	'FastSim'       When 'on', this option disables      	'off'
%                       all animation and data display.
%                       If 'FastSim' is set to 'on', 
%                       then any change made to  'Animation'
%                       and 'DataDisplay' parameter would 
%                       not change simulation behavior. Turn 
%                       'FastSim' to 'off' to enable animation
%                       and data display
%       
%   	'Animation'     This option enables/disables        	'on'
%                       animation of Ladder Diagram 
%                       rungs, when FastSim is in 'off'
%                       state    
%
%   	'DataDisplay'   This option enables/disables data   	'off'
%                       display of Ladder Diagram rungs,
%                       when FastSim is in 'off' state
%       
%
%   	'SLDV'          This option enables/disables using  	'off'
%                       SLDV features with Ladder Diagrams
%                       Set 'SLDV' parameter to 'on' before 
%                       SLDV analysis. SLDV analysis could be 
%                       still done without using 'SLDV' option,
%                       by setting 'FastSim' to 'on' and 
%                       'Prescan' to 'off'
%
%   	'Prescan'       This option enables/disables Prescan 	'on' 
%                       for instructions and AOI blocks 
%                       explicitly for SLDV analysis
%
%       'Callbacks'     This option enables/disables internal   'on'
%                       Simulink PLC Coder callbacks. This 
%                       will also lock the top level block if
%                       Callbacks are off.
%       'SampleTime'    This option sets the sample time of 
%                       all eligible blocks to the specified 
%                       value. Plese note that model changes 
%                       due to this option are irreversibile.
%       
%   Example usage:
%     >> currentState = plcladderoption ('sampleLadderModel', 'Animation')
%     >> plcladderoption ('sampleLadderModel', 'FastSim', 'on')
%     >> plcladderoption ('sampleLadderModel', 'Animation', 'on')
%     >> plcladderoption ('sampleLadderModel', 'DataDisplay', 'on')
%     >> plcladderoption ('sampleLadderModel', 'SLDV', 'on')
%     >> plcladderoption ('sampleLadderModel', 'Prescan', 'on')
%     >> plcladderoption ('sampleLadderModel', 'Callbacks', 'on')
%     >> plcladderoption ('sampleLadderModel', 'SampleTime', '0.01')
%
%   See also PLCGENERATERUNNERTB PLCIMPORTLADDER PLCGENERATECODE
%   PLCLADDERLIB
%

%   Copyright 2016-2019 The MathWorks, Inc.

    try
        out = plcladderoption_internal(varargin);
    catch ex
        if plccore.util.IsLadderException(ex)
            throwAsCaller(ex);
        else
            rethrow(ex);
        end
    end
end

function out = plcladderoption_internal(args)
    % Using new variable names as key and old name as their values
    PLC_ladder_old = {'PLCLadderLogicDebug','PLCLadderLogicAnimation','PLCLadderLogicDataShow','PLCLadderLogicSLDVPreprocessing','PLCLadderLogicPrescan', 'PLCCallbacks', 'SampleTime'};
    PLC_ladder_new = {'FastSim', 'Animation', 'DataDisplay', 'SLDV','Prescan', 'Callbacks', 'SampleTime'};

    % Mapping using containers.Map
    PLC_map = containers.Map(PLC_ladder_new,PLC_ladder_old);
    out = {};
    narg = length(args);
    if narg ==2
        modelName = args{1};
        paramName = PLC_map(args{2});
        % 'FastSim' is an inversion of 'PLCLadderLogicDebug'
        if strcmp(paramName,'PLCLadderLogicDebug')
            out=slplc.api.getModelParam(modelName, paramName);
            if strcmp(out,'on')
                out='off';
            else
                out='on';
            end
        else
            out=slplc.api.getModelParam(modelName, paramName);
        end
    elseif narg==3
        modelName = args{1};
        paramName = PLC_map(args{2});
        paramValue = args{3};
        % 'FastSim' is an inversion of 'PLCLadderLogicDebug'
        if strcmp(paramName,'PLCLadderLogicDebug')
            if strcmp(paramValue,'off')
                paramValue='on';
            else
                paramValue='off';
            end
        end
        slplc.api.setModelParam(modelName, paramName, paramValue);
        out=slplc.api.getModelParam(modelName, paramName);
        if strcmp(paramName,'PLCLadderLogicDebug')
            if strcmp(out,'off')
                out='on';
            else
                out='off';
            end
        end
    else
        import plccore.common.plcThrowError;
        plcThrowError('plccoder:plccore:WrongNumberOfArguments', num2str(narg));
    end
end
      

% LocalWords:  plccoder plccore Preprocessing PLCGENERATERUNNERTB CURRENTSTATE
% LocalWords:  PLCIMPORTLADDER PLCGENERATECODE PLCLADDERLIB MDLNAME Prescan
