package com.irccloud.android;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;

public class EventsDataSource {

	public class comparator implements Comparator<IRCCloudJSONObject> {
		public int compare(IRCCloudJSONObject e1, IRCCloudJSONObject e2) {
			long l1 = e1.eid(), l2 = e2.eid();
			if(l1 == l2)
				return 0;
			else if(l1 > l2)
				return 1;
			else return -1;
		}
	}
	
	private HashMap<Integer,HashMap<Long, IRCCloudJSONObject>> events;
	
	private static EventsDataSource instance = null;
	
	public static EventsDataSource getInstance() {
		if(instance == null)
			instance = new EventsDataSource();
		return instance;
	}

	public EventsDataSource() {
		events = new HashMap<Integer,HashMap<Long, IRCCloudJSONObject>>();
	}

	public void clear() {
		synchronized(events) {
			events.clear();
		}
	}
	
	public void addEvent(IRCCloudJSONObject event) {
		synchronized(events) {
			if(!events.containsKey(event.bid()))
				events.put(event.bid(), new HashMap<Long,IRCCloudJSONObject>());
			events.get(event.bid()).put(event.eid(), event);
		}
	}

	public IRCCloudJSONObject getEvent(long eid, int bid) {
		synchronized(events) {
			if(events.containsKey(bid))
				return events.get(bid).get(eid);
		}
		return null;
	}
	
	public void deleteEvent(long eid, int bid) {
		synchronized(events) {
			if(events.containsKey(bid) && events.get(bid).containsKey(eid))
				events.get(bid).remove(eid);
		}
	}

	public void deleteEventsForBuffer(int bid) {
		synchronized(events) {
			if(events.containsKey(bid))
				events.remove(bid);
		}
	}

	public ArrayList<IRCCloudJSONObject> getEventsForBuffer(int bid) {
		ArrayList<IRCCloudJSONObject> list = new ArrayList<IRCCloudJSONObject>();
		synchronized(events) {
			if(events.containsKey(bid)) {
				Iterator<IRCCloudJSONObject> i = events.get(bid).values().iterator();
				while(i.hasNext()) {
					list.add(i.next());
				}
				Collections.sort(list, new comparator());
			}
		}
		return list;
	}

	public int getUnreadCountForBuffer(int bid, long last_seen_eid) {
		int count = 0;
		synchronized(events) {
			if(events.containsKey(bid)) {
				Iterator<IRCCloudJSONObject> i = events.get(bid).values().iterator();
				while(i.hasNext()) {
					IRCCloudJSONObject e = i.next();
					String type = e.type();
					if(e.eid() > last_seen_eid && (type.equals("buffer_msg") || type.equals("buffer_me_msg") || type.equals("notice")))
						count++;
				}
			}
		}
		return count;
	}

	public synchronized int getHighlightCountForBuffer(int bid, long last_seen_eid) {
		int count = 0;
		synchronized(events) {
			if(events.containsKey(bid)) {
				Iterator<IRCCloudJSONObject> i = events.get(bid).values().iterator();
				while(i.hasNext()) {
					IRCCloudJSONObject e = i.next();
					if(e.eid() > last_seen_eid && e.highlight())
						count++;
				}
			}
		}
		return count;
	}
}

@@ -35,26 +35,38 @@
 ort 
-java.util.ArrayLis
+android.annotation.SuppressLin
 t;%0Ai
@@ -81,27 +81,25 @@
 va.util.
-Collections
+ArrayList
 ;%0Aimport
@@ -174,16 +174,75 @@
 erator;%0A
+import java.util.TreeMap;%0A%0A@SuppressLint(%22UseSparseArrays%22)
 %0Apublic 
@@ -552,36 +552,36 @@
 HashMap%3CInteger,
-Hash
+Tree
 Map%3CLong, IRCClo
@@ -849,20 +849,20 @@
 Integer,
-Hash
+Tree
 Map%3CLong
@@ -1110,28 +1110,28 @@
 .bid(), new 
-Hash
+Tree
 Map%3CLong,IRC
@@ -2034,16 +2034,18 @@
 %09%09%7D%0A%09%09%09%09
+//
 Collecti

Use a TreeMap instead of a HashMap so the events stay in order