package org.arkiwi.app.utility;

import java.util.Date;
import java.util.Iterator;

import org.arkiwi.app.database.Db;
import org.arkiwi.app.database.DbFeed;
import org.arkiwi.app.feed.FeedQuery;
import org.arkiwi.app.interfaces.Define;

import android.content.Context;
import android.os.Vibrator;
import android.util.Log;

public class FeedBurn implements Define {
	private final static String LOG_TAG = "FeedBurnUtility";
	public final static int NO_NEWS = 0;
	public final static int NEWS = 1;
	
	public static void vibratorNotification(Context c, int type) {
		Vibrator v = (Vibrator) c.getSystemService(Context.VIBRATOR_SERVICE);
		
		int dot = 200;      // Length of a Morse Code "dot" in milliseconds
		int dash = 500;     // Length of a Morse Code "dash" in milliseconds
		int short_gap = 200;    // Length of Gap Between dots/dashes
		//int medium_gap = 500;   // Length of Gap Between Letters
		int long_gap = 1000;    // Length of Gap Between Words
		
		long[] pattern = {
				dot,
				long_gap
		};
		
		long[] news = {
		    0,  // Start immediately
		    dot, short_gap, dot, short_gap, dot,    // s
		    long_gap
		};
		
		long[] no_news = {
			    0,  // Start immediately
			    dash, short_gap, dot,    // n
			    long_gap
			};
		
		switch (type) {
		case NO_NEWS:
			pattern=no_news;
			break;
		case NEWS:
			pattern=news;
			break;
		}
		
		v.vibrate(pattern, -1);
	}
	
	public static boolean refresh(FeedQuery nFeed, FeedQuery oFeed, DbFeed db,
			int[] changed) {

		boolean isChanged = false;
		// verifico se ci sono state delle aggiunte o delle modifiche
		if (!nFeed.getLast().equalsIgnoreCase(oFeed.getLast())) {
			Log.d(LOG_TAG, "aggiornamenti.. nFeed last:" + nFeed.getLast()+ " oFeed:" + oFeed.getLast());
			isChanged = true;

			// ottengo i dati relativi ai feed
			db.fillItem(oFeed, oFeed.getQid(), Db.I_PDATE + " DESC");

			Date oLast = SMethod.stringToDate(A_DATE_FEED, oFeed.getLast());
			// verifico quali sono i nuovi feed
			Iterator<String> iterator = nFeed.getFeedItems().keySet().iterator();
			while (iterator.hasNext()) {
				String key = (String) iterator.next();
				Date newItemPubDate = SMethod.stringToDate(A_DATE_ITEM,nFeed.getFeedItem(key).getPubDate());
				// x i nuovi item faccio modifiche solo se la loro puddate è dopo l'ultima 
				//data dei vecchi item, altrimenti significa che sono vecchi elementi
				if (newItemPubDate.after(oLast)) {
					Log.d(LOG_TAG, "tit:" + nFeed.getFeedItem(key).getTitle() + " pdate" + newItemPubDate);
					// verifico che il suo guid sia originale se non lo è,
					// cancello il feed che corrisponde al guid
					int oStatus = Db.STATUS_NEW;
					if (oFeed.getFeedItems().containsKey(key)) {
						Log.d(LOG_TAG, "modificato");
						oStatus = oFeed.getFeedItem(key).getStatus();
						db.deleteItem(oFeed.getFeedItem(key));
						Log.d(LOG_TAG, "tit:" + oFeed.getFeedItem(key) + " qid:" + oFeed.getQid());
						changed[Db.STATUS_MOD]++;
					} else {
						Log.d(LOG_TAG, "nuovo");
						changed[Db.STATUS_NEW]++;
					}

					db.addItem(nFeed.getFeedItem(key), oFeed.getQid(), oStatus);
				}
			}

		}

		// verifico se ci sono state delle cancellazioni
		Iterator<String> iterator = oFeed.getFeedItems().keySet().iterator();
		while (iterator.hasNext()) {
			String key = (String) iterator.next();
			if (!nFeed.getFeedItems().containsKey(key)) {
				Log.d(LOG_TAG, "cancellato");
				isChanged = true;
				db.deleteItem(oFeed.getFeedItem(key));
				changed[Db.STATUS_DELETED]++;
			}
		}

		db.updateAttrQuery(Db.Q_REFRESHED, nFeed.getRefresh(), oFeed.getQid());

		return isChanged;
	}
}
