package com.backfire.doudizhu.common;

import android.content.Context;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;

import com.backfire.utils.Log;

public class APNUtil {
	private static final String TAG = "APNUtil";
	
	public static final int MPROXYTYPE_WIFI = 1;
	
	public static final int MPROXYTYPE_CMWAP = 2;
	
	public static final int MPROXYTYPE_CMNET = 4;
	
	public static final int MPROXYTYPE_UNINET = 8;
	
	public static final int MPROXYTYPE_UNIWAP = 16;
	
	public static final int MPROXYTYPE_NET = 32;
	
	public static final int MPROXYTYPE_WAP = 64;
	
	public static final int MPROXYTYPE_DEFAULT = 128;
	
	public static final int MPROXYTYPE_CTNET = 256;
	
	public static final int MPROXYTYPE_CTWAP = 512;

	public static final String ANP_NAME_WIFI = "wifi"; 
	public static final String ANP_NAME_CMWAP = "cmwap"; 
	public static final String ANP_NAME_CMNET = "cmnet"; 
	public static final String ANP_NAME_UNIWAP = "uniwap";
	public static final String ANP_NAME_UNINET = "uninet"; 
	public static final String ANP_NAME_WAP = "wap"; 
	public static final String ANP_NAME_NET = "net"; 
	public static final String ANP_NAME_CTWAP = "ctwap"; 
	public static final String ANP_NAME_CTNET = "ctnet"; 
	public static final String ANP_NAME_NONE = "none"; 

	private static Uri PREFERRED_APN_URI = Uri
			.parse("content://telephony/carriers/preferapn");

	public static final String APN_PROP_APN = "apn";
	public static final String APN_PROP_PROXY = "proxy";
	public static final String APN_PROP_PORT = "port";



	public static String getApnName(Context context) {
		int netType = getMProxyType(context);

		if (netType == MPROXYTYPE_WIFI) {
			return ANP_NAME_WIFI;
		} else if (netType == MPROXYTYPE_CMWAP) {
			return ANP_NAME_CMWAP;
		} else if (netType == MPROXYTYPE_CMNET) {
			return ANP_NAME_CMNET;
		} else if (netType == MPROXYTYPE_UNIWAP) {
			return ANP_NAME_UNIWAP;
		} else if (netType == MPROXYTYPE_UNINET) {
			return ANP_NAME_UNINET;
		} else if (netType == MPROXYTYPE_WAP) {
			return ANP_NAME_WAP;
		} else if (netType == MPROXYTYPE_NET) {
			return ANP_NAME_NET;
		} else if (netType == MPROXYTYPE_CTWAP) {
			return ANP_NAME_CTWAP;
		} else if (netType == MPROXYTYPE_CTNET) {
			return ANP_NAME_CTNET;
		}
		String apn = getApn(context);
		if (apn == null || apn.length() == 0)
			return apn;
		return ANP_NAME_NONE;
	}

	public static boolean IsBroadband(Context context) {
		int netType = getMProxyType(context);
		if (netType == MPROXYTYPE_WIFI) {
			return true;
		} else if (netType == MPROXYTYPE_CMWAP) {
			return false;
		} else if (netType == MPROXYTYPE_CMNET) {
			return false;
		} else if (netType == MPROXYTYPE_UNIWAP) {
			return false;
		} else if (netType == MPROXYTYPE_UNINET) {
			return false;
		} else if (netType == MPROXYTYPE_WAP) {
			return false;
		} else if (netType == MPROXYTYPE_NET) {
			return false;
		} else if (netType == MPROXYTYPE_CTWAP) {
			return false;
		} else if (netType == MPROXYTYPE_CTNET) {
			return false;
		} else {
			return true;
		}
	}
	public static String getApn(Context context) {
		Cursor c = context.getContentResolver().query(PREFERRED_APN_URI, null,
				null, null, null);
		c.moveToFirst();
		if (c.isAfterLast()) {
			return null;
		}
		return c.getString(c.getColumnIndex(APN_PROP_APN));
	}
	public static String getApnProxy(Context context) {
		Cursor c = context.getContentResolver().query(PREFERRED_APN_URI, null,
				null, null, null);
		c.moveToFirst();
		if (c.isAfterLast()) {
			return null;
		}
		return c.getString(c.getColumnIndex(APN_PROP_PROXY));
	}
	public static String getApnPort(Context context) {
		Cursor c = context.getContentResolver().query(PREFERRED_APN_URI, null,
				null, null, null);
		c.moveToFirst();
		if (c.isAfterLast()) {
			return null;
		}
		return c.getString(c.getColumnIndex(APN_PROP_PORT));
	}

