// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.vcs.log.ui.actions;

import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.actionSystem.impl.AutoPopupSupportingListener;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.ui.popup.ListPopup;
import com.intellij.util.Function;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.vcs.log.VcsLogBundle;
import com.intellij.vcs.log.VcsLogDataKeys;
import com.intellij.vcs.log.VcsLogUi;
import com.intellij.vcs.log.graph.PermanentGraph;
import com.intellij.vcs.log.impl.MainVcsLogUiProperties;
import com.intellij.vcs.log.impl.VcsLogUiProperties;
import com.intellij.vcs.log.ui.VcsLogInternalDataKeys;
import com.intellij.vcs.log.util.GraphSortPresentationUtil;
import icons.VcsLogIcons;
import org.jetbrains.annotations.NotNull;

import java.awt.*;

public class IntelliSortChooserPopupAction extends DumbAwareAction {
  public IntelliSortChooserPopupAction() {
    super(VcsLogBundle.messagePointer("action.IntelliSortChooserPopupAction.text"),
          VcsLogBundle.messagePointer("action.IntelliSortChooserPopupAction.description"), VcsLogIcons.IntelliSort);
  }

  @Override
  public void actionPerformed(@NotNull AnActionEvent e) {
    VcsLogUi logUI = e.getRequiredData(VcsLogDataKeys.VCS_LOG_UI);
    VcsLogUiProperties properties = e.getRequiredData(VcsLogInternalDataKeys.LOG_UI_PROPERTIES);

    ActionGroup settingsGroup = new DefaultActionGroup(ContainerUtil.map(PermanentGraph.SortType.values(),
                                                                         (Function<PermanentGraph.SortType, AnAction>)sortType ->
                                                                           new SelectIntelliSortTypeAction(logUI, properties, sortType)));


    ListPopup popup = JBPopupFactory.getInstance()
      .createActionGroupPopup(null, settingsGroup, e.getDataContext(), JBPopupFactory.ActionSelectionAid.MNEMONICS, true,
                              ActionPlaces.TOOLWINDOW_POPUP);
    AutoPopupSupportingListener.installOn(popup);
    Component component = e.getInputEvent().getComponent();
    if (component instanceof ActionButtonComponent) {
      popup.showUnderneathOf(component);
    }
    else {
      popup.showInCenterOf(component);
    }
  }

  @Override
  public void update(@NotNull AnActionEvent e) {
    super.update(e);
    VcsLogUiProperties properties = e.getData(VcsLogInternalDataKeys.LOG_UI_PROPERTIES);
    e.getPresentation().setEnabled(properties != null);
    if (properties != null && properties.exists(MainVcsLogUiProperties.BEK_SORT_TYPE)) {
      String sortName = GraphSortPresentationUtil.getLocalizedName(properties.get(MainVcsLogUiProperties.BEK_SORT_TYPE));
      String description = VcsLogBundle.message("vcs.log.action.intellisort.title", sortName);
      e.getPresentation().setDescription(description);
      e.getPresentation().setText(description);
    }
  }

  private static class SelectIntelliSortTypeAction extends ToggleAction implements DumbAware {
    private final PermanentGraph.SortType mySortType;
    private final VcsLogUi myUI;
    private final VcsLogUiProperties myProperties;

    SelectIntelliSortTypeAction(@NotNull VcsLogUi ui,
                                @NotNull VcsLogUiProperties properties,
                                @NotNull PermanentGraph.SortType sortType) {
      super(() -> GraphSortPresentationUtil.getLocalizedName(sortType),
            () -> GraphSortPresentationUtil.getLocalizedDescription(sortType) + ".",
            null);
      myUI = ui;
      myProperties = properties;
      mySortType = sortType;
    }

    @Override
    public void update(@NotNull AnActionEvent e) {
      super.update(e);
      e.getPresentation().setEnabled(!myUI.getDataPack().isEmpty() && myProperties.exists(MainVcsLogUiProperties.BEK_SORT_TYPE));
    }

    @Override
    public boolean isSelected(@NotNull AnActionEvent e) {
      return myProperties.exists(MainVcsLogUiProperties.BEK_SORT_TYPE) &&
             myProperties.get(MainVcsLogUiProperties.BEK_SORT_TYPE).equals(mySortType);
    }

    @Override
    public void setSelected(@NotNull AnActionEvent e, boolean state) {
      if (state && myProperties.exists(MainVcsLogUiProperties.BEK_SORT_TYPE)) {
        myProperties.set(MainVcsLogUiProperties.BEK_SORT_TYPE, mySortType);
      }
    }
  }
}

@@ -1068,16 +1068,50 @@
 a.awt.*;
+%0Aimport java.awt.event.InputEvent;
 %0A%0Apublic
@@ -2320,16 +2320,96 @@
 popup);%0A
+%0A    InputEvent inputEvent = e.getInputEvent();%0A    if (inputEvent != null) %7B%0A  
     Comp
@@ -2422,30 +2422,25 @@
 component = 
-e.getI
+i
 nputEvent().
@@ -2436,18 +2436,16 @@
 putEvent
-()
 .getComp
@@ -2449,24 +2449,26 @@
 omponent();%0A
+  
     if (comp
@@ -2507,32 +2507,34 @@
 ponent) %7B%0A      
+ 
+ 
 popup.showUndern
@@ -2560,21 +2560,27 @@
     
+  
 %7D%0A    
+ 
+ 
 else %7B%0A
+  
     
@@ -2610,24 +2610,78 @@
 component);%0A
+      %7D%0A    %7D else %7B%0A      popup.showInFocusCenter();%0A
     %7D%0A  %7D%0A%0A 

fix NPE in IntelliSort chooser when it is called from quick actions popup