classdef BaseTableDialog < controllib.ui.internal.dialog.AbstractExportDialog
    % Class for the dialog to delete systems from a Linear System Analyzer
    % session

    % Copyright 2021 The MathWorks, Inc.

    properties (Access = protected)
        App
    end

    methods
        function this = BaseTableDialog(app,name,title)
            this = this@controllib.ui.internal.dialog.AbstractExportDialog(name,title);
            this.App = app;
        end
    end

    %% Implementation of protected absract or overloaded methods
    methods(Access = protected)
        % Abstract Method "getTableData"
        % Return data (of class 'table') to be shown in the dialog from subclass
        function tbl = getTableData(this)
            columnNames = {getString(message('Control:viewer:strModel')),...
                getString(message('Control:viewer:strSize')),...
                getString(message('Control:viewer:strClass'))};
            systemInfo = getSystemInfo(this);
            tbl = table({systemInfo.ModelName}',{systemInfo.Size}',{systemInfo.Class}',...
                'VariableNames',columnNames);
        end
    end

    methods (Access = private)
        function systemInfo = getSystemInfo(this)
            % Constructs systems list
            systemsInApp = this.App.Systems;
            if isempty(systemsInApp)
                systemInfo.ModelName = '';
                systemInfo.Size      = '';
                systemInfo.Class     = '';
                return
            end
            nsave = length(systemsInApp);
            for ct = 1:nsave
                [size_str,class_str] = localCreateStr(systemsInApp(ct).Model);
                systemInfo(ct,1) = struct(...
                    'ModelName', systemsInApp(ct).Name,...
                    'Size',      size_str,...
                    'Class',     class_str);
            end
        end
    end
end

function [size_str,VarClass] = localCreateStr(system)
VarClass=class(system);
wsize = size(system);
if any(strcmpi(VarClass,{'ss';'tf';'zpk';'frd';'sparss';'mechss'}))
    if isequal(length(wsize),2)
        s = mat2str(wsize);
        s = strrep(s,' ','x');
        size_str = s(2:end-1);
    else
        size_str = [num2str(length(wsize)),'-D'];
    end
elseif any(strcmpi(VarClass,{'idpoly';'idss';'idarx'}))
    if isequal(wsize([1 2 4]),[1 1 1])
        s = mat2str(wsize([1 2]));
        s = strrep(s,' ','x');
        size_str = s(2:end-1);
    else
        size_str = '4-D';
    end
end
end