/*
 * Copyright 2000-2017 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.ui.debugger.extensions;

import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.actionSystem.ex.AnActionListener;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.ui.components.JBScrollPane;
import com.intellij.ui.debugger.UiDebuggerExtension;

import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import java.awt.*;

import static com.intellij.openapi.keymap.KeymapUtil.getActiveKeymapShortcuts;

public class ActionTracer implements UiDebuggerExtension, AnActionListener {

  private final Logger LOG = Logger.getInstance("ActionTracer");
  
  private JTextArea myText;
  private JPanel myComponent;

  @Override
  public JComponent getComponent() {
    if (myComponent == null) {
      myText = new JTextArea();
      final JBScrollPane log = new JBScrollPane(myText);
      final AnAction clear = new AnAction("Clear", "Clear log", AllIcons.General.Reset) {
        @Override
        public void actionPerformed(AnActionEvent e) {
          myText.setText(null);
        }
      };
      myComponent = new JPanel(new BorderLayout());
      final DefaultActionGroup group = new DefaultActionGroup();
      group.add(clear);
      myComponent.add(ActionManager.getInstance().createActionToolbar("ActionTracer", group, true).getComponent(), BorderLayout.NORTH);
      myComponent.add(log);

      ActionManager.getInstance().addAnActionListener(this);
    }

    return myComponent;
  }

  @Override
  public String getName() {
    return "Actions";
  }

  @Override
  public void beforeActionPerformed(AnAction action, DataContext dataContext, AnActionEvent event) {
  }

  @Override
  public void afterActionPerformed(AnAction action, DataContext dataContext, AnActionEvent event) {
    StringBuilder out = new StringBuilder(String.format("%1$tF %1$tT,%1$tL ", System.currentTimeMillis()));
    final ActionManager actionManager = ActionManager.getInstance();
    final String id = actionManager.getId(action);
    out.append("id=").append(id);
    if (id != null) {
      out.append("; shortcuts:");
      final Shortcut[] shortcuts = getActiveKeymapShortcuts(id).getShortcuts();
      for (int i = 0; i < shortcuts.length; i++) {
        Shortcut shortcut = shortcuts[i];
        out.append(shortcut);
        if (i < shortcuts.length - 1) {
          out.append(",");
        }
      }
    }
    out.append("; class: ").append(action.getClass().getName());
    out.append("\n");
    final Document doc = myText.getDocument();
    try {
      doc.insertString(doc.getLength(), out.toString(), null);
      SwingUtilities.invokeLater(() -> {
        final int y = (int)myText.getBounds().getMaxY();
        myText.scrollRectToVisible(new Rectangle(0, y, myText.getBounds().width, 0));
      });
    }
    catch (BadLocationException e) {
      LOG.error(e);
    }
  }

  @Override
  public void beforeEditorTyping(char c, DataContext dataContext) {
  }

  @Override
  public void disposeUiResources() {
    ActionManager.getInstance().removeAnActionListener(this);
    myComponent = null;
    myText = null;
    
  }
}
