classdef(Hidden, Sealed) DimensionsAndClassNameRepresentation < matlab.display.internal.CompactDisplayRepresentation
    % DimensionsAndClassNameRepresentation is the
    % dimensions and class metadata for displaying within a container or
    % under space constraints
    %
    % Set UseSimpleName to true if the simple class name should be used
    %
    
    % Copyright 2020 The MathWorks, Inc.
    
    properties(SetAccess = immutable)
        % Set to true if the simple class name should be used
        UseSimpleName = false;
    end
    
    properties(SetAccess = immutable, Hidden)
        ClassName (1,1) string
        DimensionsString string
        AnnotationWidth (1,1) double
    end
    
    methods
        function obj = DimensionsAndClassNameRepresentation(containedObj, displayConfiguration, optionalParams)
        % DimensionsAndClassnameRepresentation constructor.
        % 
        % DimensionsAndClassNameRepresentation()
        % Constructs a DimensionsAndClassNameRepresentation object with its UseSimpleName
        % attribute set to true and its Annotation value set to empty.
        %
        % DimensionsAndClassNameRepresentation('UseSimpleName', useSimpleName)
        % Constructs a DimensionsAndClassNameRepresentation object with its
        % UseSimpleName set to useSimpleName and its Annotation value set
        % to empty.
        %
        % DimensionsAndClassNameRepresentation('Annotation', annotation)
        % Constructs a DimensionsAndClassNameRepresentation object with its
        % UseSimpleName set to true and its Annotation value set
        % to annotation.
        %
        % DimensionsAndClassNameRepresentation('UseSimpleName', useSimpleName, 'Annotation',
        % annotation) 
        % Constructs a DimensionsAndClassNameRepresentation object with its
        % UseSimpleName set to useSimpleName and its Annotation value set
        % to annotation.
            arguments
                containedObj matlab.mixin.internal.CustomCompactDisplayProvider
                displayConfiguration (1,1) matlab.display.internal.DisplayConfiguration
                optionalParams.UseSimpleName (1,1) logical = false
                optionalParams.Annotation (1,1) string = ""
            end
            % Compute dimensions string
            dimstring = matlab.internal.display.dimensionString(containedObj);         
            % Compute full class name
            name = class(containedObj);
            if optionalParams.UseSimpleName
                % Use simple class name if UseSimpleName is set to true
                reg_expression = '[a-zA-Z_0-9]+\.';
                name = regexprep(name, reg_expression, '');
            end
            
            if(displayConfiguration.OmitDataDelimitersForDimensionsAndClassName)
                % Omit data delimiters
                paddedDisplayOutput = dimstring + displayConfiguration.DimensionsAndClassNamePadding + name;
            else
                % Construct dimensions and class name string
                % Include data delimiters araound dimensions and class name for
                % R2020b, as we currently use data delimiters around dimensions
                % and class name in struct display
                paddedDisplayOutput = displayConfiguration.DataOpenDelimiter + ...
                            dimstring + displayConfiguration.DimensionsAndClassNamePadding + name + ...
                            displayConfiguration.DataCloseDelimiter;
            end
            
            annotationWidth = 0;
            annotation = "";
            if ~isempty(optionalParams.Annotation) && optionalParams.Annotation ~= "" && ~ismissing(optionalParams.Annotation)
                % Append annotation if it is not set to empty
                annotation = displayConfiguration.AnnotationOpenDelimiter + optionalParams.Annotation + ...
                    displayConfiguration.AnnotationCloseDelimiter;
                paddedDisplayOutput = paddedDisplayOutput + ...
                                        displayConfiguration.AnnotationPadding + ...
                                        annotation;
                % Annotation width includes annotation delimiters
                annotationWidth = columnCount(displayConfiguration, annotation);
            end
            % Computer the character width
            characterWidth = columnCount(displayConfiguration, paddedDisplayOutput);
            obj@matlab.display.internal.CompactDisplayRepresentation(annotation, characterWidth, paddedDisplayOutput);
            obj.UseSimpleName = optionalParams.UseSimpleName;
            obj.ClassName = name;
            obj.DimensionsString = dimstring;
            obj.AnnotationWidth = annotationWidth;
        end
    end
end

