/*
 * Copyright 2016 Crown Copyright
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package stroom.node.client;

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.web.bindery.event.shared.EventBus;
import stroom.alert.client.event.AlertEvent;
import stroom.core.client.MenuKeys;
import stroom.hyperlink.client.Hyperlink;
import stroom.hyperlink.client.HyperlinkEvent;
import stroom.hyperlink.client.HyperlinkType;
import stroom.menubar.client.event.BeforeRevealMenubarEvent;
import stroom.config.global.client.presenter.ManageGlobalPropertyPresenter;
import stroom.security.client.api.ClientSecurityContext;
import stroom.security.shared.PermissionNames;
import stroom.svg.client.SvgPreset;
import stroom.svg.client.SvgPresets;
import stroom.ui.config.client.UiConfigCache;
import stroom.ui.config.shared.UiConfig;
import stroom.widget.menu.client.presenter.IconMenuItem;
import stroom.widget.popup.client.event.ShowPopupEvent;
import stroom.widget.popup.client.presenter.PopupSize;
import stroom.widget.popup.client.presenter.PopupView.PopupType;

public class ManageNodeToolsPlugin extends NodeToolsPlugin {
    private final Provider<ManageGlobalPropertyPresenter> manageGlobalPropertyPresenter;

    private final UiConfigCache clientPropertyCache;

    @Inject
    public ManageNodeToolsPlugin(final EventBus eventBus,
                                 final ClientSecurityContext securityContext,
                                 final UiConfigCache clientPropertyCache,
                                 final Provider<ManageGlobalPropertyPresenter> manageGlobalPropertyPresenter) {
        super(eventBus, securityContext);
        this.manageGlobalPropertyPresenter = manageGlobalPropertyPresenter;
        this.clientPropertyCache = clientPropertyCache;
    }

    @Override
    protected void addChildItems(final BeforeRevealMenubarEvent event) {
        if (getSecurityContext().hasAppPermission(PermissionNames.MANAGE_VOLUMES_PERMISSION)) {
            clientPropertyCache.get()
                .onSuccess(uiConfig -> {
                    addIndexVolumesMenuItem(event, uiConfig);
                    addIndexVolumeGroupsMenuItem(event, uiConfig);
                })
                .onFailure(caught -> AlertEvent.fireError(ManageNodeToolsPlugin.this, caught.getMessage(), null));
        }
        if (getSecurityContext().hasAppPermission(PermissionNames.MANAGE_PROPERTIES_PERMISSION)) {
            event.getMenuItems().addMenuItem(MenuKeys.TOOLS_MENU,
                    new IconMenuItem(90, SvgPresets.PROPERTIES, SvgPresets.PROPERTIES, "Properties", null, true, () -> {
                        final PopupSize popupSize = new PopupSize(1000, 600, true);
                        ShowPopupEvent.fire(ManageNodeToolsPlugin.this, manageGlobalPropertyPresenter.get(),
                                PopupType.CLOSE_DIALOG, null, popupSize, "System Properties", null, null);
                    }));
        }
    }

    private void addIndexVolumesMenuItem(final BeforeRevealMenubarEvent event,
                                         final UiConfig uiConfig) {
        final IconMenuItem volumeMenuItem;
        final SvgPreset icon = SvgPresets.VOLUMES;
        final String indexVolumesUiUrl = uiConfig.getUrlConfig().getIndexVolumes();
        if (indexVolumesUiUrl != null && indexVolumesUiUrl.trim().length() > 0) {
            volumeMenuItem = new IconMenuItem(4, icon, null, "Index Volumes", null, true, () -> {
                final Hyperlink hyperlink = new Hyperlink.Builder()
                        .text("Index Volumes")
                        .href(indexVolumesUiUrl)
                        .type(HyperlinkType.TAB + "|Index Volumes")
                        .icon(icon)
                        .build();
                HyperlinkEvent.fire(this, hyperlink);
            });

        } else {
            volumeMenuItem = new IconMenuItem(5, icon, icon, "Index Volumes UI is not configured!", null, false, null);
        }
        event.getMenuItems().addMenuItem(MenuKeys.TOOLS_MENU, volumeMenuItem);
    }

    private void addIndexVolumeGroupsMenuItem(final BeforeRevealMenubarEvent event,
                                              final UiConfig uiConfig) {
        final SvgPreset icon = SvgPresets.VOLUMES;
        final IconMenuItem volumeGroupMenuItem;
        final String indexVolumeGroupsUiUrl = uiConfig.getUrlConfig().getIndexVolumeGroups();
        if (indexVolumeGroupsUiUrl != null && indexVolumeGroupsUiUrl.trim().length() > 0) {
            volumeGroupMenuItem = new IconMenuItem(4, icon, null, "Index Volume Groups", null, true, () -> {
                final Hyperlink hyperlink = new Hyperlink.Builder()
                        .text("Index Volume Groups")
                        .href(indexVolumeGroupsUiUrl)
                        .type(HyperlinkType.TAB + "|Index Volume Groups")
                        .icon(icon)
                        .build();
                HyperlinkEvent.fire(this, hyperlink);
            });
        } else {
            volumeGroupMenuItem = new IconMenuItem(5, icon, icon, "Index Volume Groups UI is not configured!", null, false, null);
        }
        event.getMenuItems().addMenuItem(MenuKeys.TOOLS_MENU, volumeGroupMenuItem);
    }
}

@@ -2642,75 +2642,8 @@
 g);%0A
-                    addIndexVolumeGroupsMenuItem(event, uiConfig);%0A
     
@@ -4523,1196 +4523,7 @@
    %7D
-%0A%0A    private void addIndexVolumeGroupsMenuItem(final BeforeRevealMenubarEvent event,%0A                                              final UiConfig uiConfig) %7B%0A        final SvgPreset icon = SvgPresets.VOLUMES;%0A        final IconMenuItem volumeGroupMenuItem;%0A        final String indexVolumeGroupsUiUrl = uiConfig.getUrlConfig().getIndexVolumeGroups();%0A        if (indexVolumeGroupsUiUrl != null && indexVolumeGroupsUiUrl.trim().length() %3E 0) %7B%0A            volumeGroupMenuItem = new IconMenuItem(4, icon, null, %22Index Volume Groups%22, null, true, () -%3E %7B%0A                final Hyperlink hyperlink = new Hyperlink.Builder()%0A                        .text(%22Index Volume Groups%22)%0A                        .href(indexVolumeGroupsUiUrl)%0A                        .type(HyperlinkType.TAB + %22%7CIndex Volume Groups%22)%0A                        .icon(icon)%0A                        .build();%0A                HyperlinkEvent.fire(this, hyperlink);%0A            %7D);%0A        %7D else %7B%0A            volumeGroupMenuItem = new IconMenuItem(5, icon, icon, %22Index Volume Groups UI is not configured!%22, null, false, null);%0A        %7D%0A        event.getMenuItems().addMenuItem(MenuKeys.TOOLS_MENU, volumeGroupMenuItem);%0A    %7D
 %0A%7D%0A

Remove index volume groups menu item