	public static int getApnPortInt(Context context) {
		Cursor c = context.getContentResolver().query(PREFERRED_APN_URI, null,
				null, null, null);
		c.moveToFirst();
		if (c.isAfterLast()) {
			return -1;
		}
		return c.getInt(c.getColumnIndex(APN_PROP_PORT));
	}
	public static boolean hasProxy(Context context) {
		int netType = getMProxyType(context);
		// #if ${polish.debug}
		Log.d(TAG, "netType:" + netType);
		// #endif
		if (netType == MPROXYTYPE_CMWAP || netType == MPROXYTYPE_UNIWAP
				|| netType == MPROXYTYPE_WAP || netType == MPROXYTYPE_CTWAP) {
			return true;
		}
		return false;
	}
	public static int getMProxyType(Context act) {

		int type = MPROXYTYPE_DEFAULT;
		ConnectivityManager cm = (ConnectivityManager) act
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = cm.getActiveNetworkInfo();
		if (null != info) {
			String typeName = info.getTypeName();
			String extraInfo = info.getExtraInfo();
			if (null == extraInfo) {
				extraInfo = "unknown";
			} else {
				extraInfo = extraInfo.toLowerCase();
			}
			if (DdzApplication.getApplication().isDebug()) {
				Log.i("APN", typeName, extraInfo);
			}
			if (typeName.toUpperCase().equals("WIFI")) { 
				type = MPROXYTYPE_WIFI;
			} else {
				if (extraInfo.startsWith("cmwap")) {
					type = MPROXYTYPE_CMWAP;
				} else if (extraInfo.startsWith("cmnet")
						|| extraInfo.startsWith("epc.tmobile.com")) { 
					type = MPROXYTYPE_CMNET;
				} else if (extraInfo.startsWith("uniwap")) {
					type = MPROXYTYPE_UNIWAP;
				} else if (extraInfo.startsWith("uninet")) {
					type = MPROXYTYPE_UNINET;
				} else if (extraInfo.startsWith("wap")) {
					type = MPROXYTYPE_WAP;
				} else if (extraInfo.startsWith("net")) {
					type = MPROXYTYPE_NET;
				} else if (extraInfo.startsWith("#777")) {
					String proxy = getApnProxy(act);
					if (proxy != null && proxy.length() > 0) {
						type = MPROXYTYPE_CTWAP;
					} else {
						type = MPROXYTYPE_CTNET;
					}
				} else if (extraInfo.startsWith("ctwap")) {
					type = MPROXYTYPE_CTWAP;
				} else if (extraInfo.startsWith("ctnet")) {
					type = MPROXYTYPE_CTNET;
				} else {
				}

			}
		}
		return type;
	}

	public static String getNetWorkName(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = cm.getActiveNetworkInfo();
		if (info != null)
			return info.getTypeName();
		else
			return "MOBILE";
	}

	public static boolean isNetworkAvailable(Context act) {
		ConnectivityManager cm = (ConnectivityManager) act
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = cm.getActiveNetworkInfo();
		if (info != null && info.getState() == NetworkInfo.State.CONNECTED)
			return true;
		return false;
	}
	public static boolean isActiveNetworkAvailable(Context ctx) {
		ConnectivityManager cm = (ConnectivityManager) ctx
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = cm.getActiveNetworkInfo();
		if (info != null)
			return info.isAvailable();
		return false;
	}
}