{{!
    This file is part of Moodle - http://moodle.org/

    Moodle is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Moodle is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
}}
{{!
    @template theme_degrade/core/user_menu

    Action link template.

    Context variables required for this template:
    * unauthenticateduseruser - (Optional) Items to be displayed if an an unautheticated user is accessing the site
        * content - The content to be displayed in the header.
        * url - The login url
    * items - Array of user menu items used in user_action_menu_items. Required if the above not provided.
    * metadata - Array of additional metadata to be displayed in the dropdown button.
    * avatardata - Array of avatars to be displayed. Usually only the current user's avatar. If viewing as another user,
        includes that user's avatar.
    * userfullname - The name of the logged in user
    * submenus - Array of submenus within the user menu.
      * id - The id of the submenu.
      * title - The title of the submenu.
      * items - Array of the submenu items used in core/user_action_menu_submenu_items.

    Example context (json):
    {
        "unauthenticateduser": {
            "content": "You are not logged in",
            "url": "https://yourmoodlesite/login/index.php"
        },
        "items": [],
        "metadata": [],
        "avatardata": [],
        "userfullname": "Admin User",
        "submenus": [
            {
                "id": "86cebd87",
                "title": "Submenu title",
                "items": []
            }
        ]
    }
}}
<div class="usermenu">
    {{#unauthenticateduser}}
        {{#url}}
            <a class="login pl-2 btn btn-primary" href="{{{url}}}">{{#str}} login, core {{/str}}</a>
        {{/url}}
    {{/unauthenticateduser}}
    {{^unauthenticateduser}}
        <a href="#" id="user-menu-toggle">
            <span class="userbutton">
                {{> core/user_menu_metadata }}
            </span>
        </a>
    {{/unauthenticateduser}}
</div>
{{#js}}
    require(["jquery", "core/notification"], function ($, Notification) {

        $("#user-menu-toggle").click(function (event) {
            event.preventDefault();
            let body = `
                {{#avatardata}}
                    <span class="avatar d-none {{{classes}}}">
                        {{{content}}}
                    </span>
                {{/avatardata}}
                <div class="usermenu-modal-tabs">
                    <div class="tabs-menu-link">
                        {{> core/user_action_menu_items }}
                    </div>
                    <div class="usermenu-modal-tabs-content"></div>
                </div>`;
            require(['core/modal'], function (ModalModule) {
                const Modal = ModalModule && (ModalModule.default || ModalModule);
                if (Modal && typeof Modal.create === 'function') {
                    Modal.create({
                        title: "{{{userfullname}}}",
                        body: body,
                        large: true,
                        show: true,
                        removeOnClose: true,
                    }).then(function (modal) {
                        if (!modal.root) {
                            modal.root = modal._root;
                        }
                        usermodal_init(modal);
                    }).catch(Notification.exception);
                } else {
                    require(["core/modal_factory"], function (ModalFactory) {
                        ModalFactory.create({
                            type: ModalFactory.types.DEFAULT,
                            title: "{{{userfullname}}}",
                            body: body,
                            footer: "",
                            removeOnClose: true,
                        }).done(function (modal) {
                            if (!modal.root) {
                                modal.root = modal._root;
                            }
                            modal.show();
                            usermodal_init(modal);
                        });
                    });
                }
            });
        });

        function usermodal_init(modal) {
            modal.root.find(".modal-dialog").addClass("usermenu-modal-userlinks modal-xl");
            modal.root.find(".modal-header").append(`
                <div class="user-action d-flex">
                    <div class="languages dropdown">
                        <a href="#" class="dropdown-toggle nav-link">{{#str}}language{{/str}}</a>
                        <div class="submenus" style="z-index:2">
                            {{#submenus}}{{>core/user_action_menu_submenu_items}}{{/submenus}}
                        </div>
                    </div>
                </div>`);
            modal.root.find(".usermenu-modal-tabs .tabs-menu-link a:first-child").addClass("active");
            modal.root.find(".usermenu-modal-tabs .tabs-menu-link a").click(function () {
                event.preventDefault();
                setIframeUrl($(this), modal);
            });

            if ($(window).width() <= 575) {
                /* se menor não faz nada */
            } else {
                let $menu = modal.root.find(".usermenu-modal-tabs .tabs-menu-link a:first-child");
                setIframeUrl($menu, modal);
            }
        }

        function setIframeUrl($menu, modal) {
            modal.root.find(".usermenu-modal-tabs .tabs-menu-link a").removeClass("active");
            $menu.addClass("active");

            var href = $menu.attr("href");

            if ($(window).width() <= 575) {
                location.href = href;
            }

            if (href.includes("login/logout.php") ||
                    href.includes("switchrole.php") ||
                    href.includes("reportbuilder") ||
                    href.includes("user/files.php")) {
                location.href = href;
            } else if (href == "#") {
                alert("ops...");
            } else {
                if (href.includes("?")) {
                    href += "&embed-frame-top=1";
                } else {
                    href += "?embed-frame-top=1";
                }

                if (href.includes("user/profile.php")) {
                    modal.root.find(".modal-header").addClass("show-avatar");
                    modal.root.find(".usermenu-modal-tabs-content").html(`
                        <div class="user m-2">
                            {{#avatardata}}
                                <span class="avatar d-none {{{classes}}}">
                                    {{{content}}}
                                </span>
                            {{/avatardata}}
                            <h2>{{{userfullname}}}</h2>
                        </div>
                        <div class="usermenu-modal-tabs-content-internal"></div>`);
                    modal.root.find(".usermenu-modal-tabs-content-internal").load(href);
                } else {
                    modal.root.find(".modal-header").removeClass("show-avatar");
                    modal.root.find(".usermenu-modal-tabs-content").load(href);
                }
            }
        }
    });
{{/js}}
