package io.rapidpro.sdk.chat;

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

import java.util.ArrayList;
import java.util.List;

import io.rapidpro.sdk.FcmClient;
import io.rapidpro.sdk.UiConfiguration;
import io.rapidpro.sdk.chat.tags.OnTagClickListener;
import io.rapidpro.sdk.core.models.FlowRuleset;
import io.rapidpro.sdk.core.models.Message;

/**
 * Created by johncordeiro on 7/21/15.
 */
class ChatMessagesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int VIEW_TYPE_TAGS = 0;
    private static final int VIEW_TYPE_TEXT_MESSAGES = 1;

    private FlowRuleset rulesets;
    private List<Message> chatMessages;
    private final int iconResource;

    private ChatMessageViewHolder.OnChatMessageSelectedListener onChatMessageSelectedListener;
    private final OnTagClickListener onTagClickListener;

    ChatMessagesAdapter(OnTagClickListener onTagClickListener) {
        this.onTagClickListener = onTagClickListener;
        this.chatMessages = new ArrayList<>();
        this.iconResource = FcmClient.getUiConfiguration().getIconResource() != UiConfiguration.INVALID_VALUE ?
                FcmClient.getUiConfiguration().getIconResource() : FcmClient.getAppIcon();
        setHasStableIds(true);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case VIEW_TYPE_TEXT_MESSAGES:
                return new ChatMessageViewHolder(parent.getContext(), parent, iconResource);
            case VIEW_TYPE_TAGS:
                return new RulesViewHolder(parent.getContext(), parent, onTagClickListener);
            default:
                throw new IllegalStateException("View type not recognized by ChatMessagesAdapter");
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case VIEW_TYPE_TEXT_MESSAGES:
                ChatMessageViewHolder chatMessageViewHolder = ((ChatMessageViewHolder)holder);
                chatMessageViewHolder.setOnChatMessageSelectedListener(onChatMessageSelectedListener);
                chatMessageViewHolder.bindView(getItem(position));
                break;
            case VIEW_TYPE_TAGS:
                RulesViewHolder rulesViewHolder = ((RulesViewHolder)holder);
                rulesViewHolder.bind(rulesets);
        }
    }

    private Message getItem(int position) {
        position = hasRulesets() ? position - 1 : position;
        return chatMessages.get(position);
    }

    @Override
    public int getItemViewType(int position) {
        return hasRulesets() && position == 0 ? VIEW_TYPE_TAGS : VIEW_TYPE_TEXT_MESSAGES;
    }

    @Override
    public long getItemId(int position) {
        switch (getItemViewType(position)) {
            case VIEW_TYPE_TEXT_MESSAGES:
                Message chatMessage = getItem(position);
                return chatMessage.getId() != null ? chatMessage.getId().hashCode() : 0;
            case VIEW_TYPE_TAGS:
                return rulesets.getUuid().hashCode();
            default:
                return super.getItemId(position);
        }
    }

    @Override
    public int getItemCount() {
        return hasRulesets() ? chatMessages.size() + 1 : chatMessages.size();
    }

    void setRulesets(FlowRuleset rulesets) {
        this.rulesets = rulesets;
        notifyDataSetChanged();
    }

    void removeRulesets() {
        if (hasRulesets()) {
            rulesets = null;
            notifyDataSetChanged();
        }
    }

    Message getLastMessage() {
        return chatMessages.isEmpty() ? null : getItem(getFirstPosition());
    }

    private int getFirstPosition() {
        return hasRulesets() ? 1 : 0;
    }

    void setMessages(List<Message> messages) {
        this.chatMessages = messages;
        notifyDataSetChanged();
    }

    void addChatMessage(Message message) {
        int location = chatMessages.indexOf(message);
        if (location >= 0) {
            chatMessages.set(location, message);
            notifyItemChanged(location);
        } else {
            int position = getFirstPosition();
            chatMessages.add(position, message);
            notifyItemInserted(position);
        }
    }

    private boolean hasRulesets() {
        return rulesets != null;
    }

    public void removeChatMessage(Message message) {
        int indexOfMessage = chatMessages.indexOf(message);
        if(indexOfMessage >= 0) {
            chatMessages.remove(indexOfMessage);
            notifyItemRemoved(indexOfMessage);
        }
    }

    public void setOnChatMessageSelectedListener(ChatMessageViewHolder.OnChatMessageSelectedListener onChatMessageSelectedListener) {
        this.onChatMessageSelectedListener = onChatMessageSelectedListener;
    }
}

@@ -1438,35 +1438,26 @@
  VIEW_TYPE_T
-EXT_MESS
 AG
-E
 S:%0A         
@@ -1474,27 +1474,21 @@
 urn new 
-ChatMessage
+Rules
 ViewHold
@@ -1519,28 +1519,34 @@
 parent, 
-iconResource
+onTagClickListener
 );%0A     
@@ -1560,34 +1560,43 @@
 case VIEW_TYPE_T
+EXT_MESS
 AG
+E
 S:%0A             
@@ -1601,37 +1601,43 @@
      return new 
-Rules
+ChatMessage
 ViewHolder(paren
@@ -1660,34 +1660,28 @@
 parent, 
-onTagClickListener
+iconResource
 );%0A     
@@ -1945,32 +1945,213 @@
 pe(position)) %7B%0A
+            case VIEW_TYPE_TAGS:%0A                RulesViewHolder rulesViewHolder = ((RulesViewHolder)holder);%0A                rulesViewHolder.bind(rulesets);%0A                break;%0A
             case
@@ -2433,198 +2433,41 @@
 tem(
-p
+getP
 osition
-));%0A                break;%0A            case VIEW_TYPE_TAGS:%0A                RulesViewHolder rulesViewHolder = ((RulesViewHolder)holder);%0A                rulesViewHolder.bind(rulesets
+FromMessage(position))
 );%0A 
@@ -2530,68 +2530,8 @@
 ) %7B%0A
-        position = hasRulesets() ? position - 1 : position;%0A
     
@@ -2827,32 +2827,119 @@
 pe(position)) %7B%0A
+            case VIEW_TYPE_TAGS:%0A                return rulesets.getUuid().hashCode();%0A
             case
@@ -3010,16 +3010,39 @@
 getItem(
+getPositionFromMessage(
 position
@@ -3034,32 +3034,33 @@
 essage(position)
+)
 ;%0A              
@@ -3138,95 +3138,8 @@
  0;%0A
-            case VIEW_TYPE_TAGS:%0A                return rulesets.getUuid().hashCode();%0A
     
@@ -3692,34 +3692,26 @@
 l : 
-getItem(getFirstPosition()
+chatMessages.get(0
 );%0A 
@@ -3735,30 +3735,48 @@
  int get
-FirstP
+PositionFromMessage(int p
 osition
-(
 ) %7B%0A    
@@ -3806,13 +3806,31 @@
 ) ? 
-1 : 0
+position - 1 : position
 ;%0A  
@@ -4195,55 +4195,8 @@
 e %7B%0A
-            int position = getFirstPosition();%0A
     
@@ -4220,24 +4220,17 @@
 ges.add(
-position
+0
 , messag
@@ -4264,24 +4264,37 @@
 nserted(
-position
+hasRulesets() ? 1 : 0
 );%0A     

Fix messages position when rulesets are enabled