/*******************************************************************************
 * Copyright (c) 2012-2014 Codenvy, S.A.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Codenvy, S.A. - initial API and implementation
 *******************************************************************************/
package com.codenvy.ide.ext.datasource.client.sqleditor.codeassist;

import com.codenvy.ide.text.BadLocationException;
import com.codenvy.ide.text.Document;
import com.codenvy.ide.text.Region;
import com.codenvy.ide.text.RegionImpl;
import com.codenvy.ide.text.edits.MalformedTreeException;
import com.codenvy.ide.text.edits.ReplaceEdit;
import com.codenvy.ide.texteditor.api.codeassistant.Completion;
import com.codenvy.ide.texteditor.api.codeassistant.CompletionProposal;
import com.codenvy.ide.util.loging.Log;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;
import org.vectomatic.dom.svg.ui.SVGImage;

public class SqlCodeCompletionProposal implements CompletionProposal {

    protected String            name;
    protected String            replacementString;
    protected int               cursorPosition;
    protected int               selectionLength = 0;
    protected InvocationContext invocationContext;


    public SqlCodeCompletionProposal(String name) {
        this(name, name, name.length());
    }

    public SqlCodeCompletionProposal(String name, String replacementString, int cursorPosition) {
        this.name = name;
        this.replacementString = replacementString;
        this.cursorPosition = cursorPosition;

    }

    @Override
    public Widget getAdditionalProposalInfo() {
        return null;
    }

    @Override
    public String getDisplayString() {
        return new SafeHtmlBuilder().appendEscaped(name).toSafeHtml().asString();
    }

    @Override
    public Image getImage() {
        Image image = new Image();
        image.setResource(invocationContext.getResources().sqlCompletionIcon());
        return image;
    }
    
    @Override
    public SVGImage getSVGImage() {
        // TODO create SVG image to be displayed in SQL autocomplition.
        return null;
    }

    @Override
    public char[] getTriggerCharacters() {
        // when space is entered we may have completion
        return new char[]{' '};
        // return new char[0];
    }

    @Override
    public boolean isAutoInsertable() {
        return true;
    }

    @Override
    public void getCompletion(CompletionCallback completionCallback) {
        completionCallback.onCompletion(new Completion() {
            /** {@inheritDoc} */
            @Override
            public void apply(Document document) {
                ReplaceEdit replaceEdit =
                                          new ReplaceEdit(
                                                          invocationContext.getOffset()
                                                              - invocationContext.getQuery().getLastQueryPrefix().length(),
                                                          invocationContext.getQuery().getLastQueryPrefix().length(), replacementString);
                try {
                    replaceEdit.apply(document);
                    // Do not try a new codeassist proposal
                    // invocationContext.getEditor().doOperation(TextEditorOperations.CODEASSIST_PROPOSALS);
                } catch (MalformedTreeException e) {
                    Log.error(getClass(), e);
                } catch (BadLocationException e) {
                    Log.error(getClass(), e);
                }
            }

            /** {@inheritDoc} */
            @Override
            public Region getSelection(Document document) {
                return new RegionImpl(invocationContext.getOffset() + cursorPosition
                                      - invocationContext.getQuery().getLastQueryPrefix().length(), selectionLength);
            }
        });
    }

    public String getName() {
        return name;
    }

    public void setInvocationContext(InvocationContext invocationContext) {
        this.invocationContext = invocationContext;
    }

    public String getReplacementString() {
        return replacementString;
    }

    public void setReplacementString(String replacementString) {
        this.replacementString = replacementString;
    }

    public int getCursorPosition() {
        return cursorPosition;
    }

    public void setCursorPosition(int cursorPosition) {
        this.cursorPosition = cursorPosition;
    }

    public void setSelectionLength(int selectionLength) {
        this.selectionLength = selectionLength;
    }

}

@@ -584,16 +584,52 @@
 ssist;%0A%0A
+import com.codenvy.ide.api.ui.Icon;%0A
 import c
@@ -662,32 +662,32 @@
 ationException;%0A
-
 import com.coden
@@ -1124,52 +1124,8 @@
 er;%0A
-import com.google.gwt.user.client.ui.Image;%0A
 impo
@@ -1168,51 +1168,8 @@
 get;
-%0Aimport org.vectomatic.dom.svg.ui.SVGImage;
 %0A%0Apu
@@ -2073,21 +2073,19 @@
 ic I
-mage
+con
  getI
-mage
+con
 () %7B
@@ -2097,290 +2097,100 @@
     
-Image image = new Image();%0A        image.setResource(invocationContext.getResources().sqlCompletionIcon());%0A        return image;%0A    %7D%0A    %0A    @Override%0A    public SVGImage getSVGImage() %7B%0A        // TODO create SVG image to be displayed in SQL autocomplition.%0A        return null
+return new Icon(%22sql.completion.icon%22, invocationContext.getResources().sqlCompletionIcon())
 ;%0A  

use com.codenvy.ide.api.ui.Icon