/*
 * Copyright 2000-2015 JetBrains s.r.o.
 *
 * 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 com.intellij.vcs.log.ui.actions;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.vcs.log.graph.PermanentGraph;
import com.intellij.vcs.log.graph.actions.ActionController;
import com.intellij.vcs.log.graph.actions.GraphAction;
import com.intellij.vcs.log.graph.actions.GraphAnswer;
import com.intellij.vcs.log.impl.MainVcsLogUiProperties;
import com.intellij.vcs.log.impl.VcsLogUiProperties;
import com.intellij.vcs.log.statistics.VcsLogUsageTriggerCollector;
import com.intellij.vcs.log.ui.MainVcsLogUi;
import com.intellij.vcs.log.ui.VcsLogInternalDataKeys;
import com.intellij.vcs.log.visible.VisiblePack;
import org.jetbrains.annotations.NotNull;

import java.util.function.Supplier;

abstract class CollapseOrExpandGraphAction extends DumbAwareAction {
  private final Supplier<String> myLinearBranchesAction;
  private final Supplier<String> myLinearBranchesDescription;
  private final Supplier<String> myMergesAction;
  private final Supplier<String> myMergesDescription;
  //private static final String LINEAR_BRANCHES = "Linear Branches";
  //private static final String LINEAR_BRANCHES_DESCRIPTION = "linear branches";
  //private static final String MERGES = "Merges";
  //private static final String MERGES_DESCRIPTION = "merges";

  protected CollapseOrExpandGraphAction(@NotNull Supplier<String> linearBranchesAction,
                                        @NotNull Supplier<String> linearBranchesDescription,
                                        @NotNull Supplier<String> mergesAction,
                                        @NotNull Supplier<String> mergesDescription) {
    super(linearBranchesAction, linearBranchesDescription, null);
    myLinearBranchesAction = linearBranchesAction;
    myLinearBranchesDescription = linearBranchesDescription;
    myMergesAction = mergesAction;
    myMergesDescription = mergesDescription;
  }

  //CollapseOrExpandGraphAction(@NotNull String mergesAction) {
  //  super(mergesAction + " " + LINEAR_BRANCHES, mergesAction + " " + LINEAR_BRANCHES_DESCRIPTION, null);
  //}

  @Override
  public void actionPerformed(@NotNull AnActionEvent e) {
    VcsLogUsageTriggerCollector.triggerUsage(e, this);

    executeAction(e.getRequiredData(VcsLogInternalDataKeys.MAIN_UI));
  }

  @Override
  public void update(@NotNull AnActionEvent e) {
    MainVcsLogUi ui = e.getData(VcsLogInternalDataKeys.MAIN_UI);
    VcsLogUiProperties properties = e.getData(VcsLogInternalDataKeys.LOG_UI_PROPERTIES);

    if (ui != null && !ui.getDataPack().isEmpty() && properties != null && properties.exists(MainVcsLogUiProperties.BEK_SORT_TYPE)) {
      e.getPresentation().setEnabled(true);
      if (!ui.getFilterUi().getFilters().getDetailsFilters().isEmpty()) {
        e.getPresentation().setEnabled(false);
      }

      if (properties.get(MainVcsLogUiProperties.BEK_SORT_TYPE) == PermanentGraph.SortType.LinearBek) {
        e.getPresentation().setText(myMergesAction.get());
        e.getPresentation().setDescription(myMergesDescription.get());
      }
      else {
        e.getPresentation().setText(myLinearBranchesAction.get());
        e.getPresentation().setDescription(myLinearBranchesDescription.get());
      }
    }
    else {
      e.getPresentation().setEnabled(false);
    }

    e.getPresentation().setText(myLinearBranchesAction.get());
    e.getPresentation().setDescription(myLinearBranchesDescription.get());
  }

  protected abstract void executeAction(@NotNull MainVcsLogUi vcsLogUi);

  protected void performLongAction(@NotNull MainVcsLogUi logUi, @NotNull GraphAction graphAction, @NotNull String title) {
    VisiblePack dataPack = logUi.getDataPack();
    ProgressManager.getInstance().runProcessWithProgressSynchronously(() -> {
      ActionController<Integer> actionController = dataPack.getVisibleGraph().getActionController();
      GraphAnswer<Integer> answer = actionController.performAction(graphAction);
      Runnable updater = answer.getGraphUpdater();
      ApplicationManager.getApplication().invokeLater(() -> {
        assert updater != null : "Action:" + title +
                                 "\nController: " + actionController +
                                 "\nAnswer:" + answer;
        updater.run();
        logUi.getTable().handleAnswer(answer);
      });
    }, title, false, null, logUi.getMainComponent());
  }
}

@@ -1780,272 +1780,8 @@
 ion;
-%0A  //private static final String LINEAR_BRANCHES = %22Linear Branches%22;%0A  //private static final String LINEAR_BRANCHES_DESCRIPTION = %22linear branches%22;%0A  //private static final String MERGES = %22Merges%22;%0A  //private static final String MERGES_DESCRIPTION = %22merges%22;
 %0A%0A  
@@ -2393,186 +2393,8 @@
  %7D%0A%0A
-  //CollapseOrExpandGraphAction(@NotNull String mergesAction) %7B%0A  //  super(mergesAction + %22 %22 + LINEAR_BRANCHES, mergesAction + %22 %22 + LINEAR_BRANCHES_DESCRIPTION, null);%0A  //%7D%0A%0A
   @O

remove commented code