/*
 * 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.project.DumbAwareAction;
import com.intellij.openapi.wm.impl.content.ToolWindowContentUi;
import com.intellij.vcs.log.VcsLogDataKeys;
import com.intellij.vcs.log.VcsLogUi;
import com.intellij.vcs.log.graph.PermanentGraph;
import com.intellij.vcs.log.ui.VcsLogUiImpl;
import icons.VcsLogIcons;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;

abstract class CollapseOrExpandGraphAction extends DumbAwareAction {
  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";

  private final boolean myExpand;

  public CollapseOrExpandGraphAction(boolean isExpand) {
    super(getPrefix(isExpand) + LINEAR_BRANCHES, getPrefix(isExpand) + LINEAR_BRANCHES_DESCRIPTION, getBranchesIcon(isExpand));
    myExpand = isExpand;
  }

  @Override
  public void actionPerformed(@NotNull AnActionEvent e) {
    VcsLogUi ui = e.getRequiredData(VcsLogDataKeys.VCS_LOG_UI);
    VcsLogUiImpl vcsLogUi = (VcsLogUiImpl)ui;
    if (myExpand) {
      vcsLogUi.expandAll();
    }
    else {
      vcsLogUi.collapseAll();
    }
  }

  @Override
  public void update(@NotNull AnActionEvent e) {
    VcsLogUi ui = e.getData(VcsLogDataKeys.VCS_LOG_UI);
    if (ui != null && ui.areGraphActionsEnabled()) {
      e.getPresentation().setEnabled(true);
      update(e.getRequiredData(VcsLogDataKeys.VCS_LOG_UI), e);
    }
    else {
      e.getPresentation().setEnabled(false);
    }
  }

  protected void update(@NotNull VcsLogUi ui, @NotNull AnActionEvent e) {
    if (!ui.getFilterUi().getFilters().getDetailsFilters().isEmpty()) {
      e.getPresentation().setEnabled(false);
    }

    if (ui.getBekType() == PermanentGraph.SortType.LinearBek) {
      e.getPresentation().setIcon(getMergesIcon(myExpand));
      e.getPresentation().setText(getPrefix(myExpand) + MERGES);
      e.getPresentation().setDescription(getPrefix(myExpand) + MERGES_DESCRIPTION);
    }
    else {
      e.getPresentation().setIcon(getBranchesIcon(myExpand));
      e.getPresentation().setText(getPrefix(myExpand) + LINEAR_BRANCHES);
      e.getPresentation().setDescription(getPrefix(myExpand) + LINEAR_BRANCHES_DESCRIPTION);
    }
  }

  private static Icon getMergesIcon(boolean expand) {
    return expand ? VcsLogIcons.ExpandMerges : VcsLogIcons.CollapseMerges;
  }

  private static Icon getBranchesIcon(boolean expand) {
    return expand ? VcsLogIcons.ExpandBranches : VcsLogIcons.CollapseBranches;
  }

  @NotNull
  private static String getPrefix(boolean expand) {
    return expand ? "Expand " : "Collapse ";
  }

  public static class CollapseGraphAction extends CollapseOrExpandGraphAction {
    public CollapseGraphAction() {
      super(false);
    }
  }

  public static class ExpandGraphAction extends CollapseOrExpandGraphAction {
    public ExpandGraphAction() {
      super(true);
    }
  }
}

@@ -2054,24 +2054,25 @@
 CS_LOG_UI);%0A
+%0A
     if (ui !
@@ -2162,216 +2162,8 @@
 ;%0A  
-    update(e.getRequiredData(VcsLogDataKeys.VCS_LOG_UI), e);%0A    %7D%0A    else %7B%0A      e.getPresentation().setEnabled(false);%0A    %7D%0A  %7D%0A%0A  protected void update(@NotNull VcsLogUi ui, @NotNull AnActionEvent e) %7B%0A
     
@@ -2226,24 +2226,26 @@
 sEmpty()) %7B%0A
+  
       e.getP
@@ -2281,19 +2281,23 @@
 e);%0A    
+  
 %7D%0A%0A
+  
     if (
@@ -2348,24 +2348,26 @@
 inearBek) %7B%0A
+  
       e.getP
@@ -2387,39 +2387,153 @@
 .set
-Icon(getMergesIcon(myExpand));%0A
+Text(getPrefix(myExpand) + MERGES);%0A        e.getPresentation().setDescription(getPrefix(myExpand) + MERGES_DESCRIPTION);%0A      %7D%0A      else %7B%0A  
     
@@ -2580,33 +2580,44 @@
 myExpand) + 
-MERG
+LINEAR_BRANCH
 ES);%0A
+  
       e.getP
@@ -2659,36 +2659,45 @@
 fix(myExpand) + 
-MERG
+LINEAR_BRANCH
 ES_DESCRIPTION);
@@ -2689,32 +2689,40 @@
 S_DESCRIPTION);%0A
+      %7D%0A
     %7D%0A    else %7B
@@ -2755,213 +2755,416 @@
 .set
-Icon(getBranchesIcon(myExpand));%0A      e.getPresentation().setText(getPrefix(myExpand) + LINEAR_BRANCHES);%0A      e.getPresentation().setDescription(getPrefix(myExpand) + LINEAR_BRANCHES_DESCRIPTION);%0A    %7D
+Enabled(false);%0A    %7D%0A%0A    if (isIconHidden(e)) %7B%0A      e.getPresentation().setIcon(null);%0A    %7D%0A    else %7B%0A      e.getPresentation()%0A        .setIcon(ui != null && ui.getBekType() == PermanentGraph.SortType.LinearBek ? getMergesIcon(myExpand) : getBranchesIcon(myExpand));%0A    %7D%0A  %7D%0A%0A  private boolean isIconHidden(@NotNull AnActionEvent e) %7B%0A    return e.getPlace().equals(ToolWindowContentUi.POPUP_PLACE);
 %0A  %7D

refactor code and turn icons off when actions are in popup