classdef HDLRCPHILLibSpMVMaskInit
%

%   Copyright 2024 The MathWorks, Inc.

    methods(Static)

        % Following properties of 'maskInitContext' are available to use:
        %  - BlockHandle
        %  - MaskObject
        %  - MaskWorkspace: Use get/set APIs to work with mask workspace.
        function MaskInitialization(maskInitContext)
            thisBlockH = maskInitContext.BlockHandle;

            SelPortH = find_system(thisBlockH, 'LookUnderMasks', 'on', 'FollowLinks', 'on', 'Name', 'Sel');
            SelDTCH = find_system(thisBlockH, 'LookUnderMasks', 'on', 'FollowLinks', 'on', 'Name', 'SelDTC');
            SelectorH = find_system(thisBlockH, 'LookUnderMasks', 'on', 'FollowLinks', 'on', 'Name', 'Selector');
            ParamDTCH = find_system(thisBlockH, 'LookUnderMasks', 'on', 'FollowLinks', 'on', 'Name', 'ParamDTC');
            MatrixConstantH = find_system(thisBlockH, 'LookUnderMasks', 'on', 'FollowLinks', 'on', 'Name', 'MatrixConstant');
            MatrixConstantValue = hdlslResolve('Value', MatrixConstantH);

            if size(MatrixConstantValue, 3) > 1
                % Matrix is 3-dimensional
                if isempty(SelPortH)
                    SelPortH = add_block('built-in/Inport', [getfullname(thisBlockH), '/Sel'], ...
                        'Position', [-130 103 -100 117]);
                end
                if isempty(SelDTCH)
                    SelDTCH = add_block('built-in/DataTypeConversion', [getfullname(thisBlockH), '/SelDTC'], ...
                        'Position', [-45 91 -5 129], ...
                        'OutDataTypeStr', 'uint32');
                end
                if isempty(SelectorH)
                    SelectorH = add_block('built-in/Selector', [getfullname(thisBlockH), '/Selector'], ...
                        'Position', [70 36 110 74], ...
                        'NumberOfDimensions', '3', ...
                        'IndexMode', 'Zero-based', ...
                        'IndexOptionArray', {'Select all', 'Select all', 'Index vector (port)'});
                end
                outLine = get_param(SelPortH, 'LineHandles');
                if outLine.Outport ~= -1
                    delete_line(outLine.Outport);
                end
                inLine = get_param(SelDTCH, 'LineHandles');
                if inLine.Inport ~= -1
                    delete_line(inLine.Inport);
                end
                add_line(thisBlockH, [get_param(SelPortH, 'Name'), '/1'], ...
                    [get_param(SelDTCH, 'Name'), '/1'], ...
                    'autorouting', 'on');

                outLine = get_param(SelDTCH, 'LineHandles');
                if outLine.Outport ~= -1
                    delete_line(outLine.Outport);
                end
                inLine = get_param(SelectorH, 'LineHandles');
                if inLine.Inport(2) ~= -1
                    delete_line(inLine.Inport(2));
                end
                add_line(thisBlockH, [get_param(SelDTCH, 'Name'), '/1'], ...
                    [get_param(SelectorH, 'Name'), '/2'], ...
                    'autorouting', 'on');

                outLine = get_param(SelectorH, 'LineHandles');
                if outLine.Outport ~= -1
                    delete_line(outLine.Outport);
                end
                inLine = get_param(ParamDTCH, 'LineHandles');
                if inLine.Inport(1) ~= -1
                    delete_line(inLine.Inport);
                end
                add_line(thisBlockH, [get_param(SelectorH, 'Name'), '/1'], ...
                    [get_param(ParamDTCH, 'Name'), '/1'], ...
                    'autorouting', 'on');

                outLine = get_param(MatrixConstantH, 'LineHandles');
                if outLine.Outport ~= -1
                    delete_line(outLine.Outport);
                end
                inLine = get_param(SelectorH, 'LineHandles');
                if inLine.Inport(1) ~= -1
                    delete_line(inLine.Inport(1));
                end
                add_line(thisBlockH, [get_param(MatrixConstantH, 'Name'), '/1'], ...
                    [get_param(SelectorH, 'Name'), '/1'], ...
                    'autorouting', 'on');
            else
                % Matrix is 2-dimensional
                if ~isempty(SelPortH)
                    outLine = get_param(SelPortH, 'LineHandles');
                    if outLine.Outport ~= -1
                        delete_line(outLine.Outport);
                    end
                    delete_block(SelPortH);
                end
                if ~isempty(SelDTCH)
                    outLine = get_param(SelDTCH, 'LineHandles');
                    if outLine.Outport ~= -1
                        delete_line(outLine.Outport);
                    end
                    delete_block(SelDTCH);
                end
                if ~isempty(SelectorH)
                    outLine = get_param(SelectorH, 'LineHandles');
                    if outLine.Outport ~= -1
                        delete_line(outLine.Outport);
                    end
                    delete_block(SelectorH);
                end
                outLine = get_param(MatrixConstantH, 'LineHandles');
                if outLine.Outport ~= -1
                    delete_line(outLine.Outport);
                end
                inLine = get_param(ParamDTCH, 'LineHandles');
                if inLine.Inport ~= -1
                    delete_line(inLine.Inport);
                end
                add_line(thisBlockH, [get_param(MatrixConstantH, 'Name'), '/1'], ...
                    [get_param(ParamDTCH, 'Name'), '/1'], ...
                    'autorouting', 'on');
            end
        end
    end
end
