package com.miguelgaeta.bootstrap.mg_text;

import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;

import com.miguelgaeta.bootstrap.mg_recycler.MGRecyclerAdapterSimple;
import com.miguelgaeta.bootstrap.mg_recycler.MGRecyclerViewHolder;

import java.util.Map;

import lombok.NonNull;
import lombok.Setter;

/**
 * Created by mrkcsc on 5/24/15.
 */
@SuppressWarnings({"MismatchedQueryAndUpdateOfCollection", "UnusedDeclaration"})
public class MGTextEditMentionAdapter extends MGRecyclerAdapterSimple {

    @Setter
    private MGTextEdit.OnMentionsRecyclerItem onItem;

    @Setter
    private MGTextEdit editText;

    @Setter
    private Map<String, Object> tags;

    public MGTextEditMentionAdapter(@NonNull RecyclerView recycler) {
        super(recycler);
    }

    /**
     * When we need to create a view holder delegate
     * the initialization and configuration
     * to the callee.
     */
    @Override
    public MGRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return onItem.onItem(this);
    }

    /**
     * Fetch data item at position.
     */
    public String getTag(int position) {

        return (String)getItem(position);
    }

    /**
     * Look into the superset of all
     * available tags to fetch
     * associated data.
     */
    public Object getTagData(int position) {

        return tags.get(getTag(position));
    }

    public void mentionClicked(int position) {

        insertMention(editText, getTag(position));
    }

    private void insertMention(MGTextEdit editText, String tag) {

        // Fetch position of cursor.
        int position = editText.getCursorPosition();

        String lastToken = MGTextEditMentionUtils.getPartialMentionToken(editText);

        if (lastToken != null) {

            // Insert tag, replacing any partial tag already in place.
            editText.insert(tag + " ", position - lastToken.length(), position);
        }
    }
}

@@ -1788,24 +1788,88 @@
 != null) %7B%0A%0A
+            int positionStart = position - lastToken.length();%0A%0A
             
@@ -1912,25 +1912,8 @@
  tag
- already in place
 .%0A  
@@ -1945,16 +1945,17 @@
 (tag + %22
+ 
  %22, posi
@@ -1962,39 +1962,216 @@
 tion
- - lastToken.length(), position
+Start, position);%0A%0A            // Selection hack to prevent next user inputted key%0A            // press from overwriting what just got input.%0A            editText.setSelection(positionStart + tag.length() + 1
 );%0A 

Edit text insertion hack.