package org.asciidoc.intellij.editor.notification;

import com.intellij.ide.BrowserUtil;
import com.intellij.ide.util.PropertiesComponent;
import com.intellij.openapi.application.ApplicationInfo;
import com.intellij.openapi.fileEditor.FileEditor;
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.ui.EditorNotificationPanel;
import com.intellij.ui.EditorNotifications;
import org.asciidoc.intellij.file.AsciiDocFileType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * Notify user that they should update the IntelliJ version.
 *
 * @author Alexander Schwartz (alexander.schwartz@gmx.net) 2021
 */
public class UpdateIntellijNotificationProvider extends EditorNotifications.Provider<EditorNotificationPanel> implements DumbAware {
  private static final Key<EditorNotificationPanel> KEY = Key.create("Please update IntelliJ");

  private static final String PLEASE_UPDATE_2021_1_1 = "asciidoc.intellij.pleaseupdate.2021.1.1";

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

  @Nullable
  @Override
  public EditorNotificationPanel createNotificationPanel(@NotNull VirtualFile file, @NotNull final FileEditor fileEditor, @NotNull Project project) {
    // only in AsciiDoc files
    if (file.getFileType() != AsciiDocFileType.INSTANCE) {
      return null;
    }

    // only if not previously disabled
    if (PropertiesComponent.getInstance().getBoolean(PLEASE_UPDATE_2021_1_1)) {
      return null;
    }

    final ApplicationInfo applicationInfo = ApplicationInfo.getInstance();
    if (applicationInfo.getFullVersion().equals("2021.1")) {
      final EditorNotificationPanel panel = new EditorNotificationPanel();
      panel.setText("The IDE version 2021.1 has a known indexing bug. Please update to version 2021.1.1!");
      panel.createActionLabel("Yes, tell me more!", ()
        -> BrowserUtil.browse("https://github.com/asciidoctor/asciidoctor-intellij-plugin/issues/725"));
      panel.createActionLabel("Do not show again", () -> {
        PropertiesComponent.getInstance().setValue(PLEASE_UPDATE_2021_1_1, true);
        EditorNotifications.updateAll();
      });
      return panel;
    }

    return null;
  }
}

@@ -370,16 +370,63 @@
 il.Key;%0A
+import com.intellij.openapi.util.SystemInfoRt;%0A
 import c
@@ -1157,16 +1157,115 @@
 .1.1%22;%0A%0A
+  private static final String PLEASE_UPDATE_2021_1_2 = %22asciidoc.intellij.pleaseupdate.2021.1.2%22;%0A%0A
   @NotNu
@@ -1646,51 +1646,233 @@
     
-// only if not previously disabled%0A    if (
+final ApplicationInfo applicationInfo = ApplicationInfo.getInstance();%0A%0A    if (SystemInfoRt.isMac%0A      && (applicationInfo.getFullVersion().equals(%222021.1%22) %7C%7C applicationInfo.getFullVersion().equals(%222021.1.1%22))%0A      && !
 Prop
@@ -1933,17 +1933,17 @@
 _2021_1_
-1
+2
 )) %7B%0A   
@@ -1949,159 +1949,719 @@
     
-return null;%0A    %7D%0A%0A    final ApplicationInfo applicationInfo = ApplicationInfo.getInstance();%0A    if (applicationInfo.getFullVersion().equals(%222021.1%22
+final EditorNotificationPanel panel = new EditorNotificationPanel();%0A      panel.setText(%22The IDE versions 2021.1 and 2021.1.1 on macOS can sometimes freeze. Please update to version 2021.1.2!%22);%0A      panel.createActionLabel(%22Yes, tell me more!%22, ()%0A        -%3E BrowserUtil.browse(%22https://github.com/asciidoctor/asciidoctor-intellij-plugin/issues/765%22));%0A      panel.createActionLabel(%22Do not show again%22, () -%3E %7B%0A        PropertiesComponent.getInstance().setValue(PLEASE_UPDATE_2021_1_2, true);%0A        EditorNotifications.updateAll();%0A      %7D);%0A      return panel;%0A    %7D%0A%0A    if (applicationInfo.getFullVersion().equals(%222021.1%22)%0A       && !PropertiesComponent.getInstance().getBoolean(PLEASE_UPDATE_2021_1_1
 )) %7B

add update notification for 2021.1 and 2021.1.1 on macOS (#765)