/*
 * Copyright 2017 The Chromium Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
package io.flutter.editor;

import com.intellij.openapi.editor.colors.EditorColors;
import com.intellij.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleUtilCore;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectLocator;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.ui.EditorNotificationPanel;
import com.intellij.ui.EditorNotifications;
import icons.FlutterIcons;
import io.flutter.FlutterConstants;
import io.flutter.sdk.FlutterSdk;
import io.flutter.utils.FlutterModuleUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

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

public class FlutterPubspecNotificationProvider extends EditorNotifications.Provider<EditorNotificationPanel> implements DumbAware {
  private static final Key<EditorNotificationPanel> KEY = Key.create("flutter.pubspec");

  @NotNull
  @Override
  public Key<EditorNotificationPanel> getKey() {
    return KEY;
  }

  @Nullable
  @Override
  public EditorNotificationPanel createNotificationPanel(@NotNull VirtualFile file, @NotNull FileEditor editor) {
    if (!file.isInLocalFileSystem()) {
      return null;
    }

    if (!FlutterConstants.PUBSPEC_YAML.equalsIgnoreCase(file.getName())) {
      return null;
    }

    // Check for a flutter sdk.
    final Project project = ProjectLocator.getInstance().guessProjectForFile(file);
    if (project == null) {
      return null;
    }

    final Module module = ModuleUtilCore.findModuleForFile(file, project);
    if (module == null || !FlutterModuleUtils.isFlutterModule(module)) {
      return null;
    }

    final FlutterSdk sdk = FlutterSdk.getFlutterSdk(project);
    if (sdk == null) {
      return null;
    }

    return new FlutterPubspecActionsPanel(file);
  }

  static class FlutterPubspecActionsPanel extends EditorNotificationPanel {
    @NotNull final VirtualFile myFile;

    FlutterPubspecActionsPanel(@NotNull VirtualFile file) {
      myFile = file;

      icon(FlutterIcons.Flutter);
      text("Flutter commands");

      createActionLabel("Packages get", "flutter.packages.get");
      createActionLabel("Packages upgrade", "flutter.packages.upgrade");
      myLinksPanel.add(new JSeparator(SwingConstants.VERTICAL));
      createActionLabel("Flutter upgrade", "flutter.upgrade");
      myLinksPanel.add(new JSeparator(SwingConstants.VERTICAL));
      createActionLabel("Flutter doctor", "flutter.doctor");

      // TODO: Add for 2017.1.
      //background(EditorColors.GUTTER_BACKGROUND);
    }

    // TODO: Remove for 2017.1.
    @Override
    public Color getBackground() {
      final Color color = EditorColorsManager.getInstance().getGlobalScheme().getColor(EditorColors.GUTTER_BACKGROUND);
      return color != null ? color : super.getBackground();
    }
  }
}

@@ -772,24 +772,63 @@
 ifications;%0A
+import com.intellij.ui.HyperlinkLabel;%0A
 import icons
@@ -2478,24 +2478,47 @@
 ds%22);%0A%0A     
+ HyperlinkLabel label =
  createActio
@@ -2562,32 +2562,100 @@
 ges.get%22);%0A     
+ label.setToolTipText(%22Install referenced packages%22);%0A%0A      label =
  createActionLab
@@ -2698,32 +2698,115 @@
 ages.upgrade%22);%0A
+      label.setToolTipText(%22Upgrade referenced packages to the latest versions%22);%0A%0A
       myLinksPan
@@ -2851,32 +2851,40 @@
 ERTICAL));%0A     
+ label =
  createActionLab
@@ -2921,24 +2921,108 @@
 .upgrade%22);%0A
+      label.setToolTipText(%22Upgrade the Flutter framework to the latest version%22);%0A%0A
       myLink
@@ -3079,16 +3079,24 @@
 );%0A     
+ label =
  createA
@@ -3142,16 +3142,91 @@
 octor%22);
+%0A      label.setToolTipText(%22Validate installed tools and their versions%22);
 %0A%0A      

add tooltips to the pubspec editor inspector (#736)