/*
 * Copyright 2016 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.inspections;

import com.intellij.openapi.diagnostic.Logger;
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.util.Key;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import com.intellij.ui.EditorNotificationPanel;
import com.intellij.ui.EditorNotifications;
import com.jetbrains.lang.dart.DartFileType;
import com.jetbrains.lang.dart.DartLanguage;
import io.flutter.FlutterBundle;
import io.flutter.sdk.FlutterSdk;
import io.flutter.settings.FlutterUIConfig;
import io.flutter.utils.FlutterModuleUtils;
import org.jetbrains.annotations.NotNull;

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

  private static final Logger LOG = Logger.getInstance(SdkConfigurationNotificationProvider.class);

  private final Project project;

  public SdkConfigurationNotificationProvider(@NotNull Project project) {
    this.project = project;
  }

  @SuppressWarnings("SameReturnValue")
  private static EditorNotificationPanel createNoFlutterSdkPanel() {
    final EditorNotificationPanel panel = new EditorNotificationPanel();
    panel.setText(FlutterBundle.message("flutter.no.sdk.warning"));
    panel.createActionLabel("Dismiss", () -> panel.setVisible(false));

    // TODO(skybrian) we should add a link to go to the Dart SDK panel.
    // However, not yet because this will change with 2017.1 to be project-specific.

    return panel;
  }

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

  @Override
  public EditorNotificationPanel createNotificationPanel(@NotNull VirtualFile file, @NotNull FileEditor fileEditor) {
    if (file.getFileType() != DartFileType.INSTANCE) return null;

    final PsiFile psiFile = PsiManager.getInstance(project).findFile(file);
    if (psiFile == null) return null;

    if (psiFile.getLanguage() != DartLanguage.INSTANCE) return null;

    final Module module = ModuleUtilCore.findModuleForPsiElement(psiFile);
    if (module == null) return null;

    if (!FlutterModuleUtils.isFlutterModule(module)) return null;

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

    if (!flutterSdk.getVersion().isSupported()) {
      return createOutOfDateFlutterSdkPanel(flutterSdk);
    }

    return null;
  }

  private EditorNotificationPanel createOutOfDateFlutterSdkPanel(@NotNull FlutterSdk sdk) {

    final FlutterUIConfig settings = FlutterUIConfig.getInstance();
    if (settings.shouldIgnoreOutOfDateFlutterSdks()) return null;

    final EditorNotificationPanel panel = new EditorNotificationPanel();
    panel.setText(FlutterBundle.message("flutter.old.sdk.warning"));
    panel.createActionLabel("Dismiss", () -> {
      settings.setIgnoreOutOfDateFlutterSdks();
      panel.setVisible(false);
    });

    return panel;
  }
}

@@ -845,24 +845,99 @@
 tterBundle;%0A
+import io.flutter.bazel.Workspace;%0Aimport io.flutter.bazel.WorkspaceCache;%0A
 import io.fl
@@ -1431,24 +1431,33 @@
 .class);%0A%0A  
+@NotNull 
 private fina
@@ -2256,16 +2256,22 @@
 NotNull 
+final 
 VirtualF
@@ -2289,16 +2289,22 @@
 NotNull 
+final 
 FileEdit
@@ -2316,24 +2316,332 @@
 leEditor) %7B%0A
+%0A    // If this is a Bazel configured Flutter project, exit immediately, neither of the notifications should be shown for this project type.%0A    final Workspace workspace = WorkspaceCache.getInstance(project).getNow();%0A    if (workspace != null && workspace.usesFlutter(project)) %7B%0A      return null;%0A    %7D%0A%0A
     if (file
@@ -2798,32 +2798,12 @@
 null
-) return null;%0A%0A    if (
+ %7C%7C 
 psiF
@@ -2961,32 +2961,12 @@
 null
-) return null;%0A%0A    if (
+ %7C%7C 
 !Flu
@@ -3161,29 +3161,33 @@
 el();%0A    %7D%0A
-%0A
     
+else 
 if (!flutter

Change the SDK configuration notification provider to exit if the project is configured as a Bazel-Flutter project. (#1121)