/*
 * 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.execution.runners;

import com.intellij.execution.impl.ConsoleViewImpl;
import com.intellij.execution.ui.ConsoleView;
import com.intellij.execution.ui.ExecutionConsole;
import com.intellij.execution.ui.RunContentDescriptor;
import com.intellij.ide.util.PropertiesComponent;
import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.actionSystem.ex.ActionManagerEx;
import com.intellij.openapi.keymap.KeymapUtil;
import com.intellij.openapi.ui.popup.Balloon;
import com.intellij.openapi.util.Disposer;
import com.intellij.ui.GotItMessage;
import com.intellij.ui.awt.RelativePoint;
import com.intellij.util.Alarm;
import com.intellij.util.ui.JBUI;
import com.intellij.util.ui.PositionTracker;
import com.intellij.util.ui.UIUtil;
import com.intellij.util.ui.update.UiNotifyConnector;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;

public class RerunTestsNotification {

  private static final String KEY = "rerun.tests.notification.shown";

  public static void showRerunNotification(@Nullable RunContentDescriptor contentToReuse,
                                           @NotNull ExecutionConsole executionConsole) {
    if (contentToReuse == null) {
      return;
    }
    String lastActionId = ActionManagerEx.getInstanceEx().getPrevPreformedActionId();
    boolean showNotification = !RerunTestsAction.ID.equals(lastActionId);
    if (showNotification && !PropertiesComponent.getInstance().isTrueValue(KEY)) {
      UiNotifyConnector.doWhenFirstShown(executionConsole.getComponent(), () -> doShow(executionConsole));
    }
  }

  private static void doShow(@NotNull ExecutionConsole executionConsole) {
    Alarm alarm = new Alarm();
    alarm.addRequest(() -> {
      String shortcutText = KeymapUtil.getFirstKeyboardShortcutText(
        ActionManager.getInstance().getAction(RerunTestsAction.ID)
      );
      if (shortcutText.isEmpty()) {
        return;
      }

      ConsoleView consoleView = UIUtil.findComponentOfType(executionConsole.getComponent(), ConsoleViewImpl.class);
      if (consoleView != null) {
        GotItMessage message = GotItMessage.createMessage("Rerun tests with " + shortcutText, "");
        message.setDisposable(executionConsole);
        message.setCallback(() -> PropertiesComponent.getInstance().setValue(KEY, true));
        message.setShowCallout(false);
        JComponent consoleComponent = consoleView.getComponent();
        message.show(
          new PositionTracker<Balloon>(consoleComponent) {
            @Override
            public RelativePoint recalculateLocation(@NotNull Balloon balloon) {
              RelativePoint point = RelativePoint.getSouthEastOf(consoleComponent);
              Dimension balloonSize = balloon.getPreferredSize();
              int scrollBarSize = JBUI.scale(10);
              point.getPoint().translate(-balloonSize.width / 2 - scrollBarSize, -balloonSize.height / 2 - scrollBarSize);
              return point;
            }
          },
          Balloon.Position.below
        );
      }

      Disposer.dispose(alarm);
    }, 1000);
  }

}

@@ -1138,24 +1138,60 @@
 l.Disposer;%0A
+import com.intellij.ui.BalloonImpl;%0A
 import com.i
@@ -1321,93 +1321,9 @@
 .ui.
-JBUI;%0Aimport com.intellij.util.ui.PositionTracker;%0Aimport com.intellij.util.ui.UIUtil
+*
 ;%0Aim
@@ -3325,32 +3325,263 @@
     
-Dimension balloonSize = 
+Insets shadowInsets = balloon instanceof BalloonImpl ? ((BalloonImpl)balloon).getShadowBorderInsets()%0A                                                                   : JBUI.emptyInsets();%0A              Dimension balloonContentSize = JBDimension.create(
 ball
@@ -3602,16 +3602,23 @@
 edSize()
+, true)
 ;%0A      
@@ -3629,25 +3629,266 @@
     
-int scrollBarSize
+JBInsets.removeFrom(balloonContentSize, shadowInsets);%0A%0A              // compensate %22-shift.top%22 from BalloonImpl.Below.getShiftedPoint(java.awt.Point, java.awt.Insets)%0A              point.getPoint().y += shadowInsets.top;%0A%0A              int spacingFromEdges
  = J
@@ -3898,17 +3898,17 @@
 .scale(1
-0
+2
 );%0A     
@@ -3947,24 +3947,31 @@
 ate(-balloon
+Content
 Size.width /
@@ -3976,28 +3976,31 @@
  / 2 - s
-crollBarSize
+pacingFromEdges
 , -ballo
@@ -4001,16 +4001,23 @@
 -balloon
+Content
 Size.hei
@@ -4031,20 +4031,23 @@
  - s
-crollBarSize
+pacingFromEdges
 );%0A 

fix "Rerun tests" notification location under Darcula (IDEA-CR-25441)