classdef CPFC3_autoreflow < 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
        ExitButton                   matlab.ui.control.Button
        StopButton                   matlab.ui.control.Button
        StartButton                  matlab.ui.control.Button
        RightPanel                   matlab.ui.container.Panel
        TabGroup                     matlab.ui.container.TabGroup
        DashboardTab                 matlab.ui.container.Tab
        UIAxes2_3                    matlab.ui.control.UIAxes
        UIAxes2_2                    matlab.ui.control.UIAxes
        UIAxes2                      matlab.ui.control.UIAxes
        UIAxes                       matlab.ui.control.UIAxes
        ScienceDataTab               matlab.ui.container.Tab
        EngrDataTab                  matlab.ui.container.Tab
        SendCommandTab               matlab.ui.container.Tab
        ClearCommandButton           matlab.ui.control.Button
        SendCommandButton            matlab.ui.control.Button
        hoursSpinner                 matlab.ui.control.Spinner
        hoursSpinnerLabel            matlab.ui.control.Label
        minutesSpinner               matlab.ui.control.Spinner
        minutesSpinnerLabel          matlab.ui.control.Label
        ParkTimeButton               matlab.ui.control.Button
        metersSpinner                matlab.ui.control.Spinner
        metersSpinnerLabel           matlab.ui.control.Label
        ParkDepthButton              matlab.ui.control.Button
        RecoveryModeFalseButton      matlab.ui.control.Button
        RecoveryModeTrueButton       matlab.ui.control.Button
        CommandStringEditField       matlab.ui.control.EditField
        CommandStringEditFieldLabel  matlab.ui.control.Label
        GoButton                     matlab.ui.control.Button
        DownloadDataTab              matlab.ui.container.Tab
        SetupTab                     matlab.ui.container.Tab
        ChangeDataFolderButton       matlab.ui.control.Button
        DataFolderEditField          matlab.ui.control.EditField
        DataFolderEditFieldLabel     matlab.ui.control.Label
        SBDFolderEditField           matlab.ui.control.EditField
        SBDFolderEditFieldLabel      matlab.ui.control.Label
        ChangeSBDFolderButton        matlab.ui.control.Button
    end

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


    properties (Access = private)
        RandTimer          % Timer object
        PlotLine           % Line object
        
        %TODO How do I make this a struct?
        latDD = [36.754699];
        lonDD = [-122.033034];
        northing;
        easting;

        positionNum = 0; % Description
    end
    
    methods (Access = private)
    
        function RandTimerFcn(app,~,~)
            app.positionNum = app.positionNum + 1;

            [app.northing(app.positionNum), app.easting(app.positionNum)] = DecDegToUTM(app, app.latDD(app.positionNum), app.lonDD(app.positionNum));

            app.northing(app.positionNum) = app.northing(app.positionNum) + 100 * randn(1);
            app.easting(app.positionNum) = app.easting(app.positionNum) + 100 * randn(1);
            
            app.latDD(app.positionNum + 1) = app.latDD(app.positionNum) + 0.0001;
            app.lonDD(app.positionNum + 1) = app.lonDD(app.positionNum) + 0.0001;

            app.PlotLine = plot(app.UIAxes, app.easting, app.northing, 'rx-', 'LineWidth', 2);
       end        
        
        
       function [northing, easting] = DecDegToUTM(~, latDD, lonDD)
           [easting, northing] = utmups_fwd(latDD, lonDD);
        end
    end


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

        % Code that executes after component creation
        function startupFcn(app)
            % Configure x- and y- axis
            app.UIAxes.DataAspectRatio = [1 1 1];

            %[mbay, R] = readgeoraster('montbay_colorshd.tif');
            %mapshow(app.UIAxes,mbay,R);
            hold(app.UIAxes, "on");

            % Create timer object
            app.RandTimer = timer(...
                'ExecutionMode', 'fixedRate', ...    % Run timer repeatedly
                'Period', 1, ...                     % Period is 1 second
                'BusyMode', 'queue',...              % Queue timer callbacks when busy
                'TimerFcn', @app.RandTimerFcn);      % Specify callback function
        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 = {629, 629};
                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 = {156, '1x'};
                app.RightPanel.Layout.Row = 1;
                app.RightPanel.Layout.Column = 2;
            end
        end

        % Button pushed function: StartButton
        function StartButtonPushed(app, event)
            % If timer is not running, start it
            if strcmp(app.RandTimer.Running, 'off')
               start(app.RandTimer);
            end
        end

        % Button pushed function: StopButton
        function StopButtonPushed(app, event)
            % Stop the timer
            stop(app.RandTimer);
        end

        % Close request function: UIFigure
        function UIFigureCloseRequest(app, event)
            % Stop timer, then delete timer and app
            close all
            stop(app.RandTimer);
            delete(app.RandTimer);
            delete(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.AutoResizeChildren = 'off';
            app.UIFigure.Position = [100 100 1238 629];
            app.UIFigure.Name = 'CPF Mission Control';
            app.UIFigure.CloseRequestFcn = createCallbackFcn(app, @UIFigureCloseRequest, true);
            app.UIFigure.SizeChangedFcn = createCallbackFcn(app, @updateAppLayout, true);

            % Create GridLayout
            app.GridLayout = uigridlayout(app.UIFigure);
            app.GridLayout.ColumnWidth = {156, '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 StartButton
            app.StartButton = uibutton(app.LeftPanel, 'push');
            app.StartButton.ButtonPushedFcn = createCallbackFcn(app, @StartButtonPushed, true);
            app.StartButton.Position = [26 574 100 22];
            app.StartButton.Text = 'Start';

            % Create StopButton
            app.StopButton = uibutton(app.LeftPanel, 'push');
            app.StopButton.ButtonPushedFcn = createCallbackFcn(app, @StopButtonPushed, true);
            app.StopButton.Position = [26 524 100 22];
            app.StopButton.Text = 'Stop';

            % Create ExitButton
            app.ExitButton = uibutton(app.LeftPanel, 'push');
            app.ExitButton.Position = [27 52 100 22];
            app.ExitButton.Text = 'Exit';

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

            % Create TabGroup
            app.TabGroup = uitabgroup(app.RightPanel);
            app.TabGroup.Position = [34 51 1011 577];

            % Create DashboardTab
            app.DashboardTab = uitab(app.TabGroup);
            app.DashboardTab.Title = 'Dashboard';

            % Create UIAxes
            app.UIAxes = uiaxes(app.DashboardTab);
            title(app.UIAxes, 'CPF Position')
            xlabel(app.UIAxes, 'Easting (meters)')
            ylabel(app.UIAxes, 'Northing (meters)')
            app.UIAxes.XGrid = 'on';
            app.UIAxes.YGrid = 'on';
            app.UIAxes.Box = 'on';
            app.UIAxes.Position = [13 32 627 463];

            % Create UIAxes2
            app.UIAxes2 = uiaxes(app.DashboardTab);
            title(app.UIAxes2, 'Battery Voltage')
            xlabel(app.UIAxes2, 'Profile Num')
            ylabel(app.UIAxes2, 'Bus Voltage')
            zlabel(app.UIAxes2, 'Z')
            app.UIAxes2.XGrid = 'on';
            app.UIAxes2.YGrid = 'on';
            app.UIAxes2.Position = [685 370 269 154];

            % Create UIAxes2_2
            app.UIAxes2_2 = uiaxes(app.DashboardTab);
            title(app.UIAxes2_2, 'Can Humidity')
            xlabel(app.UIAxes2_2, 'Profile Num')
            ylabel(app.UIAxes2_2, 'Humidity (RH)')
            zlabel(app.UIAxes2_2, 'Z')
            app.UIAxes2_2.XGrid = 'on';
            app.UIAxes2_2.YGrid = 'on';
            app.UIAxes2_2.Position = [686 32 269 145];

            % Create UIAxes2_3
            app.UIAxes2_3 = uiaxes(app.DashboardTab);
            title(app.UIAxes2_3, 'Descend Bellows Position')
            xlabel(app.UIAxes2_3, 'Profile Num')
            ylabel(app.UIAxes2_3, 'Bellows Position (mm)')
            zlabel(app.UIAxes2_3, 'Z')
            app.UIAxes2_3.XGrid = 'on';
            app.UIAxes2_3.YGrid = 'on';
            app.UIAxes2_3.Position = [685 206 269 154];

            % Create ScienceDataTab
            app.ScienceDataTab = uitab(app.TabGroup);
            app.ScienceDataTab.Title = 'Science Data';

            % Create EngrDataTab
            app.EngrDataTab = uitab(app.TabGroup);
            app.EngrDataTab.Title = 'Engr Data';

            % Create SendCommandTab
            app.SendCommandTab = uitab(app.TabGroup);
            app.SendCommandTab.Title = 'Send Command';

            % Create GoButton
            app.GoButton = uibutton(app.SendCommandTab, 'push');
            app.GoButton.Position = [118 473 100 22];
            app.GoButton.Text = 'Go';

            % Create CommandStringEditFieldLabel
            app.CommandStringEditFieldLabel = uilabel(app.SendCommandTab);
            app.CommandStringEditFieldLabel.HorizontalAlignment = 'right';
            app.CommandStringEditFieldLabel.Position = [51 104 96 22];
            app.CommandStringEditFieldLabel.Text = 'Command String';

            % Create CommandStringEditField
            app.CommandStringEditField = uieditfield(app.SendCommandTab, 'text');
            app.CommandStringEditField.Position = [125 104 547 22];

            % Create RecoveryModeTrueButton
            app.RecoveryModeTrueButton = uibutton(app.SendCommandTab, 'push');
            app.RecoveryModeTrueButton.Position = [82 435 137 22];
            app.RecoveryModeTrueButton.Text = 'Recovery Mode = True';

            % Create RecoveryModeFalseButton
            app.RecoveryModeFalseButton = uibutton(app.SendCommandTab, 'push');
            app.RecoveryModeFalseButton.Position = [75 399 143 22];
            app.RecoveryModeFalseButton.Text = 'Recovery Mode = False';

            % Create ParkDepthButton
            app.ParkDepthButton = uibutton(app.SendCommandTab, 'push');
            app.ParkDepthButton.Position = [118 366 100 22];
            app.ParkDepthButton.Text = 'Park Depth = ';

            % Create metersSpinnerLabel
            app.metersSpinnerLabel = uilabel(app.SendCommandTab);
            app.metersSpinnerLabel.HorizontalAlignment = 'right';
            app.metersSpinnerLabel.Position = [233 366 42 22];
            app.metersSpinnerLabel.Text = 'meters';

            % Create metersSpinner
            app.metersSpinner = uispinner(app.SendCommandTab);
            app.metersSpinner.Position = [290 366 100 22];

            % Create ParkTimeButton
            app.ParkTimeButton = uibutton(app.SendCommandTab, 'push');
            app.ParkTimeButton.Position = [118 326 100 22];
            app.ParkTimeButton.Text = 'Park Time = ';

            % Create minutesSpinnerLabel
            app.minutesSpinnerLabel = uilabel(app.SendCommandTab);
            app.minutesSpinnerLabel.HorizontalAlignment = 'right';
            app.minutesSpinnerLabel.Position = [403 326 48 22];
            app.minutesSpinnerLabel.Text = 'minutes';

            % Create minutesSpinner
            app.minutesSpinner = uispinner(app.SendCommandTab);
            app.minutesSpinner.Position = [466 326 100 22];

            % Create hoursSpinnerLabel
            app.hoursSpinnerLabel = uilabel(app.SendCommandTab);
            app.hoursSpinnerLabel.HorizontalAlignment = 'right';
            app.hoursSpinnerLabel.Position = [236 326 36 22];
            app.hoursSpinnerLabel.Text = 'hours';

            % Create hoursSpinner
            app.hoursSpinner = uispinner(app.SendCommandTab);
            app.hoursSpinner.Position = [287 326 74 22];

            % Create SendCommandButton
            app.SendCommandButton = uibutton(app.SendCommandTab, 'push');
            app.SendCommandButton.Position = [360 65 102 22];
            app.SendCommandButton.Text = 'Send Command';

            % Create ClearCommandButton
            app.ClearCommandButton = uibutton(app.SendCommandTab, 'push');
            app.ClearCommandButton.Position = [303 272 103 22];
            app.ClearCommandButton.Text = 'Clear Command';

            % Create DownloadDataTab
            app.DownloadDataTab = uitab(app.TabGroup);
            app.DownloadDataTab.Title = 'Download Data';

            % Create SetupTab
            app.SetupTab = uitab(app.TabGroup);
            app.SetupTab.Title = 'Setup';

            % Create ChangeSBDFolderButton
            app.ChangeSBDFolderButton = uibutton(app.SetupTab, 'push');
            app.ChangeSBDFolderButton.Position = [106 435 123 22];
            app.ChangeSBDFolderButton.Text = 'Change SBD Folder';

            % Create SBDFolderEditFieldLabel
            app.SBDFolderEditFieldLabel = uilabel(app.SetupTab);
            app.SBDFolderEditFieldLabel.HorizontalAlignment = 'right';
            app.SBDFolderEditFieldLabel.Position = [51 473 68 22];
            app.SBDFolderEditFieldLabel.Text = 'SBD Folder';

            % Create SBDFolderEditField
            app.SBDFolderEditField = uieditfield(app.SetupTab, 'text');
            app.SBDFolderEditField.Position = [134 473 100 22];

            % Create DataFolderEditFieldLabel
            app.DataFolderEditFieldLabel = uilabel(app.SetupTab);
            app.DataFolderEditFieldLabel.HorizontalAlignment = 'right';
            app.DataFolderEditFieldLabel.Position = [293 473 68 22];
            app.DataFolderEditFieldLabel.Text = 'Data Folder';

            % Create DataFolderEditField
            app.DataFolderEditField = uieditfield(app.SetupTab, 'text');
            app.DataFolderEditField.Position = [376 473 100 22];

            % Create ChangeDataFolderButton
            app.ChangeDataFolderButton = uibutton(app.SetupTab, 'push');
            app.ChangeDataFolderButton.Position = [364 435 124 22];
            app.ChangeDataFolderButton.Text = 'Change Data Folder';

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

            % 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