/*
 * Copyright 2000-2009 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.psi.impl.source.resolve.reference.impl.providers;

import com.intellij.codeInsight.lookup.LookupElementBuilder;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.extensions.ExtensionPointListener;
import com.intellij.openapi.extensions.PluginDescriptor;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.file.FileLookupInfoProvider;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @author spleaner
 */
public class FileInfoManager implements Disposable {
  private final Map<FileType, FileLookupInfoProvider> myFileType2InfoProvider = new HashMap<>();

  public FileInfoManager() {
    for (FileLookupInfoProvider provider : FileLookupInfoProvider.EP_NAME.getExtensions()) {
      for (FileType type : provider.getFileTypes()) {
        myFileType2InfoProvider.put(type, provider);
      }
    }

    FileLookupInfoProvider.EP_NAME.addExtensionPointListener(new ExtensionPointListener<FileLookupInfoProvider>() {
      @Override
      public void extensionAdded(@NotNull FileLookupInfoProvider extension, @NotNull PluginDescriptor pluginDescriptor) {
        for (FileType type : extension.getFileTypes()) {
          myFileType2InfoProvider.put(type, extension);
        }
      }

      @Override
      public void extensionRemoved(@NotNull FileLookupInfoProvider extension, @NotNull PluginDescriptor pluginDescriptor) {
        for (FileType type : extension.getFileTypes()) {
          myFileType2InfoProvider.remove(type);
        }
      }
    }, this);
  }

  public static FileInfoManager getFileInfoManager() {
    return ServiceManager.getService(FileInfoManager.class);
  }

  public static Object getFileLookupItem(PsiElement psiElement) {
    if (!(psiElement instanceof PsiFile) || !(psiElement.isPhysical())) {
      return psiElement;
    }

    final PsiFile file = (PsiFile)psiElement;
    return getFileInfoManager()._getLookupItem(file, file.getName(), file.getIcon(0));
  }

  @Nullable
  public static String getFileAdditionalInfo(PsiElement psiElement) {
    return getFileInfoManager()._getInfo(psiElement);
  }

  @Nullable
  private String _getInfo(PsiElement psiElement) {
    if (!(psiElement instanceof PsiFile) || !(psiElement.isPhysical())) {
      return null;
    }

    final PsiFile psiFile = (PsiFile)psiElement;
    final FileLookupInfoProvider provider = myFileType2InfoProvider.get(psiFile.getFileType());
    if (provider != null) {
      final VirtualFile virtualFile = psiFile.getVirtualFile();
      if (virtualFile != null) {
        final Pair<String, String> info = provider.getLookupInfo(virtualFile, psiElement.getProject());
        return Pair.getSecond(info);
      }
    }

    return null;
  }

  public static LookupElementBuilder getFileLookupItem(PsiElement psiElement, String encoded, Icon icon) {
    if (!(psiElement instanceof PsiFile) || !(psiElement.isPhysical())) {
      return LookupElementBuilder.create(psiElement, encoded).withIcon(icon);
    }

    return getFileInfoManager()._getLookupItem((PsiFile)psiElement, encoded, icon);
  }

  public LookupElementBuilder _getLookupItem(@NotNull final PsiFile file, String name, Icon icon) {
    LookupElementBuilder builder = LookupElementBuilder.create(file, name).withIcon(icon);

    final String info = _getInfo(file);
    if (info != null) {
      return builder.withTailText(String.format(" (%s)", info), true);
    }

    return builder;
  }

  @Override
  public void dispose() {
    myFileType2InfoProvider.clear();
  }
}

@@ -827,128 +827,8 @@
 er;%0A
-import com.intellij.openapi.extensions.ExtensionPointListener;%0Aimport com.intellij.openapi.extensions.PluginDescriptor;%0A
 impo
@@ -1722,208 +1722,27 @@
 ner(
-new ExtensionPointListener%3CFileLookupInfoProvider%3E() %7B%0A      @Override%0A      public void extensionAdded(@NotNull FileLookupInfoProvider extension, @NotNull PluginDescriptor pluginDescriptor) %7B%0A       
+%0A      (e, pd) -%3E %7B
  for
@@ -1756,32 +1756,24 @@
 ype type : e
-xtension
 .getFileType
@@ -1772,36 +1772,24 @@
 FileTypes())
- %7B%0A         
  myFileType2
@@ -1816,185 +1816,32 @@
 e, e
-xtension);%0A        %7D%0A      %7D%0A%0A      @Override%0A      public void extensionRemoved(@NotNull FileLookupInfoProvider extension, @NotNull PluginDescriptor pluginDescriptor) %7B%0A       
+); %7D,%0A      (e, pd) -%3E %7B
  for
@@ -1859,24 +1859,16 @@
 type : e
-xtension
 .getFile
@@ -1879,20 +1879,8 @@
 s())
- %7B%0A         
  myF
@@ -1917,33 +1917,18 @@
 pe);
+ %7D, 
 %0A     
-   %7D%0A      %7D%0A    %7D,
  thi

use simplified api