classdef graph_visualization < 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
        FoundDiseasesListBox       matlab.ui.control.ListBox
        FoundDiseasesListBoxLabel  matlab.ui.control.Label
        DiseasenameEditField       matlab.ui.control.EditField
        DiseasenameEditFieldLabel  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)

        % Code that executes after component creation
        function startupFcn(app)
            data = readtable('MGREL.RRF', Delimiter='|', FileType='text', NumHeaderLines=0, VariableNamingRule='preserve');
            data = renamevars(data,"#CUI1","CUI1");
        end

        % Button down function: UIAxes
        function UIAxesButtonDown(app, event)
            s = [1 2 2 3 3 3 4 5 5 5 8 8 9];
            t = [2 3 4 1 4 5 5 3 6 7 9 10 10];
            G = digraph(s,t,[],20);
            plot(app.UIAxes, G,'Layout','layered')
        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 = {264, '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 684 480];
            app.UIFigure.Name = 'MATLAB App';
            app.UIFigure.SizeChangedFcn = createCallbackFcn(app, @updateAppLayout, true);

            % Create GridLayout
            app.GridLayout = uigridlayout(app.UIFigure);
            app.GridLayout.ColumnWidth = {264, '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 DiseasenameEditFieldLabel
            app.DiseasenameEditFieldLabel = uilabel(app.LeftPanel);
            app.DiseasenameEditFieldLabel.HorizontalAlignment = 'right';
            app.DiseasenameEditFieldLabel.Position = [40 404 81 22];
            app.DiseasenameEditFieldLabel.Text = 'Disease name';

            % Create DiseasenameEditField
            app.DiseasenameEditField = uieditfield(app.LeftPanel, 'text');
            app.DiseasenameEditField.Position = [136 404 100 22];

            % Create FoundDiseasesListBoxLabel
            app.FoundDiseasesListBoxLabel = uilabel(app.LeftPanel);
            app.FoundDiseasesListBoxLabel.HorizontalAlignment = 'right';
            app.FoundDiseasesListBoxLabel.Position = [30 343 91 22];
            app.FoundDiseasesListBoxLabel.Text = 'Found Diseases';

            % Create FoundDiseasesListBox
            app.FoundDiseasesListBox = uilistbox(app.LeftPanel);
            app.FoundDiseasesListBox.Position = [136 293 100 74];

            % 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, 'Graph')
            app.UIAxes.XTick = [];
            app.UIAxes.YTick = [];
            app.UIAxes.ButtonDownFcn = createCallbackFcn(app, @UIAxesButtonDown, true);
            app.UIAxes.Position = [1 6 413 468];

            % 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 = graph_visualization

            runningApp = getRunningApp(app);

            % Check for running singleton app
            if isempty(runningApp)

                % Create UIFigure and components
                createComponents(app)

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

                % Execute the startup function
                runStartupFcn(app, @startupFcn)
            else

                % Focus the running singleton app
                figure(runningApp.UIFigure)

                app = runningApp;
            end

            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