function HMEC_RAO_demo_launcher
% Prof. Deniz Gedikli | ORE | University of Hawaii at Manoa
% All rights reserved
% Please email me at egedikli@hawaii.edu if you find any mistakes.
%
% HMEC_RAO_DEMO_LAUNCHER Open either classroom RAO demonstration.
%
% Put this file and the two demo files in the same MATLAB folder, then run:
%   HMEC_RAO_demo_launcher
%
% Direct commands:
%   HMEC_heave_RAO_demo
%   HMEC_heave_pitch_RAO_demo
%   HMEC_heave_RAO_demo('selftest')
%   HMEC_heave_pitch_RAO_demo('selftest')

    fig = uifigure( ...
        'Name', 'HMEC Marine Hydrodynamics | MATLAB RAO Demos', ...
        'Position', [280 170 850 560], ...
        'Color', [0.97 0.97 0.97]);

    layout = uigridlayout(fig, [6 2]);
    layout.RowHeight = {72, 88, 88, 55, 120, '1x'};
    layout.ColumnWidth = {'1x', '1x'};
    layout.Padding = [18 18 18 18];
    layout.RowSpacing = 12;
    layout.ColumnSpacing = 12;

    titleLabel = uilabel(layout, ...
        'Text', 'Marine Hydrodynamics & Floating-Body Dynamics', ...
        'HorizontalAlignment', 'center', ...
        'FontSize', 24, ...
        'FontWeight', 'bold');
    titleLabel.Layout.Row = 1;
    titleLabel.Layout.Column = [1 2];

    heaveDescription = uilabel(layout, ...
        'Text', ['1-DOF HEAVE: vary body mass, waterplane area, added mass, ' ...
                 'radiation/PTO damping, stiffness, excitation, and wave amplitude.'], ...
        'WordWrap', 'on', ...
        'FontSize', 14, ...
        'HorizontalAlignment', 'center');
    heaveDescription.Layout.Row = 2;
    heaveDescription.Layout.Column = 1;

    coupledDescription = uilabel(layout, ...
        'Text', ['2-DOF HEAVE + PITCH: explore coupled matrices, direct excitation, ' ...
                 'mode interaction, pitch units, phase, power, and animated motion.'], ...
        'WordWrap', 'on', ...
        'FontSize', 14, ...
        'HorizontalAlignment', 'center');
    coupledDescription.Layout.Row = 2;
    coupledDescription.Layout.Column = 2;

    heaveButton = uibutton(layout, 'push', ...
        'Text', 'Open 1-DOF Heave Demo', ...
        'FontSize', 16, ...
        'FontWeight', 'bold', ...
        'ButtonPushedFcn', @(~,~) HMEC_heave_RAO_demo());
    heaveButton.Layout.Row = 3;
    heaveButton.Layout.Column = 1;

    coupledButton = uibutton(layout, 'push', ...
        'Text', 'Open 2-DOF Heave + Pitch Demo', ...
        'FontSize', 16, ...
        'FontWeight', 'bold', ...
        'ButtonPushedFcn', @(~,~) HMEC_heave_pitch_RAO_demo());
    coupledButton.Layout.Row = 3;
    coupledButton.Layout.Column = 2;

    testButton = uibutton(layout, 'push', ...
        'Text', 'Run both numerical self-tests', ...
        'FontSize', 14, ...
        'ButtonPushedFcn', @runTests);
    testButton.Layout.Row = 4;
    testButton.Layout.Column = [1 2];

    statusArea = uitextarea(layout, ...
        'Editable', 'off', ...
        'WordWrap', 'on', ...
        'Value', { ...
            'Recommended classroom sequence:', ...
            '1. Ask for a prediction.', ...
            '2. Set the current case as the dashed baseline.', ...
            '3. Change one control.', ...
            '4. Compare RAO magnitude, phase, coefficient curves, motion, and PTO power.'});
    statusArea.Layout.Row = 5;
    statusArea.Layout.Column = [1 2];

    footer = uilabel(layout, ...
        'Text', ['All internal calculations use SI units and radians. Hydrodynamic curves ' ...
                 'are physically constrained teaching data, not validated device predictions. Prof. Deniz Gedikli | All Rights Reserved'], ...
        'WordWrap', 'on', ...
        'HorizontalAlignment', 'center', ...
        'FontAngle', 'italic');
    footer.Layout.Row = 6;
    footer.Layout.Column = [1 2];

    function runTests(varargin)
        testButton.Enable = 'off';
        cleanup = onCleanup(@() enableTestButton());
        try
            report1 = HMEC_heave_RAO_demo('selftest');
            report2 = HMEC_heave_pitch_RAO_demo('selftest');
            statusArea.Value = { ...
                'SELF-TESTS PASSED', ...
                sprintf('1-DOF peak: T = %.2f s, |H3| = %.3f.', ...
                    report1.defaultPeakPeriod_s, report1.defaultPeakRAO), ...
                sprintf('2-DOF heave peak: T = %.2f s, |H3| = %.3f m/m.', ...
                    report2.defaultHeavePeakPeriod_s, report2.defaultHeavePeakRAO_mPerM), ...
                sprintf('2-DOF pitch peak: T = %.2f s, |H5| = %.2f deg/m.', ...
                    report2.defaultPitchPeakPeriod_s, report2.defaultPitchPeakRAO_degPerM), ...
                'Matrix symmetry/definiteness and expected parameter trends were also checked.'};
        catch errorInfo
            statusArea.Value = { ...
                'SELF-TEST FAILED', ...
                errorInfo.message, ...
                'Open the MATLAB Command Window for the full diagnostic.'};
            rethrow(errorInfo)
        end
        clear cleanup
        enableTestButton();
    end

    function enableTestButton()
        if isvalid(testButton)
            testButton.Enable = 'on';
        end
    end
end
