package de.stephanlindauer.criticalmaps.adapter;

import android.animation.AnimatorInflater;
import android.animation.ObjectAnimator;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.text.DateFormat;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.stephanlindauer.criticalmaps.R;
import de.stephanlindauer.criticalmaps.interfaces.IChatMessage;
import de.stephanlindauer.criticalmaps.utils.TimeToWordStringConverter;
import de.stephanlindauer.criticalmaps.model.chat.ReceivedChatMessage;

public class ChatMessageAdapter extends RecyclerView.Adapter<ChatMessageAdapter.ChatMessageViewHolder> {

    private List<IChatMessage> chatMessages;

    public static class ChatMessageViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.firstLine)
        TextView labelView;

        @BindView(R.id.secondLine)
        TextView valueView;

        private final DateFormat dateFormatter = DateFormat.getDateTimeInstance(
                DateFormat.DEFAULT, DateFormat.SHORT, Locale.getDefault());
        private final Context context;

        public ChatMessageViewHolder(View itemView) {
            super(itemView);
            context = itemView.getContext();
            ButterKnife.bind(this, itemView);
        }

        public void bind(IChatMessage message) {
            valueView.setText(message.getMessage());
            if (message instanceof ReceivedChatMessage) {
                dateFormatter.setTimeZone(TimeZone.getDefault());
                labelView.setText(TimeToWordStringConverter.getTimeAgo(
                        ((ReceivedChatMessage) message).getTimestamp(), context));
                labelView.clearAnimation();
            } else {
                labelView.setText(R.string.chat_sending);

                ObjectAnimator sendingAnimator = (ObjectAnimator) AnimatorInflater.loadAnimator(
                        itemView.getContext(),
                        R.animator.map_gps_fab_searching_animation);
                sendingAnimator.setTarget(labelView);
                sendingAnimator.start();
            }
        }
    }

    public ChatMessageAdapter(List<IChatMessage> chatMessages) {
        this.chatMessages = chatMessages;
    }

    @NonNull
    @Override
    public ChatMessageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        final View res = inflater.inflate(R.layout.view_chatmessage, parent, false);
        return new ChatMessageViewHolder(res);
    }

    @Override
    public void onBindViewHolder(@NonNull ChatMessageViewHolder holder, int position) {
        holder.bind(chatMessages.get(position));
    }

    @Override
    public int getItemCount() {
        return chatMessages.size();
    }

    public void updateData(List<IChatMessage> savedAndOutgoingMessages) {
        this.chatMessages = savedAndOutgoingMessages;
        notifyDataSetChanged();
    }
}

@@ -131,39 +131,8 @@
 or;%0A
-import android.content.Context;
 %0Aimp
@@ -912,23 +912,16 @@
 s;%0A%0A    
-public 
 static c
@@ -1286,29 +1286,38 @@
 ate 
-final Context context
+ObjectAnimator sendingAnimator
 ;%0A%0A 
@@ -1319,31 +1319,24 @@
 r;%0A%0A        
-public 
 ChatMessageV
@@ -1395,53 +1395,8 @@
 w);%0A
-            context = itemView.getContext();%0A
     
@@ -1452,31 +1452,24 @@
  %7D%0A%0A        
-public 
 void bind(IC
@@ -1815,60 +1815,30 @@
 (), 
-context));%0A                labelView.clearAnimation(
+itemView.getContext())
 );%0A 
@@ -1915,17 +1915,16 @@
 nding);%0A
-%0A
         
@@ -1931,31 +1931,16 @@
         
-ObjectAnimator 
 sendingA
@@ -2047,32 +2047,8 @@
 t(),
-%0A                       
  R.a
@@ -2203,24 +2203,205 @@
  %7D%0A        %7D
+%0A%0A        void clearAnimation() %7B%0A            if (sendingAnimator != null) %7B%0A                sendingAnimator.cancel();%0A                itemView.setAlpha(1f);%0A            %7D%0A        %7D
 %0A    %7D%0A%0A    
@@ -3003,32 +3003,168 @@
 ition));%0A    %7D%0A%0A
+    @Override%0A    public void onViewDetachedFromWindow(@NonNull ChatMessageViewHolder holder) %7B%0A        holder.clearAnimation();%0A    %7D%0A%0A
     @Override%0A  

Fix animation not clearing