// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.ide.browsers.actions;

import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.fileEditor.FileEditorLocation;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.FileEditorState;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.UserDataHolderBase;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.*;
import com.intellij.ui.jcef.JCEFHtmlPanel;
import com.intellij.util.Alarm;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.beans.PropertyChangeListener;
import java.io.IOException;

/**
 * @author Konstantin Bulenkov
 */
public class WebPreviewFileEditor extends UserDataHolderBase implements FileEditor {

  private final Project myProject;
  private final VirtualFile myFile;
  private final JCEFHtmlPanel myPanel;

  public WebPreviewFileEditor(@NotNull Project project, @NotNull WebPreviewVirtualFile file) {
    myProject = project;
    myFile = file.getOriginalFile();
    myPanel = new JCEFHtmlPanel(myFile.getUrl());
    myPanel.getCefBrowser().createImmediately();
    reloadHtml();
    Alarm alarm = new Alarm(this);
    Runnable run = new Runnable() {
      @Override
      public void run() {
        reloadHtml();
        alarm.addRequest(this, 10000);
      }
    };
    alarm.addRequest(run, 10000);

    VirtualFileManager.getInstance().addVirtualFileManagerListener(new MyVirtualFileListener(), this);
  }

  private void reloadHtml() {
    try {
      myPanel.setHtml(FileUtil.loadTextAndClose(myFile.getInputStream()));
    }
    catch (IOException e) {
      e.printStackTrace();
    }
  }

  @Override
  public @NotNull JComponent getComponent() {
    return myPanel.getComponent();
  }

  @Override
  public @Nullable JComponent getPreferredFocusedComponent() {
    return myPanel.getComponent();
  }

  @Override
  public @Nls(capitalization = Nls.Capitalization.Title) @NotNull String getName() {
    return "Preview for " + myFile.getName();
  }

  @Override
  public void setState(@NotNull FileEditorState state) {

  }

  @Override
  public boolean isModified() {
    return false;
  }

  @Override
  public boolean isValid() {
    return true;
  }

  @Override
  public void addPropertyChangeListener(@NotNull PropertyChangeListener listener) {

  }

  @Override
  public void removePropertyChangeListener(@NotNull PropertyChangeListener listener) {

  }

  @Override
  public @Nullable FileEditorLocation getCurrentLocation() {
    return null;
  }

  @Override
  public void dispose() {

  }

  class MyVirtualFileListener implements VirtualFileListener, @NotNull VirtualFileManagerListener {
    @Override
    public void contentsChanged(@NotNull VirtualFileEvent event) {
      if (myFile.equals(event.getFile())) {
        reloadHtml();
      }
    }

    @Override
    public void beforeFileDeletion(@NotNull VirtualFileEvent event) {
      if (myFile.equals(event.getFile())) {
        FileEditorManager.getInstance(myProject).closeFile(myFile);
      }
    }
  }
}

@@ -178,16 +178,236 @@
 tions;%0A%0A
+import com.intellij.openapi.editor.Document;%0Aimport com.intellij.openapi.editor.event.DocumentEvent;%0Aimport com.intellij.openapi.editor.event.DocumentListener;%0Aimport com.intellij.openapi.fileEditor.FileDocumentManager;%0A
 import c
@@ -512,66 +512,8 @@
 on;%0A
-import com.intellij.openapi.fileEditor.FileEditorManager;%0A
 impo
@@ -740,17 +740,140 @@
 api.vfs.
-*
+VirtualFile;%0Aimport com.intellij.psi.PsiDocumentManager;%0Aimport com.intellij.psi.PsiFile;%0Aimport com.intellij.psi.PsiManager
 ;%0Aimport
@@ -1285,44 +1285,8 @@
 or %7B
-%0A%0A  private final Project myProject;
 %0A  p
@@ -1457,33 +1457,8 @@
 ) %7B%0A
-    myProject = project;%0A
     
@@ -1650,40 +1650,287 @@
     
-Runnable run = new Runnable
+PsiFile psiFile = PsiManager.getInstance(project).findFile(myFile);%0A    if (psiFile != null) %7B%0A      Document document = PsiDocumentManager.getInstance(project).getDocument(psiFile);%0A      if (document != null) %7B%0A        document.addDocumentListener(new DocumentListener
 () %7B%0A
+    
     
@@ -1943,24 +1943,28 @@
 rride%0A      
+  
+  
 public void 
@@ -1967,35 +1967,123 @@
 oid 
-run() %7B%0A        reloadHtml(
+documentChanged(@NotNull DocumentEvent event) %7B%0A            FileDocumentManager.getInstance().saveDocument(document
 );%0A 
@@ -2093,54 +2093,47 @@
     
+    
 alarm.
-addRequest(this, 10000);%0A      %7D%0A    %7D;%0A
+cancelAllRequests();%0A        
     
@@ -2153,124 +2153,71 @@
 est(
-run
+() -%3E reloadHtml()
 , 100
-00
 );%0A
-%0A    VirtualFileManager.getInstance().addVirtualFileManagerListener(new MyVirtualFileListener(), this);
+          %7D%0A        %7D);%0A      %7D%0A    %7D
 %0A  %7D
@@ -3319,484 +3319,7 @@
 %0A  %7D
-%0A%0A  class MyVirtualFileListener implements VirtualFileListener, @NotNull VirtualFileManagerListener %7B%0A    @Override%0A    public void contentsChanged(@NotNull VirtualFileEvent event) %7B%0A      if (myFile.equals(event.getFile())) %7B%0A        reloadHtml();%0A      %7D%0A    %7D%0A%0A    @Override%0A    public void beforeFileDeletion(@NotNull VirtualFileEvent event) %7B%0A      if (myFile.equals(event.getFile())) %7B%0A        FileEditorManager.getInstance(myProject).closeFile(myFile);%0A      %7D%0A    %7D%0A  %7D
 %0A%7D%0A

add document listener to update Web Preview