package com.gh.common.util;

import android.content.Context;
import android.net.wifi.WifiManager;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import java.util.HashMap;
import java.util.Map;

public class DeviceUtils {

	public synchronized static String getDeviceHeader(Context context) {
		StringBuffer buffer = new StringBuffer();
		String imei = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
		if (!TextUtils.isEmpty(imei)) {
			buffer.append("imei=");
			buffer.append(imei);
		}
		String android_id = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);
		if (!TextUtils.isEmpty(android_id)) {
			if (buffer.length() != 0) {
				buffer.append(",");
			}
			buffer.append("android_id=");
			buffer.append(android_id);
		}
		WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
		String mac = wm.getConnectionInfo().getMacAddress();
		if (!TextUtils.isEmpty(mac)) {
			if (buffer.length() != 0) {
				buffer.append(",");
			}
			buffer.append("mac=");
			buffer.append(mac);
		}
		return buffer.toString();
	}

	public synchronized static Map<String, String> getDeviceParams(Context context) {
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("imei", ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId());
		params.put("android_id", Secure.getString(context.getContentResolver(), Secure.ANDROID_ID));
		WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
		params.put("mac", wm.getConnectionInfo().getMacAddress());
		return params;
	}
	
	public synchronized static String getDeviceID(Context context) {
		String imei = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
		if (!TextUtils.isEmpty(imei)) {
			return imei;
		}
		String android_id = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);
		if (!TextUtils.isEmpty(android_id)) {
			return android_id;
		}
		return Installation.getUUID(context);
	}

}
