classdef app < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure                       matlab.ui.Figure
        CustomizePlotsPanel            matlab.ui.container.Panel
        DataTextArea                   matlab.ui.control.TextArea
        DataTextAreaLabel              matlab.ui.control.Label
        StatusTextArea                 matlab.ui.control.TextArea
        StatusTextAreaLabel            matlab.ui.control.Label
        SetTimestempsPanel             matlab.ui.container.Panel
        UseCheckBox                    matlab.ui.control.CheckBox
        Label_4                        matlab.ui.control.Label
        end_h                          matlab.ui.control.NumericEditField
        Label_3                        matlab.ui.control.Label
        end_s                          matlab.ui.control.NumericEditField
        end_m                          matlab.ui.control.NumericEditField
        StopLabel                      matlab.ui.control.Label
        StartLabel                     matlab.ui.control.Label
        Label_2                        matlab.ui.control.Label
        start_h                        matlab.ui.control.NumericEditField
        Label                          matlab.ui.control.Label
        start_s                        matlab.ui.control.NumericEditField
        start_m                        matlab.ui.control.NumericEditField
        CustomSubplotsPanel            matlab.ui.container.Panel
        yAxisTitleEditField            matlab.ui.control.EditField
        yAxisTitleEditFieldLabel       matlab.ui.control.Label
        SubplotTitleEditField          matlab.ui.control.EditField
        SubplotTitleEditFieldLabel     matlab.ui.control.Label
        OverallTitleEditField          matlab.ui.control.EditField
        OverallTitleEditFieldLabel     matlab.ui.control.Label
        Field_NoSP                     matlab.ui.control.NumericEditField
        textfield                      matlab.ui.control.EditField
        ResetSubplotButton             matlab.ui.control.Button
        SelectButton                   matlab.ui.control.Button
        Field_SelectedSP               matlab.ui.control.NumericEditField
        SelectedSubplotEditFieldLabel  matlab.ui.control.Label
        PlotSubplotButton              matlab.ui.control.Button
        SelectDataListBox              matlab.ui.control.ListBox
        SelectDataLabel                matlab.ui.control.Label
        ColumnsEditField               matlab.ui.control.NumericEditField
        ColumnsLabel                   matlab.ui.control.Label
        RowsEditField                  matlab.ui.control.NumericEditField
        Rowsmax2EditFieldLabel         matlab.ui.control.Label
        DefaultPlottypeDropDown        matlab.ui.control.DropDown
        PlottypeLabel                  matlab.ui.control.Label
        PlotButton                     matlab.ui.control.Button
        LoadDefaultButton              matlab.ui.control.Button
        EditField                      matlab.ui.control.EditField
        BrowseButton                   matlab.ui.control.Button
    end

    
    properties (Access = public)
        pltData = 0;
        filepath_last = "none"; % Description
        filepath = "none";
        fig = [];
        save_path = "plots\";
    end
    
    methods (Access = private)
        
        
    end
    
    methods (Access = public)
        
        % browse: choose file to load (place .mat file into data)
        function browse(app)
            f = figure('Renderer', 'painters', 'Position', [-100 -100 0 0]); %create a dummy figure so that uigetfile doesn't minimize our GUI
            [name, location] = uigetfile(...
                {'*.m;*.mat',...
                'MATLAB Files (*.m,*.mat)';
                '*.*',  'All Files (*.*)'}, ...
                "Select File","data\");
            app.filepath = append(location,name);
            app.EditField.Value = app.filepath;
            delete(f); %delete the dummy figure
        end

        % load: loads data based on default configuration (ABX,Shunt,...)
        % and generetes list of all data avaible
        function load(app)
            % check if data is already loaded
            if strcmp(app.filepath,app.filepath_last) == false
                % load data via vector2timetable (see podio)
                data = vector2timetable(app.filepath, "ABX*","Shunt*","AMS_Status*","INV*");
                data_fn = load(app.filepath);
                % generate list of all avaible data
                fn = fieldnames(data_fn);
                app.SelectDataListBox.Items = fn.'
            else
                msgbox("Data already loaded!");
            end
        app.filepath_last = app.filepath;
        end

        % startup function to set additional paths
        function startup(app)
            addpath("assets\");
            addpath("plot_settings\");
        end

        % adds subplots in fig[1], fig[2], ...
        function subplotting(app)
            app.fig(app.Field_SelectedSP.Value) = subplot(app.ColumnsEditField.Value, app.RowsEditField.Value, app.Field_SelectedSP.Value);
        end

        % reads additional data from file
        function data_selected = read_selected(app)
            fn_selected = app.SelectDataListBox.Value;
            data_selected = vector2timetable(app.filepath,fn_selected{:});
            if app.UseCheckBox.Value == 1
                tr = set_tr(app, data_selected);
                data_selected = data_selected(tr,:);
            end
            subplotting(app);
            hold on
            for i=1:length(fn_selected)
                plot(data_selected.Time, data_selected.(fn_selected{i}));
            end
            hold off
        end

        function ResetSubplot(app)
            delete(app.fig(app.Field_SelectedSP.Value));
        end
        
        function tr = set_tr(app, data_selected)
            date = string(datetime(data_selected.Time(1), 'Format', 'dd-MMM-yyyy'));
            start = date + " "+ string(app.start_h.Value) + ":" + string(app.start_m.Value) + ":"  + string(app.start_s.Value);
            stop = date + " "+ string(app.end_h.Value) + ":"  + string(app.end_m.Value) + ":"  + string(app.end_s.Value);
            tr = timerange(start, stop);
        end
    end
    

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

        % Code that executes after component creation
        function startupFcn(app)
            startup(app);
        end

        % Button pushed function: BrowseButton
        function BrowseButtonPushed(app, event)
            browse(app);
        end

        % Button pushed function: LoadDefaultButton
        function LoadDefaultButtonPushed(app, event)
            app.StatusTextArea.Value = "Loading up to 1min...";
            pause(0.01);
            load(app);
            app.StatusTextArea.Value = "Finished Loading";
            pause(0.01);
        end

        % Value changed function: SelectDataListBox
        function ItemsSelected(app, event)
            if app.SelectDataListBox.Value ~= "none"
                value = app.SelectDataListBox.Value;
            else
                value = "";
            end
            app.DataTextArea.Value = value;
        end

        % Value changed function: ColumnsEditField, RowsEditField
        function NumSP(app, event)
            rows = app.RowsEditField.Value;
            cols = app.ColumnsEditField.Value;
            app.Field_NoSP.Value = rows * cols;
        end

        % Button pushed function: PlotSubplotButton
        function PlotSubplotButtonPushed(app, event)
            app.StatusTextArea.Value = "Loading...";
            pause(0.01);
            read_selected(app);
            app.StatusTextArea.Value = "Finished Loading";
            pause(0.01);
            subplotting(app);
        end

        % Button pushed function: ResetSubplotButton
        function ResetSubplotButtonPushed(app, event)
            ResetSubplot(app);
        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.Position = [100 100 1090 613];
            app.UIFigure.Name = 'MATLAB App';
            app.UIFigure.Scrollable = 'on';

            % Create BrowseButton
            app.BrowseButton = uibutton(app.UIFigure, 'push');
            app.BrowseButton.ButtonPushedFcn = createCallbackFcn(app, @BrowseButtonPushed, true);
            app.BrowseButton.Position = [22 579 100 23];
            app.BrowseButton.Text = 'Browse';

            % Create EditField
            app.EditField = uieditfield(app.UIFigure, 'text');
            app.EditField.Position = [129 579 385 22];
            app.EditField.Value = '/data';

            % Create LoadDefaultButton
            app.LoadDefaultButton = uibutton(app.UIFigure, 'push');
            app.LoadDefaultButton.ButtonPushedFcn = createCallbackFcn(app, @LoadDefaultButtonPushed, true);
            app.LoadDefaultButton.Position = [527 579 100 23];
            app.LoadDefaultButton.Text = 'Load Default';

            % Create PlotButton
            app.PlotButton = uibutton(app.UIFigure, 'push');
            app.PlotButton.Position = [526 546 100 23];
            app.PlotButton.Text = 'Plot';

            % Create PlottypeLabel
            app.PlottypeLabel = uilabel(app.UIFigure);
            app.PlottypeLabel.HorizontalAlignment = 'right';
            app.PlottypeLabel.Position = [23 546 96 22];
            app.PlottypeLabel.Text = 'Default Plot type:';

            % Create DefaultPlottypeDropDown
            app.DefaultPlottypeDropDown = uidropdown(app.UIFigure);
            app.DefaultPlottypeDropDown.Position = [131 546 385 22];

            % Create CustomSubplotsPanel
            app.CustomSubplotsPanel = uipanel(app.UIFigure);
            app.CustomSubplotsPanel.TitlePosition = 'centertop';
            app.CustomSubplotsPanel.Title = 'Custom Subplots';
            app.CustomSubplotsPanel.Position = [21 62 250 459];

            % Create Rowsmax2EditFieldLabel
            app.Rowsmax2EditFieldLabel = uilabel(app.CustomSubplotsPanel);
            app.Rowsmax2EditFieldLabel.HorizontalAlignment = 'right';
            app.Rowsmax2EditFieldLabel.Position = [16 374 82 22];
            app.Rowsmax2EditFieldLabel.Text = 'Rows  (max.2)';

            % Create RowsEditField
            app.RowsEditField = uieditfield(app.CustomSubplotsPanel, 'numeric');
            app.RowsEditField.Limits = [1 2];
            app.RowsEditField.ValueChangedFcn = createCallbackFcn(app, @NumSP, true);
            app.RowsEditField.Position = [108 374 132 22];
            app.RowsEditField.Value = 1;

            % Create ColumnsLabel
            app.ColumnsLabel = uilabel(app.CustomSubplotsPanel);
            app.ColumnsLabel.HorizontalAlignment = 'right';
            app.ColumnsLabel.Position = [46 406 52 22];
            app.ColumnsLabel.Text = 'Columns';

            % Create ColumnsEditField
            app.ColumnsEditField = uieditfield(app.CustomSubplotsPanel, 'numeric');
            app.ColumnsEditField.Limits = [1 5];
            app.ColumnsEditField.ValueChangedFcn = createCallbackFcn(app, @NumSP, true);
            app.ColumnsEditField.Position = [109 406 131 22];
            app.ColumnsEditField.Value = 1;

            % Create SelectDataLabel
            app.SelectDataLabel = uilabel(app.CustomSubplotsPanel);
            app.SelectDataLabel.Position = [7 212 67 22];
            app.SelectDataLabel.Text = 'Select Data';

            % Create SelectDataListBox
            app.SelectDataListBox = uilistbox(app.CustomSubplotsPanel);
            app.SelectDataListBox.Multiselect = 'on';
            app.SelectDataListBox.ValueChangedFcn = createCallbackFcn(app, @ItemsSelected, true);
            app.SelectDataListBox.Position = [6 48 239 158];
            app.SelectDataListBox.Value = {'Item 1'};

            % Create PlotSubplotButton
            app.PlotSubplotButton = uibutton(app.CustomSubplotsPanel, 'push');
            app.PlotSubplotButton.ButtonPushedFcn = createCallbackFcn(app, @PlotSubplotButtonPushed, true);
            app.PlotSubplotButton.Position = [17 16 100 23];
            app.PlotSubplotButton.Text = 'Plot Subplot';

            % Create SelectedSubplotEditFieldLabel
            app.SelectedSubplotEditFieldLabel = uilabel(app.CustomSubplotsPanel);
            app.SelectedSubplotEditFieldLabel.HorizontalAlignment = 'right';
            app.SelectedSubplotEditFieldLabel.Position = [2 341 99 22];
            app.SelectedSubplotEditFieldLabel.Text = 'Selected Subplot';

            % Create Field_SelectedSP
            app.Field_SelectedSP = uieditfield(app.CustomSubplotsPanel, 'numeric');
            app.Field_SelectedSP.Limits = [1 10];
            app.Field_SelectedSP.Position = [109 342 27 22];
            app.Field_SelectedSP.Value = 1;

            % Create SelectButton
            app.SelectButton = uibutton(app.CustomSubplotsPanel, 'push');
            app.SelectButton.Position = [194 341 51 23];
            app.SelectButton.Text = 'Select';

            % Create ResetSubplotButton
            app.ResetSubplotButton = uibutton(app.CustomSubplotsPanel, 'push');
            app.ResetSubplotButton.ButtonPushedFcn = createCallbackFcn(app, @ResetSubplotButtonPushed, true);
            app.ResetSubplotButton.Position = [126 16 100 23];
            app.ResetSubplotButton.Text = 'Reset Subplot';

            % Create textfield
            app.textfield = uieditfield(app.CustomSubplotsPanel, 'text');
            app.textfield.Editable = 'off';
            app.textfield.Enable = 'off';
            app.textfield.Position = [145 341 13 22];
            app.textfield.Value = '/';

            % Create Field_NoSP
            app.Field_NoSP = uieditfield(app.CustomSubplotsPanel, 'numeric');
            app.Field_NoSP.Editable = 'off';
            app.Field_NoSP.Position = [165 341 24 22];
            app.Field_NoSP.Value = 1;

            % Create OverallTitleEditFieldLabel
            app.OverallTitleEditFieldLabel = uilabel(app.CustomSubplotsPanel);
            app.OverallTitleEditFieldLabel.HorizontalAlignment = 'right';
            app.OverallTitleEditFieldLabel.Position = [6 308 68 22];
            app.OverallTitleEditFieldLabel.Text = 'Overall Title';

            % Create OverallTitleEditField
            app.OverallTitleEditField = uieditfield(app.CustomSubplotsPanel, 'text');
            app.OverallTitleEditField.Position = [109 308 131 22];

            % Create SubplotTitleEditFieldLabel
            app.SubplotTitleEditFieldLabel = uilabel(app.CustomSubplotsPanel);
            app.SubplotTitleEditFieldLabel.HorizontalAlignment = 'right';
            app.SubplotTitleEditFieldLabel.Position = [6 273 71 22];
            app.SubplotTitleEditFieldLabel.Text = 'Subplot Title';

            % Create SubplotTitleEditField
            app.SubplotTitleEditField = uieditfield(app.CustomSubplotsPanel, 'text');
            app.SubplotTitleEditField.Position = [92 273 148 22];

            % Create yAxisTitleEditFieldLabel
            app.yAxisTitleEditFieldLabel = uilabel(app.CustomSubplotsPanel);
            app.yAxisTitleEditFieldLabel.HorizontalAlignment = 'right';
            app.yAxisTitleEditFieldLabel.Position = [11 243 70 22];
            app.yAxisTitleEditFieldLabel.Text = 'y-Axis - Title';

            % Create yAxisTitleEditField
            app.yAxisTitleEditField = uieditfield(app.CustomSubplotsPanel, 'text');
            app.yAxisTitleEditField.Position = [96 243 144 22];

            % Create SetTimestempsPanel
            app.SetTimestempsPanel = uipanel(app.UIFigure);
            app.SetTimestempsPanel.Title = 'Set Timestemps';
            app.SetTimestempsPanel.Position = [291 437 237 84];

            % Create start_m
            app.start_m = uieditfield(app.SetTimestempsPanel, 'numeric');
            app.start_m.Limits = [0 59];
            app.start_m.ValueDisplayFormat = '%02.0f';
            app.start_m.Position = [101 33 30 22];

            % Create start_s
            app.start_s = uieditfield(app.SetTimestempsPanel, 'numeric');
            app.start_s.Limits = [0 59];
            app.start_s.ValueDisplayFormat = '%02.0f';
            app.start_s.Position = [141 33 32 22];

            % Create Label
            app.Label = uilabel(app.SetTimestempsPanel);
            app.Label.HorizontalAlignment = 'center';
            app.Label.Position = [90 33 10 22];
            app.Label.Text = ':';

            % Create start_h
            app.start_h = uieditfield(app.SetTimestempsPanel, 'numeric');
            app.start_h.Limits = [0 23];
            app.start_h.ValueDisplayFormat = '%02.0f';
            app.start_h.Position = [60 33 30 22];

            % Create Label_2
            app.Label_2 = uilabel(app.SetTimestempsPanel);
            app.Label_2.HorizontalAlignment = 'center';
            app.Label_2.Position = [131 33 10 22];
            app.Label_2.Text = ':';

            % Create StartLabel
            app.StartLabel = uilabel(app.SetTimestempsPanel);
            app.StartLabel.Position = [15 33 37 22];
            app.StartLabel.Text = 'Start';

            % Create StopLabel
            app.StopLabel = uilabel(app.SetTimestempsPanel);
            app.StopLabel.Position = [15 4 37 22];
            app.StopLabel.Text = 'Stop';

            % Create end_m
            app.end_m = uieditfield(app.SetTimestempsPanel, 'numeric');
            app.end_m.Limits = [0 59];
            app.end_m.ValueDisplayFormat = '%02.0f';
            app.end_m.Position = [101 4 30 22];

            % Create end_s
            app.end_s = uieditfield(app.SetTimestempsPanel, 'numeric');
            app.end_s.Limits = [0 59];
            app.end_s.ValueDisplayFormat = '%02.0f';
            app.end_s.Position = [141 4 32 22];

            % Create Label_3
            app.Label_3 = uilabel(app.SetTimestempsPanel);
            app.Label_3.HorizontalAlignment = 'center';
            app.Label_3.Position = [90 4 10 22];
            app.Label_3.Text = ':';

            % Create end_h
            app.end_h = uieditfield(app.SetTimestempsPanel, 'numeric');
            app.end_h.Limits = [0 23];
            app.end_h.ValueDisplayFormat = '%02.0f';
            app.end_h.Position = [60 4 30 22];

            % Create Label_4
            app.Label_4 = uilabel(app.SetTimestempsPanel);
            app.Label_4.HorizontalAlignment = 'center';
            app.Label_4.Position = [131 4 10 22];
            app.Label_4.Text = ':';

            % Create UseCheckBox
            app.UseCheckBox = uicheckbox(app.SetTimestempsPanel);
            app.UseCheckBox.Text = 'Use';
            app.UseCheckBox.Position = [191 20 45 22];

            % Create StatusTextAreaLabel
            app.StatusTextAreaLabel = uilabel(app.UIFigure);
            app.StatusTextAreaLabel.HorizontalAlignment = 'right';
            app.StatusTextAreaLabel.FontColor = [1 0 0];
            app.StatusTextAreaLabel.Position = [27 15 39 22];
            app.StatusTextAreaLabel.Text = 'Status';

            % Create StatusTextArea
            app.StatusTextArea = uitextarea(app.UIFigure);
            app.StatusTextArea.Editable = 'off';
            app.StatusTextArea.FontColor = [1 0 0];
            app.StatusTextArea.Position = [81 16 166 23];

            % Create DataTextAreaLabel
            app.DataTextAreaLabel = uilabel(app.UIFigure);
            app.DataTextAreaLabel.HorizontalAlignment = 'right';
            app.DataTextAreaLabel.Position = [609 495 30 22];
            app.DataTextAreaLabel.Text = 'Data';

            % Create DataTextArea
            app.DataTextArea = uitextarea(app.UIFigure);
            app.DataTextArea.Editable = 'off';
            app.DataTextArea.Position = [654 267 356 252];

            % Create CustomizePlotsPanel
            app.CustomizePlotsPanel = uipanel(app.UIFigure);
            app.CustomizePlotsPanel.Title = 'Customize Plots';
            app.CustomizePlotsPanel.Position = [292 64 236 361];

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

            % Create UIFigure and components
            createComponents(app)

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

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

            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