classdef sf_lamp_ui < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure         matlab.ui.Figure
        Lamp             matlab.ui.control.Lamp
        ModeButtonGroup  matlab.ui.container.ButtonGroup
        SolidButton      matlab.ui.control.RadioButton
        BlinkingButton   matlab.ui.control.RadioButton
        BlinkRateflashespersecSliderLabel  matlab.ui.control.Label
        BlinkRateSlider  matlab.ui.control.Slider
        Label            matlab.ui.control.Label
        Switch           matlab.ui.control.Switch
    end

    properties (Access = private)
        lampLogic
    end

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

        % Code that executes after component creation
        function startupFcn(app)
            app.lampLogic = sf_lamp_logic('delay',0.5,'app',app);            
        end

        % Value changed function: Switch
        function SwitchValueChanged(app, event)
            value = app.Switch.Value;
            switch lower(value)
                case 'off'
                    OFF(app.lampLogic);
                case 'on'
                    ON(app.lampLogic);
            end            
        end

        % Selection changed function: ModeButtonGroup
        function ModeButtonGroupSelectionChanged(app, event)
            selectedButton = app.ModeButtonGroup.SelectedObject;
            if app.SolidButton == selectedButton
                SOLID(app.lampLogic);
            else
                BLINKING(app.lampLogic);
            end
        end

        % Value changed function: BlinkRateSlider
        function BlinkRateSliderValueChanged(app, event)
            app.lampLogic.delay = round(0.5/app.BlinkRateSlider.Value,2);            
        end

        % Close request function: UIFigure
        function UIFigureCloseRequest(app, event)
            delete(app.lampLogic);
            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.Position = [100 100 359 202];
            app.UIFigure.Name = 'UI Figure';
            app.UIFigure.CloseRequestFcn = createCallbackFcn(app, @UIFigureCloseRequest, true);

            % Create Lamp
            app.Lamp = uilamp(app.UIFigure);
            app.Lamp.Position = [215 123 58 58];
            app.Lamp.Color = [0.302 0.7451 0.9333];

            % Create ModeButtonGroup
            app.ModeButtonGroup = uibuttongroup(app.UIFigure);
            app.ModeButtonGroup.SelectionChangedFcn = createCallbackFcn(app, @ModeButtonGroupSelectionChanged, true);
            app.ModeButtonGroup.Title = 'Mode';
            app.ModeButtonGroup.Position = [41 28 100 85];

            % Create SolidButton
            app.SolidButton = uiradiobutton(app.ModeButtonGroup);
            app.SolidButton.Text = 'Solid';
            app.SolidButton.Position = [11 39 50 15];
            app.SolidButton.Value = true;

            % Create BlinkingButton
            app.BlinkingButton = uiradiobutton(app.ModeButtonGroup);
            app.BlinkingButton.Text = 'Blinking';
            app.BlinkingButton.Position = [11 17 66 15];

            % Create BlinkRateflashespersecSliderLabel
            app.BlinkRateflashespersecSliderLabel = uilabel(app.UIFigure);
            app.BlinkRateflashespersecSliderLabel.HorizontalAlignment = 'center';
            app.BlinkRateflashespersecSliderLabel.VerticalAlignment = 'top';
            app.BlinkRateflashespersecSliderLabel.Position = [169 28 152 22];
            app.BlinkRateflashespersecSliderLabel.Text = 'Blink Rate (flashes per sec)';

            % Create BlinkRateSlider
            app.BlinkRateSlider = uislider(app.UIFigure);
            app.BlinkRateSlider.Limits = [0.5 3];
            app.BlinkRateSlider.ValueChangedFcn = createCallbackFcn(app, @BlinkRateSliderValueChanged, true);
            app.BlinkRateSlider.Position = [170 89 150 3];
            app.BlinkRateSlider.Value = 1;

            % Create Label
            app.Label = uilabel(app.UIFigure);
            app.Label.HorizontalAlignment = 'center';
            app.Label.VerticalAlignment = 'top';
            app.Label.Position = [79 112 25 15];
            app.Label.Text = '';

            % Create Switch
            app.Switch = uiswitch(app.UIFigure, 'slider');
            app.Switch.ValueChangedFcn = createCallbackFcn(app, @SwitchValueChanged, true);
            app.Switch.Position = [69 142 45 20];

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

            % 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