function [state, messageStr] = systemSelectorStateFcn(obj, ~)
% Validate the selected system in the local scope of the system

%   Copyright 2018-2019 The MathWorks, Inc.

    messageStr = "";
    state = 'supported';

    isSubsystem = isa(obj, 'Simulink.SubSystem');
    isModelReference = isa(obj, 'Simulink.ModelReference');
    if  isa(obj, 'Simulink.BlockDiagram') || ...
            isSubsystem || ...
            isModelReference
        if isSubsystem
            % all Stateflow blocks appear as subsystems
            % check if MLFB, Chart, truth table, etc...
            dutStateflow = ~strcmp(obj.SFBlockType, 'NONE');
            if dutStateflow
                % give same error message as if a model or subsystem were
                % not selected
                state = 'nonsupported';
                messageStr = message('hdlcoder:toolstrip:SystemSelectorInvalidSystem').string;
                return
            end
            
            % check if variant subsystem
            dutVaraint = strcmp(obj.getPropValue('Variant'), 'on');
            if dutVaraint
                % variant subsystem as DUT is not supported
                state = 'nonsupported';
                messageStr = message('hdlcoder:toolstrip:SystemSelectorVariantSystem').string;
                return
            end
            
            % look for enable, trigger and reset ports
            dutEnablePorts = find_system(obj.handle,'SearchDepth', '1', 'BlockType','EnablePort');
            dutTriggerPorts = find_system(obj.handle,'SearchDepth', '1', 'BlockType','TriggerPort');
            dutResetPorts = find_system(obj.handle,'SearchDepth', '1', 'BlockType','ResetPort');
            if ~isempty(cat(1, dutEnablePorts, dutTriggerPorts, dutResetPorts))
                % enable, trigger, and reset ports on DUT are not supported
                state = 'nonsupported';
                messageStr = message('hdlcoder:toolstrip:SystemSelectorControlledSystem').string;
                return
            end
            
            % check if for each subsystem
            dutForEach = find_system(obj.handle,'SearchDepth', '1', 'BlockType','ForEach');
            if ~isempty(dutForEach)
                % for each subsystem as DUT is not supported
                state = 'nonsupported';
                messageStr = message('hdlcoder:toolstrip:SystemSelectorForEachSystem').string;
                return
            end
            
        elseif isModelReference
            % check if protected model
            dutProtectedMdl = strcmp(obj.ProtectedModel, 'on');
            if dutProtectedMdl
                % protected model as DUT is not supported
                state = 'nonsupported';
                messageStr = message('hdlcoder:toolstrip:SystemSelectorProtectedModel').string;
            end
        end
    else
        % code generation only supported for subsystems, models and model
        % references
        state = 'nonsupported';
        messageStr = message('hdlcoder:toolstrip:SystemSelectorInvalidSystem').string;
    end

end
