package jetbrains.mps.vcs.diff.ui.merge;

/*Generated by MPS */

import jetbrains.mps.annotations.GeneratedClass;
import jetbrains.mps.vcs.diff.ui.common.ButtonsPainter;
import jetbrains.mps.nodeEditor.EditorComponent;
import jetbrains.mps.vcs.diff.ui.common.ChangeGroupLayout;
import jetbrains.mps.vcs.diff.ui.common.FoldingAreaButton;
import jetbrains.mps.vcs.diff.ui.common.ChangeGroup;
import jetbrains.mps.ide.icons.IdeIcons;
import jetbrains.mps.vcs.diff.merge.MergeSession;
import jetbrains.mps.vcs.diff.changes.ModelChange;
import java.util.Arrays;
import jetbrains.mps.vcs.diff.ui.common.DiffEditor;
import jetbrains.mps.baseLanguage.closures.runtime._FunctionTypes;
import javax.swing.Icon;

@GeneratedClass(node = "r:351fe3d9-2ce5-4ea0-8afc-9b076259a949(jetbrains.mps.vcs.diff.ui.merge)/708166622413811400", model = "r:351fe3d9-2ce5-4ea0-8afc-9b076259a949(jetbrains.mps.vcs.diff.ui.merge)")
public class MergeButtonsPainter extends ButtonsPainter {
  private MergeRootsPane myPane;
  private MergeButtonsPainter(MergeRootsPane pane, EditorComponent editorComponent, ChangeGroupLayout changeGroupLayout) {
    super(2, editorComponent, changeGroupLayout);
    myPane = pane;
  }
  @Override
  protected Iterable<FoldingAreaButton> createButtonsForChangeGroup(ChangeGroup changeGroup, int y) {
    int applyX = (isHighlightLeft() ? getX(0) : getX(1));
    int excludeX = (isHighlightLeft() ? getX(1) : getX(0));
    FoldingAreaButton apply = new MyButton(changeGroup, applyX, y, "Apply", (isHighlightLeft() ? IdeIcons.APPLY_RIGHT : IdeIcons.APPLY), (MergeSession session, Iterable<ModelChange> changes) -> session.applyChanges(changes));
    FoldingAreaButton exclude = new MyButton(changeGroup, excludeX, y, "Exclude", IdeIcons.EXCLUDE, (MergeSession session, Iterable<ModelChange> changes) -> session.excludeChanges(changes));
    return Arrays.asList(apply, exclude);
  }
  public static MergeButtonsPainter addTo(MergeRootsPane pane, DiffEditor diffEditor, ChangeGroupLayout changeGroupLayout, boolean inspector) {
    EditorComponent editorComponent = diffEditor.getEditorComponent(inspector);
    MergeButtonsPainter painter = new MergeButtonsPainter(pane, editorComponent, changeGroupLayout);
    editorComponent.getLeftEditorHighlighter().addFoldingAreaPainter(painter);
    return painter;
  }
  private class MyButton extends FoldingAreaButton {
    private _FunctionTypes._void_P2_E0<? super MergeSession, ? super Iterable<ModelChange>> myAction;
    private MyButton(ChangeGroup changeGroup, int x, int y, String name, Icon icon, _FunctionTypes._void_P2_E0<? super MergeSession, ? super Iterable<ModelChange>> action) {
      super(changeGroup, x, y, name, icon);
      myAction = action;
    }
    @Override
    public void performAction() {
      getEditorComponent().getEditorContext().getRepository().getModelAccess().executeCommand(() -> {
        myAction.invoke(myPane.getMergeSession(), getChangeGroup().getChanges());
        myPane.rehighlight();
      });
    }
  }
}
