package org.arkiwi.app.service;

import org.arkiwi.app.R;
import org.arkiwi.app.utility.SNetwork;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.IBinder;
import android.util.Log;

public class CheckUpdate extends Service {
	private final static String LOG_TAG = "CheckUpdate";
	private final static String aarkiwi_apk = "org.arkiwi.app";
	private final static String aarkiwi_site = "http://app.arkiwi.org/";
	private final static String aarkiwi_www_current_apk = "latest.apk";
	private final static String aarkiwi_www_current_version = "update/current";
	
	public final static int NOTIFY_APP_UPDATE = 2;
	
	private BgThread bgThread;
	private NotificationManager notificationManager;	
	private Notification notification;
	private PendingIntent pIntent;
	private int notificationNumber;
	private Resources res;

	@Override
	public void onCreate() {
		super.onCreate();
		res = getResources();
		
		bgThread = new BgThread();
		bgThread.start();
		
		notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		notification = new Notification(R.drawable.software,
				res.getString(R.string.app_update_notify_title), System.currentTimeMillis());		
		notification.flags |= Notification.FLAG_AUTO_CANCEL;

		Intent intent = new Intent(this, Downloader.class);
		
		intent.putExtra("checkUpdate", NOTIFY_APP_UPDATE);
		String uri = aarkiwi_site + aarkiwi_www_current_apk;
		intent.setData(Uri.parse(uri));
		intent.putExtra("urlpath", uri);
		
		pIntent = PendingIntent.getService(this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT );
		
		Log.i(LOG_TAG, "Service Created");
	}

	public void sendNotification(int newVersion) {
		notification.number=++notificationNumber;
		notification.setLatestEventInfo (this, 
				res.getString(R.string.app_update_notify_title),
				res.getString(R.string.app_update_new_version),
				pIntent);
		
		notificationManager.notify(NOTIFY_APP_UPDATE, notification);
	}	
	
	private final class BgThread extends Thread {
		public void run() {

			try {
				String version = SNetwork.doGet(aarkiwi_site + aarkiwi_www_current_version);
				int curVersion = getPackageManager().getPackageInfo(aarkiwi_apk, 0).versionCode;
				int newVersion = Integer.valueOf(version.trim());
				Log.d(LOG_TAG,"cur:"+curVersion+" new:"+newVersion);

				if (newVersion > curVersion) {
					sendNotification(newVersion);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			stopSelf();
		}
	}
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		Log.i(LOG_TAG, "Service Started");
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		Log.i(LOG_TAG, "Service Destroyed");
	}

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

}
