/*
 * Copyright 2000-2013 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.xml.util;

import com.intellij.codeInsight.completion.InsertHandler;
import com.intellij.codeInsight.completion.InsertionContext;
import com.intellij.codeInsight.completion.PrioritizedLookupElement;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupElementBuilder;
import com.intellij.codeInsight.lookup.LookupElementDecorator;
import com.intellij.codeInsight.lookup.LookupValueWithPriority;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.wm.WindowManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.xml.XmlToken;
import com.intellij.ui.ColorChooser;
import com.intellij.ui.ColorPickerListener;
import com.intellij.ui.ColorPickerListenerFactory;
import com.intellij.xml.XmlBundle;
import org.jetbrains.annotations.Nullable;

import java.awt.*;

/**
 * @author maxim
 */
public class UserColorLookup extends LookupElementDecorator<LookupElement> {
  private static final String COLOR_STRING = XmlBundle.message("choose.color.in.color.lookup");

  public UserColorLookup() {
    super(PrioritizedLookupElement.withPriority(LookupElementBuilder.create(COLOR_STRING).withInsertHandler(
      new InsertHandler<LookupElement>() {
        @Override
        public void handleInsert(InsertionContext context, LookupElement item) {
          handleUserSelection(context);
        }
      }), LookupValueWithPriority.HIGH));
  }

  private static void handleUserSelection(InsertionContext context) {
    Color myColorAtCaret = null;

    Editor selectedTextEditor = context.getEditor();
    PsiElement element = context.getFile().findElementAt(selectedTextEditor.getCaretModel().getOffset());

    if (element instanceof XmlToken) {
      myColorAtCaret = getColorFromElement(element);
    }

    context.getDocument().deleteString(context.getStartOffset(), context.getTailOffset());

    ColorPickerListener[] listeners = ColorPickerListenerFactory.createListenersFor(element);
    Color color = ColorChooser.chooseColor(WindowManager.getInstance().suggestParentWindow(context.getProject()),
                                           XmlBundle.message("choose.color.dialog.title"), myColorAtCaret, true, listeners, true);

    if (color != null) {
      String s = Integer.toHexString(color.getRGB() & 0xFFFFFF);
      if (s.length() != 6) {
        StringBuilder buf = new StringBuilder(s);
        for (int i = 6 - buf.length(); i > 0; --i) {
          buf.insert(0, '0');
        }
        s = buf.toString();
      }
      s = "#" + s;
      context.getDocument().insertString(context.getStartOffset(), s);
      context.getEditor().getCaretModel().moveToOffset(context.getTailOffset());
    }
  }

  @Nullable
  public static Color getColorFromElement(final PsiElement element) {
    if (!(element instanceof XmlToken)) return null;

    return ColorSampleLookupValue.getColor(element.getText());
  }
}

@@ -1060,63 +1060,8 @@
 ty;%0A
-import com.intellij.openapi.actionSystem.LangDataKeys;%0A
 impo
@@ -1375,21 +1375,132 @@
 lij.
-xml.XmlBundle
+ui.ColorUtil;%0Aimport com.intellij.util.Function;%0Aimport com.intellij.xml.XmlBundle;%0Aimport org.jetbrains.annotations.NotNull
 ;%0Aim
@@ -1763,34 +1763,368 @@
 %22);%0A
-%0A  public UserColorLookup(
+  private static final Function%3CColor,String%3E COLOR_TO_STRING_CONVERTER = new Function%3CColor, String%3E() %7B%0A    @Override%0A    public String fun(Color color) %7B%0A      return '#' + ColorUtil.toHex(color);%0A    %7D%0A  %7D;%0A%0A  public UserColorLookup() %7B%0A    this(COLOR_TO_STRING_CONVERTER);%0A  %7D%0A%0A  public UserColorLookup(final Function%3CColor, String%3E colorToStringConverter
 ) %7B%0A
@@ -2411,16 +2411,40 @@
 (context
+, colorToStringConverter
 );%0A     
@@ -2561,16 +2561,73 @@
  context
+, @NotNull Function%3CColor, String%3E colorToStringConverter
 ) %7B%0A    
@@ -3389,285 +3389,55 @@
 ing 
-s = Integer.toHexString(color.getRGB() & 0xFFFFFF);%0A      if (s.length() != 6) %7B%0A        StringBuilder buf = new StringBuilder(s);%0A        for (int i = 6 - buf.length(); i %3E 0; --i) %7B%0A          buf.insert(0, '0');%0A        %7D%0A        s = buf.toString();%0A      %7D%0A      s = %22#%22 + s
+colorString = colorToStringConverter.fun(color)
 ;%0A  
@@ -3501,17 +3501,27 @@
 fset(), 
-s
+colorString
 );%0A     

Insert rgba() value from ColorPicker if alpha had been set