package com.karateca.jstoolbox.sort;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.VisualPosition;
import com.intellij.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.editor.colors.EditorColorsScheme;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.popup.ComponentPopupBuilder;
import com.intellij.openapi.ui.popup.JBPopup;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.wm.IdeFocusManager;
import com.intellij.ui.awt.RelativePoint;

import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

public class SortSelection extends AnAction {

  private Project project;
  private SearchBox searchBox;

  public void actionPerformed(AnActionEvent actionEvent) {

    Editor editor = actionEvent.getData(PlatformDataKeys.EDITOR);
    if (editor == null) {
      return;
    }

    project = actionEvent.getProject();

    EditorColorsScheme scheme = EditorColorsManager.getInstance().getGlobalScheme();

    Font font = new Font(scheme.getEditorFontName(), Font.BOLD, scheme.getConsoleFontSize());

    searchBox = new SearchBox();
    int width = searchBox.getFontMetrics(font).stringWidth("w");
    Dimension dimension = new Dimension(width * 2, editor.getLineHeight());
    if (SystemInfo.isMac) {
      dimension.setSize(dimension.width * 2, dimension.height * 2);
    }

    searchBox.setSize(dimension);
    searchBox.setFocusable(true);
    searchBox.addFocusListener(new FocusListener() {
      @Override
      public void focusGained(FocusEvent focusEvent) {
        System.out.println("gained");
      }

      @Override
      public void focusLost(FocusEvent focusEvent) {
        System.out.println("lost");
      }
    });

    searchBox.setVisible(true);

    JBPopup popup = JBPopupFactory.getInstance()
            .createComponentPopupBuilder(searchBox, searchBox)
            .setCancelKeyEnabled(true)
            .createPopup();

    popup.show(guessBestLocation(editor));
    popup.setSize(dimension);

    focus();
  }

  private RelativePoint guessBestLocation(Editor editor) {
    VisualPosition logicalPosition = editor.getCaretModel().getVisualPosition();
    return getPointFromVisualPosition(editor, logicalPosition);
  }

  private RelativePoint getPointFromVisualPosition(Editor editor, VisualPosition logicalPosition) {
    Point p = editor.visualPositionToXY(new VisualPosition(logicalPosition.line, logicalPosition.column));
    return new RelativePoint(editor.getContentComponent(), p);

  }

  void focus() {
    ApplicationManager.getApplication().invokeLater(new Runnable() {
      @Override
      public void run() {
        IdeFocusManager.getInstance(project).requestFocus(searchBox, true);
      }
    });
  }
}

@@ -525,68 +525,8 @@
 ct;%0A
-import com.intellij.openapi.ui.popup.ComponentPopupBuilder;%0A
 impo
@@ -845,16 +845,82 @@
 istener;
+%0Aimport java.awt.event.KeyAdapter;%0Aimport java.awt.event.KeyEvent;
 %0A%0Apublic
@@ -1017,16 +1017,41 @@
 archBox;
+%0A  private JBPopup popup;
 %0A%0A  publ
@@ -2075,16 +2075,270 @@
 %0A    %7D);
+%0A    searchBox.addKeyListener(new KeyAdapter() %7B%0A      @Override%0A      public void keyPressed(KeyEvent keyEvent) %7B%0A        if (keyEvent.getKeyCode() == KeyEvent.VK_ENTER) %7B%0A          doSort();%0A        %7D%0A        super.keyPressed(keyEvent);%0A      %7D%0A    %7D);
 %0A%0A    se
@@ -2368,24 +2368,16 @@
 );%0A%0A    
-JBPopup 
 popup = 
@@ -2620,32 +2620,127 @@
   focus();%0A  %7D%0A%0A
+  private void doSort() %7B%0A    String text = searchBox.getText();%0A    popup.closeOk(null);%0A  %7D%0A%0A
   private Relati
@@ -3202,17 +3202,16 @@
 (), p);%0A
-%0A
   %7D%0A%0A  v

Add key listener