package com.usai.apex;

import com.usai.util.Crypto;
import com.usai.util.dbUtil;

import android.app.AlarmManager;
import android.app.Application;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.SystemClock;
import android.text.TextUtils;
import android.util.Log;

public class ApexTrackingApplication extends Application
{
	static final String						TAG				= "ApexTrackingApplication";
	private static ApexTrackingApplication	instance;
	private static String					m_sessionid		= "";
	private static String					m_user			= "";
	private static String					m_password		= "";
	public static int						ncount			= 1;

	private static boolean					m_bauthorized	= false;

	private static long LastAlermTime = -120000;
	
	public static void UpdateLastCheckMessageTime()
	{
		LastAlermTime = SystemClock.elapsedRealtime();
	}
	public static long getLastCheckMessageTime()
	{
		return LastAlermTime;
	}
	@Override
	public void onCreate()
	{

		// TODO Auto-generated method stub
		Log.d(TAG, "onCreate");
		super.onCreate();
		instance = this;

		SharedPreferences pref = ApexTrackingApplication.get_instance()
				.getSharedPreferences("Apex", 0);
		boolean autologin = pref.getBoolean("autologin", false);
		if (autologin == true)
		{

			String u = pref.getString("user", "");
			String p = pref.getString("password", "");
			String s = pref.getString("sessionid", "");

			try
			{
				if (!TextUtils.isEmpty(u))
					m_user = Crypto.decrypt("apexu", u);
				if (!TextUtils.isEmpty(p))
					m_password = Crypto.decrypt("apexp", p);
				if (!TextUtils.isEmpty(s))
					m_sessionid = Crypto.decrypt("apexp", s);
			}
			catch (Exception e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		dbUtil.cleanhistory();

		// Intent serviceintent = new Intent();
		// serviceintent.setClass(this, ApexPushService.class);
		//
		//
		// startService(serviceintent);
	}

	// @Override
	// protected void finalize() throws Throwable
	// {
	// NotificationManager nManager = (NotificationManager)
	// instance.getSystemService(
	// Context.NOTIFICATION_SERVICE);
	// nManager.cancel(R.layout.activity_apex);
	// super.finalize();
	// }

	public static ApexTrackingApplication get_instance()
	{
		return instance;
	}

	public static void startalarm(String caller)
	{
		SharedPreferences pref = get_instance()
				.getSharedPreferences("Apex", 0);
		boolean autologin = pref.getBoolean("autologin", false);
		if (autologin == false)
			return;
		// 启动完成
		Intent iAlarm = new Intent(get_instance(), Alarmreceiver.class);
		iAlarm.putExtra("caller", caller);
		iAlarm.setAction("com.usai.apex.push");
		PendingIntent sender = PendingIntent.getBroadcast(get_instance(), 0,
				iAlarm, PendingIntent.FLAG_UPDATE_CURRENT);

		long firstime = SystemClock.elapsedRealtime();
//		UpdateLastAlermTime();
		AlarmManager am = (AlarmManager) get_instance().getSystemService(
				Context.ALARM_SERVICE);

		// 5分钟一个周期，不停的发送广播
		am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstime,
				300 * 1000, sender);
	}

	public static void cancelalarm()
	{

		// 启动完成
		Intent iAlarm = new Intent(get_instance(), Alarmreceiver.class);
		iAlarm.setAction("com.usai.apex.push");
		PendingIntent sender = PendingIntent.getBroadcast(get_instance(), 0,
				iAlarm, PendingIntent.FLAG_UPDATE_CURRENT);

		AlarmManager am = (AlarmManager) get_instance().getSystemService(
				Context.ALARM_SERVICE);

		am.cancel(sender);
	}

	public static void logout()
	{
		ncount = 1;
		cancelalarm();
		m_sessionid = "";
		m_user = "";
		m_password = "";
		m_bauthorized = false;
		SharedPreferences pref = ApexTrackingApplication.get_instance()
				.getSharedPreferences("Apex", 0);
		SharedPreferences.Editor editor = pref.edit();

		editor.putBoolean("autologin", false);
		editor.commit();

	}

	public static void SetAuthorizeStatus(boolean bauthorized)
	{
		m_bauthorized = bauthorized;
	}
	public static void login(String sid, String user, String pass)
	{
		m_sessionid = sid;
		m_user = user;
		m_password = pass;
		m_bauthorized = true;
		
		SharedPreferences pref = ApexTrackingApplication.get_instance()
				.getSharedPreferences("Apex", 0);
		SharedPreferences.Editor editor = pref.edit();
		try
		{

			editor.putString("user", Crypto.encrypt("apexu", m_user));
			editor.putString("password", Crypto.encrypt("apexp", m_password));
			editor.putBoolean("autologin", true);
			editor.putString("sessionid", Crypto.encrypt("apexp", m_sessionid));

		}
		catch (Exception e)
		{
			editor.putString("user", null);
			editor.putString("password", null);
			editor.putString("m_sessionid", null);
			editor.putBoolean("autologin", false);
			m_bauthorized=false;
			e.printStackTrace();
		}
		editor.commit();

		startalarm("app Login");
	}

	// public static void put_sessionid(String id)
	// {
	//
	// Log.e(TAG, "put_sessionid");
	// m_sessionid = id;
	// }

	// public static void put_authorization(boolean bauth)
	// {
	//
	// m_bauthorized = bauth;
	// }

	public static boolean get_authorization()
	{

		return m_bauthorized;
	}

	// public static void put_user(String user)
	// {
	//
	// m_user = user;
	// }
	//
	// public static void put_password(String pass)
	// {
	//
	// m_password = pass;
	// }

	public static String get_sessionid()
	{
		Log.d(TAG, "get_sessionid");
		return m_sessionid;
	}

	public static String get_user()
	{
		Log.d(TAG, "get_user");
		return m_user;
	}

	public static String get_pass()
	{
		Log.d(TAG, "get_pass");
		return m_password;
	}
}
