/*
 * 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.codeInsight.lookup;

import com.intellij.openapi.util.Iconable;
import com.intellij.openapi.util.ScalableIcon;
import com.intellij.psi.PsiElement;
import com.intellij.psi.meta.PsiMetaData;
import com.intellij.psi.util.PsiUtilCore;
import com.intellij.ui.SizedIcon;
import com.intellij.util.PlatformIcons;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

/**
 * @author peter
 */
public class DefaultLookupItemRenderer extends LookupElementRenderer<LookupItem>{
  public static final DefaultLookupItemRenderer INSTANCE = new DefaultLookupItemRenderer();

  @Override
  public void renderElement(final LookupItem item, final LookupElementPresentation presentation) {
    presentation.setIcon(getRawIcon(item));

    presentation.setItemText(getName(item));
    presentation.setItemTextBold(item.getAttribute(LookupItem.HIGHLIGHTED_ATTR) != null);
    presentation.setTailText(getText2(item), item.getAttribute(LookupItem.TAIL_TEXT_SMALL_ATTR) != null);
    presentation.setTypeText(getText3(item), null);
  }

  /**
   * @deprecated use {@link #getRawIcon(LookupElement)}
   */
  @Nullable
  @Deprecated
  public static Icon getRawIcon(final LookupElement item, boolean real) {
    return getRawIcon(item);
  }

  @Nullable
  public static Icon getRawIcon(final LookupElement item) {
    Icon icon = _getRawIcon(item);
    if (icon instanceof ScalableIcon) icon = ((ScalableIcon)icon).scale(1f);
    if (icon != null && icon.getIconHeight() > PlatformIcons.CLASS_ICON.getIconHeight()) {
      return new SizedIcon(icon, icon.getIconWidth(), PlatformIcons.CLASS_ICON.getIconHeight());
    }
    return icon;
  }

  @Nullable
  private static Icon _getRawIcon(LookupElement item) {
    if (item instanceof LookupItem) {
      Icon icon = (Icon)((LookupItem)item).getAttribute(LookupItem.ICON_ATTR);
      if (icon != null) return icon;
    }

    Object o = item.getObject();

    if (o instanceof Iconable && !(o instanceof PsiElement)) {
      return ((Iconable)o).getIcon(Iconable.ICON_FLAG_VISIBILITY);
    }

    final PsiElement element = item.getPsiElement();
    if (element != null && element.isValid()) {
      return element.getIcon(Iconable.ICON_FLAG_VISIBILITY);
    }
    return null;
  }


  @Nullable
  private static String getText3(final LookupItem item) {
    Object o = item.getObject();
    String text;
    if (o instanceof LookupValueWithUIHint) {
      text = ((LookupValueWithUIHint)o).getTypeHint();
    }
    else {
      text = (String)item.getAttribute(LookupItem.TYPE_TEXT_ATTR);
    }
    return text;
  }

  private static String getText2(final LookupItem item) {
    return (String)item.getAttribute(LookupItem.TAIL_TEXT_ATTR);
  }

  private static String getName(final LookupItem item){
    final String presentableText = item.getPresentableText();
    if (presentableText != null) return presentableText;
    final Object o = item.getObject();
    String name = null;
    if (o instanceof PsiElement) {
      final PsiElement element = (PsiElement)o;
      if (element.isValid()) {
        name = PsiUtilCore.getName(element);
      }
    }
    else if (o instanceof PsiMetaData) {
      name = ((PsiMetaData)o).getName();
    }
    else if (o instanceof PresentableLookupValue ) {
      name = ((PresentableLookupValue)o).getPresentation();
    }
    else {
      name = String.valueOf(o);
    }
    if (name == null){
      name = "";
    }

    return name;
  }

}

@@ -722,24 +722,76 @@
 alableIcon;%0A
+import com.intellij.openapi.util.registry.Registry;%0A
 import com.i
@@ -1143,16 +1143,19 @@
 okupItem
+%3C?%3E
 %3E%7B%0A  pub
@@ -1282,22 +1282,16 @@
 Element(
-final 
 LookupIt
@@ -1296,20 +1296,17 @@
 Item
+%3C?%3E
  item,
- final
  Loo
@@ -1755,16 +1755,46 @@
 %7D%0A   */%0A
+  @SuppressWarnings(%22unused%22)%0A
   @Nulla
@@ -2461,16 +2461,19 @@
 okupItem
+%3C?%3E
 )item).g
@@ -2675,32 +2675,85 @@
 able)o).getIcon(
+Registry.is(%22ide.completion.show.visibility.icon%22) ? 
 Iconable.ICON_FL
@@ -2757,32 +2757,36 @@
 _FLAG_VISIBILITY
+ : 0
 );%0A    %7D%0A%0A    fi
@@ -2909,16 +2909,69 @@
 getIcon(
+Registry.is(%22ide.completion.show.visibility.icon%22) ? 
 Iconable
@@ -2991,16 +2991,20 @@
 SIBILITY
+ : 0
 );%0A    %7D
@@ -3027,16 +3027,51 @@
 ;%0A  %7D%0A%0A%0A
+  @SuppressWarnings(%22deprecation%22)%0A
   @Nulla
@@ -3107,22 +3107,16 @@
 etText3(
-final 
 LookupIt
@@ -3109,32 +3109,35 @@
 Text3(LookupItem
+%3C?%3E
  item) %7B%0A    Obj
@@ -3425,22 +3425,16 @@
 etText2(
-final 
 LookupIt
@@ -3431,24 +3431,27 @@
 2(LookupItem
+%3C?%3E
  item) %7B%0A   
@@ -3513,24 +3513,59 @@
 ATTR);%0A  %7D%0A%0A
+  @SuppressWarnings(%22deprecation%22)%0A
   private st
@@ -3584,22 +3584,16 @@
 getName(
-final 
 LookupIt
@@ -3594,16 +3594,19 @@
 okupItem
+%3C?%3E
  item)%7B%0A

remove stripping visibility icons after the presentations have gone all real (IDEA-201824)