classdef visualize_app < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure        matlab.ui.Figure
        GridLayout      matlab.ui.container.GridLayout
        LeftPanel       matlab.ui.container.Panel
        Button          matlab.ui.control.Button
        EditField       matlab.ui.control.EditField
        EditFieldLabel  matlab.ui.control.Label
        RightPanel      matlab.ui.container.Panel
        UIAxes          matlab.ui.control.UIAxes
    end

    % Properties that correspond to apps with auto-reflow
    properties (Access = private)
        onePanelWidth = 576;
    end

    % Callbacks that handle component events
    methods (Access = private)

        % Button pushed function: Button
        function ButtonPushed(app, event)
                        % Read the input key
            key = app.EditField.Value;

            % Read the data and create the connections map
            data = readtable('MGREL.RRF', 'Delimiter', '|', 'FileType', 'text', 'NumHeaderLines', 0, 'VariableNamingRule', 'preserve');
            data = renamevars(data, '#CUI1', 'CUI1');
            data = data(1:1000,:); 

            connectionsMap = containers.Map('KeyType', 'char', 'ValueType', 'char');

            for i = 1:size(data, 1)
                node = data{i, 'CUI1'};
                char_node = char(node);

                connectedNode = data{i, 'CUI2'};
                char_connectedNode = char(connectedNode);

                if isKey(connectionsMap, char_node)
                    connectionsMap(char_node) = [connectionsMap(char_node), '|', char_connectedNode];
                else
                    connectionsMap(char_node) = char_connectedNode;
                end

               
                if isKey(connectionsMap, char_connectedNode)
                    connectionsMap(char_connectedNode) = [connectionsMap(char_connectedNode), '|', char_node];
                else
                    connectionsMap(char_connectedNode) = char_node;
                end

            end

            % Check if the key exists in the connectionsMap
            if isKey(connectionsMap, key)
                % Split the value string by the pipe symbol '|'
                split_values = strsplit(connectionsMap(key), '|');

                % Visualize the graph
                G = graph();
                for i = 1:length(split_values)
                    G = addedge(G, key, split_values{i});
                end

                % Plot the graph
                plot(app.UIAxes, G, 'Layout', 'force');
                title(app.UIAxes, 'Graph of Connected Nodes');
            else
                disp('Key does not exist in the connectionsMap.');
            end
        end

        % Changes arrangement of the app based on UIFigure width
        function updateAppLayout(app, event)
            currentFigureWidth = app.UIFigure.Position(3);
            if(currentFigureWidth <= app.onePanelWidth)
                % Change to a 2x1 grid
                app.GridLayout.RowHeight = {480, 480};
                app.GridLayout.ColumnWidth = {'1x'};
                app.RightPanel.Layout.Row = 2;
                app.RightPanel.Layout.Column = 1;
            else
                % Change to a 1x2 grid
                app.GridLayout.RowHeight = {'1x'};
                app.GridLayout.ColumnWidth = {220, '1x'};
                app.RightPanel.Layout.Row = 1;
                app.RightPanel.Layout.Column = 2;
            end
        end
    end

    % Component initialization
    methods (Access = private)

        % Create UIFigure and components
        function createComponents(app)

            % Create UIFigure and hide until all components are created
            app.UIFigure = uifigure('Visible', 'off');
            app.UIFigure.AutoResizeChildren = 'off';
            app.UIFigure.Position = [100 100 640 480];
            app.UIFigure.Name = 'MATLAB App';
            app.UIFigure.SizeChangedFcn = createCallbackFcn(app, @updateAppLayout, true);

            % Create GridLayout
            app.GridLayout = uigridlayout(app.UIFigure);
            app.GridLayout.ColumnWidth = {220, '1x'};
            app.GridLayout.RowHeight = {'1x'};
            app.GridLayout.ColumnSpacing = 0;
            app.GridLayout.RowSpacing = 0;
            app.GridLayout.Padding = [0 0 0 0];
            app.GridLayout.Scrollable = 'on';

            % Create LeftPanel
            app.LeftPanel = uipanel(app.GridLayout);
            app.LeftPanel.Layout.Row = 1;
            app.LeftPanel.Layout.Column = 1;

            % Create EditFieldLabel
            app.EditFieldLabel = uilabel(app.LeftPanel);
            app.EditFieldLabel.HorizontalAlignment = 'right';
            app.EditFieldLabel.Position = [25 279 55 22];
            app.EditFieldLabel.Text = 'Edit Field';

            % Create EditField
            app.EditField = uieditfield(app.LeftPanel, 'text');
            app.EditField.Position = [95 279 100 22];

            % Create Button
            app.Button = uibutton(app.LeftPanel, 'push');
            app.Button.ButtonPushedFcn = createCallbackFcn(app, @ButtonPushed, true);
            app.Button.Position = [60 228 100 23];

            % Create RightPanel
            app.RightPanel = uipanel(app.GridLayout);
            app.RightPanel.Layout.Row = 1;
            app.RightPanel.Layout.Column = 2;

            % Create UIAxes
            app.UIAxes = uiaxes(app.RightPanel);
            title(app.UIAxes, 'Title')
            xlabel(app.UIAxes, 'X')
            ylabel(app.UIAxes, 'Y')
            zlabel(app.UIAxes, 'Z')
            app.UIAxes.Position = [6 1 414 473];

            % Show the figure after all components are created
            app.UIFigure.Visible = 'on';
        end
    end

    % App creation and deletion
    methods (Access = public)

        % Construct app
        function app = visualize_app

            % Create UIFigure and components
            createComponents(app)

            % Register the app with App Designer
            registerApp(app, app.UIFigure)

            if nargout == 0
                clear app
            end
        end

        % Code that executes before app deletion
        function delete(app)

            % Delete UIFigure when app is deleted
            delete(app.UIFigure)
        end
    end
end