classdef ImportModelFromExcel < handle
    % This class read the input Excel file and returns MATLAB tables.
    
    properties
        ExcelName;
        ComponentSheetName;
        PortSheetName;
        ConnectionSheetName;
        PortInterfaceSheetName;

        Components;
        Ports;
        Connections;
        Interfaces;
    end
    
    methods
        %% Constructor function.
        function obj = ImportModelFromExcel(xlPath, compSheetName, portSheetName, conxnSheetName,portInterfaceSheetName)
            % This is a constructor function to class ImportModelFromExcel.
            % This function sets up the sheet name to read for different system
            % composer model elements.
            assert(~isempty(xlPath))
            
            obj.ExcelName = xlPath;
            
            % Hierarchy Info sheet.
            obj.ComponentSheetName = compSheetName;
            
            % Ports Info sheet.
            obj.PortSheetName = portSheetName;
            
            % Connections Info sheet.
            obj.ConnectionSheetName = conxnSheetName;
            
            % Port interfaces sheet name.
            obj.PortInterfaceSheetName = portInterfaceSheetName;
        end
        
        %% readTableFromExcel : Creates MATLAB tables from the Excel file.
        function readTableFromExcel(obj)
            % Function reads the Excel file and creates MATLAB tables.
            % Check if the Excel file exists
            if obj.excelFileExists()
                % Check if the componentSheetName exists.
                [~,B] = xlsfinfo(obj.ExcelName);
                sheetValid = any(strcmp(B, obj.ComponentSheetName));
                if sheetValid
                    % Create MATLAB tables from the excel sheet.
                    table = readtable(obj.ExcelName,'Sheet',obj.ComponentSheetName,'ReadVariableNames',true);
                    c = table2cell(table) ;
                    c(ismissing(table)) = {''};
                    obj.Components = cell2table(c, 'VariableNames',table.Properties.VariableNames) ;
                else
                    return;
                end
                
                % Check if the portSheetName exists.
                if ~isempty(obj.PortSheetName)
                    % If the port sheet name is not empty, the user has
                    % specified the worksheet to import port names from.
                    sheetValid = any(strcmp(B, obj.PortSheetName));
                    if sheetValid
                        % Create MATLAB tables from the excel sheet.
                        table = readtable(obj.ExcelName,'Sheet',obj.PortSheetName,'ReadVariableNames',true);
                        c = table2cell(table) ;
                        c(ismissing(table)) = {''};
                        obj.Ports = cell2table(c, 'VariableNames',table.Properties.VariableNames) ;
                    else
                        return;
                    end
                end
                
                % Check if the connectionSheetName exists.
                if ~isempty(obj.ConnectionSheetName)
                    % If the connection sheet name is not empty, the user has
                    % specified the worksheet to import connection names
                    % from.
                    sheetValid = any(strcmp(B, obj.ConnectionSheetName));
                    if sheetValid
                        % Create MATLAB tables from the excel sheet.
                        table = readtable(obj.ExcelName,'Sheet',obj.ConnectionSheetName,'ReadVariableNames',true);
                        c = table2cell(table) ;
                        c(ismissing(table)) = {''};
                        obj.Connections = cell2table(c, 'VariableNames',table.Properties.VariableNames) ;
                    else
                        return;
                    end
                end
                
                % Check if the portInterfaceSheetName exists.
                if ~isempty(obj.PortInterfaceSheetName)
                    % If the connection sheet name is not empty, the user has
                    % specified the worksheet to import port interface
                    % names from.
                    sheetValid = any(strcmp(B, obj.PortInterfaceSheetName));
                    if sheetValid
                        % Create MATLAB tables from the excel sheet.
                        table = readtable(obj.ExcelName,'Sheet',obj.PortInterfaceSheetName,'ReadVariableNames',true);
                        c = table2cell(table) ;
                        c(ismissing(table)) = {''};
                        obj.Interfaces = cell2table(c, 'VariableNames',table.Properties.VariableNames) ;
                    else
                        return;
                    end
                end
            end
        end
        
        %% Utility function to check if the file exists.
        function xlExists = excelFileExists(obj)
            xlExists = (exist(obj.ExcelName,'file') == 2);
        end
    end
end

