// Copyright (c) 2009-2026 SAP SE, All Rights Reserved

/**
 * @fileOverview Main Controller for the Navigation Sample Application.
 *
 * Handles the main view with IconTabBar navigation between Overview and Navigation tabs.
 *
 * @since 1.147
 */
sap.ui.define([
    "sap/m/Button",
    "sap/m/Dialog",
    "sap/m/Text",
    "sap/ui/core/mvc/Controller"
], (
    Button,
    Dialog,
    Text,
    Controller
) => {
    "use strict";

    /**
     * Main controller managing the IconTabBar and route synchronization.
     *
     * @class
     * @extends sap.ui.core.mvc.Controller
     * @alias sap.ushell.sandbox.NavigationSample.controller.Main
     * @since 1.147
     * @private
     */
    return Controller.extend("sap.ushell.sandbox.NavigationSample.controller.Main", {

        /**
         * Initializes route matching, syncs IconTabBar, and shows the standalone warning dialog.
         *
         * @private
         */
        onInit: function () {
            this.getOwnerComponent().getRouter().attachRouteMatched((oEvent) => {
                const sFilter = oEvent.getParameter("name");
                this.byId("mainTabs").setSelectedKey(sFilter);
            });

            if (this.getOwnerComponent().getModel("app").getProperty("/isStandalone")) {
                this._openStandaloneDialog();
            }
        },

        /**
         * Opens a modal dialog informing the user that the app is running in standalone mode.
         *
         * @private
         */
        _openStandaloneDialog: async function () {
            const oBundle = await this.getOwnerComponent().getModel("i18n").getResourceBundle();
            const oDialog = new Dialog({
                title: oBundle.getText("main.standaloneDialog.title"),
                type: "Message",
                state: "Warning",
                content: new Text({ text: oBundle.getText("main.standaloneDialog.text") }),
                beginButton: new Button({
                    text: oBundle.getText("main.standaloneDialog.openSandbox"),
                    type: "Emphasized",
                    press: () => {
                        oDialog.close();
                        globalThis.location.href = sap.ui.require.toUrl("sap/ushell/sandbox/NavigationSample/sandbox.html");
                    }
                }),
                endButton: new Button({
                    text: oBundle.getText("main.standaloneDialog.ok"),
                    press: () => oDialog.close()
                }),
                afterClose: () => oDialog.destroy()
            });
            this.getView().addDependent(oDialog);
            oDialog.open();
        },

        /**
         * Handles IconTabFilter selection and navigates to the corresponding route.
         *
         * @param {sap.ui.base.Event} oEvent - The select event from IconTabBar
         * @private
         */
        onIconTabFilterSelected: function (oEvent) {
            const sKey = oEvent.getParameter("key");
            this.getOwnerComponent().getRouter().navTo(sKey);
        }
    });
});
