function varargout = modelReducer(varargin)
% modelReducer  Model Reducer App.
%
%   modelReducer opens the Model Reducer App.  This Graphical User
%   Interface lets you to reduce LTI models using various model reduction
%   techniques. You can reduce the order and capture the dynamics of a
%   system on a certain time or frequency interval. You can select desired
%   modes of your system or do pole/zero simplification.
%
%   modelReducer(MODEL1,...,MODELN) opens the App with LTI models MODEL1,
%   ...,MODELN. Models are single, proper dynamic system without delay,
%   with inputs and outputs.
%
%   modelReducer(SESSIONFILE) opens the App and loads a previously saved
%   session SESSIONFILE.

%   Copyright 2015 The MathWorks, Inc.

Tool = [];
switch nargin
    case 0
        % No input        
        Tool = mrtool.ModelReductionApp();
        openTool(Tool);        
    otherwise        
        if ischar(varargin{1}) || isStringScalar(varargin{1})
            % session file                       
            FileName = appendMATExtension(varargin{1});
            if exist(FileName,'file')
                SessionFile = mrtool.util.validateSessionFile(FileName);
                Tool = modelReducer();
                preLoadSession(Tool,SessionFile.ModelReducerSession,mrtool.util.extractFileName(FileName));
            end
        else
            % model            
            [isValid,Models,InvalidType] = mrtool.util.isValidSystem(varargin);
            if all(isValid)
                for ct=1:length(varargin)
                    VarNames{ct}=inputname(ct);
                end                
                ModelWrappers = mrtool.util.createModelWrapper(Models,VarNames);
                Tool = mrtool.ModelReductionApp(ModelWrappers);
                openTool(Tool);
            else
                switch InvalidType
                    case 'ltiarray'
                        error(message('Control:mrtool:ErrorLtiArrayModel'));
                    case 'improper'
                        error(message('Control:mrtool:ErrorImproperModel'));
                    case 'delay'
                        error(message('Control:mrtool:ErrorDelayModel'));
                    case 'static'
                        error(message('Control:mrtool:ErrorStaticModel'));
                    otherwise
                        error(message('Control:mrtool:InvalidModel'))                                                
                end                        
            end                                    
        end
end
if isempty(Tool)
    error(message('Control:general:InvalidSyntaxForCommand','modelReducer','modelReducer'))
end
if nargout
    varargout{1} = Tool;
end
end

function Tool = openTool(Tool)
    TGroup = getToolGroup(Tool);
    internal.setJavaCustomData(TGroup.Peer,Tool);
end
function FileName = appendMATExtension(Param)
FileName = Param;
if ~contains(FileName,'.mat')
    if isstring(FileName)
        FileName = FileName+".mat";
    else
        FileName = [FileName '.mat'];
    end
end
end