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.base.Objects;
import com.google.common.base.Strings;
import com.google.common.collect.ComparisonChain;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

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

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(messages);
    }

    public void setUnreadMessageId(CharSequence messageId)
    {
        if(messageId == null)
        {
            this.unreadMessageId= null;
        }
        else
        {
            this.unreadMessageId= messageId.toString();
        }
    }

    public Room.Message getLatestMessage()
    {
        return this.messages.last();
    }

    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 Iterables.get(this.messages, 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);
        if(Objects.equal(view.getTag(), data.getId()))
        {
            return view;
        }
        view.setTag(data.getId());
        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);

        final TextView separatorView= (TextView)view.findViewById(R.id.separatorTextView);
        if(Strings.isNullOrEmpty(this.unreadMessageId))
        {
            separatorView.setVisibility(TextView.GONE);
        }
        else if(this.unreadMessageId.equals(data.getId()))
        {
            separatorView.setText("--- UNREAD MESSAGES FROM HERE ---");
            separatorView.setVisibility(TextView.VISIBLE);
        }
        else
        {
            separatorView.setVisibility(TextView.GONE);
        }

        return view;
    }

    private final Context context;

    private final SortedSet<Room.Message> messages= Sets.newTreeSet(new Comparator<Room.Message>(){
        @Override
        public int compare(Room.Message lhs, Room.Message rhs)
        {
            return ComparisonChain.start()
                .compare(new DateTime(lhs.getTimestamp()).getValue(), new DateTime(rhs.getTimestamp()).getValue())
                .compare(lhs.getId(), rhs.getId())
                .result()
            ;
        }
    });

    private String unreadMessageId;
}

@@ -85,16 +85,41 @@
 et.Uri;%0A
+import android.util.Log;%0A
 import a
@@ -142,24 +142,24 @@
 utInflater;%0A
-
 import andro
@@ -733,16 +733,57 @@
 l.List;%0A
+import java.util.NoSuchElementException;%0A
 import j
@@ -1543,32 +1543,58 @@
 Message()%0A    %7B%0A
+        try%0A        %7B%0A    
         return t
@@ -1610,24 +1610,199 @@
 ges.last();%0A
+        %7D%0A        catch(NoSuchElementException e)%0A        %7B%0A            Log.e(this.getClass().getSimpleName(), %22Suppressing exception%22, e);%0A            return null;%0A        %7D%0A
     %7D%0A%0A    p

Fix bug, sometimes crashes on entering a room