package akechi.projectl.component;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.api.client.util.DateTime;
import com.google.common.collect.Lists;

import java.text.DateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import akechi.projectl.AppContext;
import akechi.projectl.R;
import jp.michikusa.chitose.lingr.Room;

public class MessageAdapter
    extends BaseAdapter
{
    public MessageAdapter(Context context, List<? extends Room.Message> messages)
    {
        this.context = context;
        this.messages.addAll(messages);
    }

    public void insertHead(Collection<? extends Room.Message> messages)
    {
        this.messages.addAll(0, messages);
    }

    public void add(Room.Message e)
    {
        this.messages.add(e);
    }

    public void clear()
    {
        this.messages.clear();
    }

    @Override
    public int getCount()
    {
        return this.messages.size();
    }

    @Override
    public Room.Message getItem(int position)
    {
        return this.messages.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        final View view;
        if(convertView == null)
        {
            final LayoutInflater inflater= (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view= inflater.inflate(R.layout.custom_message, null);
        }
        else
        {
            view= convertView;
        }

        final Room.Message data= this.getItem(position);
        final ImageView iconView= (ImageView)view.findViewById(R.id.iconView);
        iconView.setImageURI(Uri.parse(data.getIconUrl()));

        final TextView nicknameView= (TextView)view.findViewById(R.id.nicknameView);
        nicknameView.setText(data.getNickname());

        final TextView timestampView= (TextView)view.findViewById(R.id.timestampView);
        {
            final Date timestamp= new Date(new DateTime(data.getTimestamp()).getValue());
            timestampView.setText(DateFormat.getDateTimeInstance().format(timestamp));
        }

        final TextView textView= (TextView)view.findViewById(R.id.textView);
        textView.setText(data.getText());

        final AppContext appContext= (AppContext)this.context.getApplicationContext();
        appContext.getInlineImageMode().doWork(textView);

        return view;
    }

    private final Context context;

    private final List<Room.Message> messages= Lists.newLinkedList();
}


@@ -368,19 +368,104 @@
 collect.
-Lis
+Iterables;%0D%0Aimport com.google.common.collect.Lists;%0D%0Aimport com.google.common.collect.Se
 ts;%0D%0A%0D%0Ai
@@ -523,16 +523,46 @@
 ction;%0D%0A
+import java.util.Comparator;%0D%0A
 import j
@@ -600,16 +600,45 @@
 l.List;%0D
+%0Aimport java.util.SortedSet;%0D
 %0A%0D%0Aimpor
@@ -1081,11 +1081,8 @@
 All(
-0, 
 mess
@@ -1427,32 +1427,46 @@
 %0A        return 
+Iterables.get(
 this.messages.ge
@@ -1462,21 +1462,18 @@
 messages
-.get(
+, 
 position
@@ -3013,19 +3013,24 @@
 e final 
-Lis
+SortedSe
 t%3CRoom.M
@@ -3051,28 +3051,223 @@
 es= 
-Lis
+Se
 ts.new
-LinkedList(
+TreeSet(new Comparator%3CRoom.Message%3E()%7B%0D%0A        @Override%0D%0A        public int compare(Room.Message lhs, Room.Message rhs)%0D%0A        %7B%0D%0A            return lhs.getId().compareTo(rhs.getId());%0D%0A        %7D%0D%0A    %7D
 );%0D%0A

Fix duplicate messages