function refreshTimerFcn(obj, ~, ~)
%REFRESHTIMERFCN Refresh timer's timer-function.
%   Used for updating simulation controls and time label based on
%   simulation status and time. 

%   Copyright 2021-2024 The MathWorks, Inc.

    app = obj.App;

    if ~isvalid(app)
      return;
    end

    if isempty(slsim.SimulationStatus(app.Simulation.Status))
      % short-circuit until a status is available.
      return;
    end

    switch (slsim.SimulationStatus(app.Simulation.Status))
        case slsim.SimulationStatus.Inactive
            
        case slsim.SimulationStatus.Initializing
            app.StopButton.Enable = 'on';
            app.StatusLabel.Text = message("simulinkcompiler:genapp:InitializingStatusLabel").string;
        
        case slsim.SimulationStatus.Initialized
            app.StopButton.Enable = 'on';
            app.modelData.simOut = app.Simulation.SimulationOutput;

        case slsim.SimulationStatus.Running

            obj.debugTrace('Getting simout');

            app.StopButton.Enable = 'on';
            app.RunPauseResumeButton.Enable = 'on';
            app.StatusLabel.Text = message("simulinkcompiler:genapp:RunningStatusLabel").string;
            app.RunPauseResumeButton.Icon = fullfile(app.AssetsFolder, 'images', 'icon_pause.png');
            
            % get the simulation output
            pause(app.Simulation);
            app.modelData.simOut = app.Simulation.SimulationOutput;
            resume(app.Simulation);
            % process the simulation results
            obj.processSimOut();
            % update plots
            obj.updateSigsAndAxesGL();
            % Make sure the plots are visible
            obj.showPanels(["plots"]); %#ok

        case slsim.SimulationStatus.Paused
            app.StopButton.Enable = 'on';
            app.RunPauseResumeButton.Enable = 'on';
            app.StatusLabel.Text = message("simulinkcompiler:genapp:PausedStatusLabel").string;
            app.RunPauseResumeButton.Icon = fullfile(app.AssetsFolder, 'images', 'icon_resume.png');

        case slsim.SimulationStatus.Stopping
            app.StopButton.Enable = 'off';
            app.RunPauseResumeButton.Enable = 'off';
            app.StatusLabel.Text = message("simulinkcompiler:genapp:StoppingStatusLabel").string;

        case slsim.SimulationStatus.Terminating
            app.StopButton.Enable = 'off';
            app.RunPauseResumeButton.Enable = 'off';
            app.StatusLabel.Text = message("simulinkcompiler:genapp:TerminatingStatusLabel").string;
    end

    simTime = app.Simulation.Time;
    if isfinite(simTime)
        prefix = message("simulinkcompiler:genapp:SimulationTimePrefix").getString;
        simTimeStr = sprintf('%s %10.4e', prefix, simTime);
    else
        simTimeStr = '';
    end
    app.SimTimeLabel.Text = simTimeStr;

    drawnow limitrate;
end
