package com.backfire.doudizhu.userinfo;

import java.util.ArrayList;

import net.yeeyo.doudizhu.feiyu.R;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.wifi.WifiManager;
import android.provider.Settings.Secure;

import com.backfire.doudizhu.common.DdzApplication;
import com.backfire.doudizhu.common.DeviceUtil;
import com.backfire.utils.Installation;
import com.backfire.utils.Log;
//import com.tencent.xinge.XPush;

public class UserMessageCenter {
	public static String TAG = UserMessageCenter.class.getName();
	private static final String moneyRegex = "(\\d)(?=(\\d{3})+$)";

	public static int thirdAPPId = 1001;
	public static String appsdkID = "100";
	public static int payType = 1000;// 无敌斗牛upay支付

	public int appId = 0;
	public String version = "";
	public String versionName = "";
	public boolean firstLogin = true;
	public String phoneSn = "";
	public String genSn = "";
	public String model = "";

	private long OnlineNum = 0;
	private String notifyMsg = "";
	private String exerMsg = "";
	private String loginAward = "";
	private int loginTimes = 0;
	private boolean islogin = true;

	private String tipMsg = "";

	public String android_id = "";
	public String mac_id = "";
	public String bluetooth_id = "";
	private int platFormID = 0;

	public boolean needUpgrade = false;
	public String upgradeMag = null;

	public boolean isGoodBuying = false;
	// 单例对象
	private static UserMessageCenter dataManagerCenter = null;

	private UserInfo userinfo;
	static ApplicationInfo appInfo;

	private UserMessageCenter() {
		// 私有，防止外部调用
		// ApplicationInfo appInfo;
		try {
			appInfo = DdzApplication
					.getApplication()
					.getPackageManager()
					.getApplicationInfo(
							DdzApplication.getApplication().getPackageName(),
							PackageManager.GET_META_DATA);
			appId = appInfo.metaData.getInt("UMENG_CHANNEL");
			thirdAPPId = appInfo.metaData.getInt("THIRD_APP_ID");
			platFormID = appInfo.metaData.getInt("PLATFORM_ID");
			appsdkID = Integer.toString(appInfo.metaData.getInt("SDK_APP_ID"));
			payType = appInfo.metaData.getInt("PAY_TYPE");

			version = String
					.valueOf(DdzApplication
							.getApplication()
							.getPackageManager()
							.getPackageInfo(
									DdzApplication.getApplication()
											.getPackageName(), 0).versionCode);
			versionName = DdzApplication
					.getApplication()
					.getPackageManager()
					.getPackageInfo(
							DdzApplication.getApplication().getPackageName(), 0).versionName;
		} catch (Exception e) {
			Log.e(this, e);
		}
		phoneSn = DeviceUtil.getDeviceID();
		if (phoneSn == null)
			phoneSn = "";
		model = android.os.Build.MODEL;
		try {
			android_id = Secure.getString(DdzApplication.getApplication()
					.getContentResolver(), Secure.ANDROID_ID);
			if (android_id == null)
				android_id = "";
		} catch (Exception e) {
			Log.e(this, e);
		}
		try {
			WifiManager wm = (WifiManager) DdzApplication.getApplication()
					.getSystemService(Context.WIFI_SERVICE);
			mac_id = wm.getConnectionInfo().getMacAddress();
			if (mac_id == null)
				mac_id = "";
			else {
				mac_id = mac_id.replace(":", "");
			}
		} catch (Exception e) {
			Log.e(this, e);
		}
		try {
			BluetoothAdapter m_BluetoothAdapter = null; // Local Bluetooth
														// adapter
			m_BluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
			bluetooth_id = m_BluetoothAdapter.getAddress();
			if (bluetooth_id == null)
				bluetooth_id = "";
		} catch (Exception e) {
			Log.e(this, e);
		}
		if ((phoneSn == null || phoneSn.trim().length() == 0)) {
			if (mac_id == null || mac_id.trim().length() == 0) {
				Installation ins = new Installation();
				genSn = ins.getClientID(DdzApplication.getApplication());
			} else {
				genSn = "0_" + mac_id;
			}
		} else {
			if (mac_id == null || mac_id.trim().length() == 0) {
				genSn = phoneSn + "_0";
			} else {
				genSn = phoneSn + "_" + mac_id;
			}
		}
	}

	public static UserMessageCenter getInstance() {
		if (dataManagerCenter == null) {
			dataManagerCenter = new UserMessageCenter();
		}
		return dataManagerCenter;
	}

	public UserInfo getUserInfo() {
		if (userinfo == null) {
			userinfo = new UserInfo();
		}
		return userinfo;
	}

	public String getStringText() {
		String text = "appId:" + appId + "\n" + "version:" + version + "\n"
				+ "genSn:" + genSn + "\n" + "model:" + model + "\n"
				+ "android_id:" + android_id + "\n" + "mac_id:" + mac_id + "\n"
				+ "bluetooth_id:" + bluetooth_id + "\n";
		if (userinfo != null) {
			text += userinfo.toString();
		}
		return text;
	}

	public void clear() {
		if (userinfo != null) {
			userinfo.clear();
			userinfo = null;
		}
		appId = 0;
		version = null;
		versionName = null;
		firstLogin = true;
		phoneSn = null;
		genSn = null;
		model = null;
		tipMsg = null;

		OnlineNum = 0;
		notifyMsg = null;
		exerMsg = null;
		loginAward = null;
		loginTimes = 0;
		islogin = false;
		isGoodBuying = false;
	}

	public String getNotifyMsg() {
		return notifyMsg;
	}

	public void setNotifyMsg(String notifyMsg) {
		this.notifyMsg = notifyMsg;
	}

	public String getExerMsg() {
		return exerMsg;
	}

	public void setExerMsg(String exerMsg) {
		this.exerMsg = exerMsg;
	}

	public String getLoginAward() {
		return loginAward;
	}

	public void setLoginAward(String loginAward) {
		this.loginAward = loginAward;
	}

	public int getLoginTimes() {
		if (loginTimes > 4)
			loginTimes = 4;
		return loginTimes;
	}

	public void setLoginTimes(int loginTimes) {
		this.loginTimes = loginTimes;
	}

	public boolean isIslogin() {
		return islogin;
	}

	public void setIslogin(boolean islogin) {
		this.islogin = islogin;
	}

	public long getOnlineNum() {
		return OnlineNum;
	}

	public void setOnlineNum(long onlineNum) {
		OnlineNum = onlineNum;
	}

	public static void destory() {
		dataManagerCenter = null;
	}

	public String getTipMsg() {
		return tipMsg;
	}

	public void setTipMsg(String tipMsg) {
		this.tipMsg = tipMsg;
	}

	public int getPlatFormID() {
		return platFormID;
	}

	public static int getUserIcon(String iconstr) {
		int id = R.drawable.user_icon_1;
		int icon = 0;
		try {
			icon = Integer.parseInt(iconstr);
		} catch (NumberFormatException e) {
			// e.printStackTrace();
		}
		switch (icon) {
		case 1:
			id = R.drawable.user_icon_1;
			break;
		case 2:
			id = R.drawable.user_icon_2;
			break;
		case 3:
			id = R.drawable.user_icon_3;
			break;
		case 4:
			id = R.drawable.user_icon_4;
			break;
		case 5:
			id = R.drawable.user_icon_5;
			break;
		case 6:
			id = R.drawable.user_icon_6;
			break;
		case 7:
			id = R.drawable.user_icon_7;
			break;
		case 8:
			id = R.drawable.user_icon_8;
			break;
		case 9:
			id = R.drawable.user_icon_9;
			break;
		case 10:
			id = R.drawable.user_icon_10;
			break;
		case 11:
			id = R.drawable.user_icon_11;
			break;
		case 12:
			id = R.drawable.user_icon_12;
			break;
		case 13:
			id = R.drawable.user_icon_13;
			break;
		case 14:
			id = R.drawable.user_icon_14;
			break;
		case 15:
			id = R.drawable.user_icon_15;
			break;
		case 16:
			id = R.drawable.user_icon_16;
			break;
		case 17:
			id = R.drawable.user_icon_17;
			break;
		case 18:
			id = R.drawable.user_icon_18;
			break;
		case 19:
			id = R.drawable.user_icon_19;
			break;
		case 20:
			id = R.drawable.user_icon_20;
			break;
		case 21:
			id = R.drawable.user_icon_21;
			break;
		}

		return id;
	}

	public static String getRoomMoney(long money) {
		float f = 0f;

		if (money < 100000l) {
			return money + "";
		} else if (money < 10000000000l) {
			return money / 1000l + "K";
		} else {
			return money / 1000000l + "M";
		}
	}

	public static ArrayList<Integer> getMoneyList(long money) {
		ArrayList<Integer> list = new ArrayList<Integer>();
		int temp = 0;
		if (money >= 1000000l) {
			temp = (int) (money / 1000000);
			list.add(temp);
			money = money % 1000000;
		}
		if (money >= 100000l) {
			temp = (int) (money / 100000);
			list.add(temp);
			money = money % 100000;
		} else {
			if (list.size() > 0) {
				list.add(0);
			}
		}
		if (money >= 10000l) {
			temp = (int) (money / 10000);
			list.add(temp);
			money = money % 10000;
		} else {
			if (list.size() > 0) {
				list.add(0);
			}
		}
		if (money >= 1000l) {
			temp = (int) (money / 1000);
			list.add(temp);
			money = money % 1000;
		} else {
			if (list.size() > 0) {
				list.add(0);
			}
		}
		if (money >= 100l) {
			temp = (int) (money / 100);
			list.add(temp);
			money = money % 100;
		} else {
			if (list.size() > 0) {
				list.add(0);
			}
		}
		if (money >= 10l) {
			temp = (int) (money / 10);
			list.add(temp);
			money = money % 10;
		} else {
			if (list.size() > 0) {
				list.add(0);
			}
		}
		list.add((int) money);
		return list;
	}

	public static String getMoney(long money) {
		String moneyStr;
		long kk = 0;
		if (money >= 100000) {
			kk = money / 1000;
		}
		if (kk > 0) {
			String temp = ("" + kk).trim();
			moneyStr = temp.replaceAll(moneyRegex, "$1,") + "k"; //
			// 转换后的结果如下：1234000-->1，234K
		} else {
			String temp = ("" + money).trim();
			moneyStr = temp.replaceAll(moneyRegex, "$1,"); //
			// 转换后的结果如下：1234-->1，234
		}
		return moneyStr;
	}

	public static String getLobbyMoney(long money) {
		String temp = ("" + money).trim();
		String moneyStr = temp.replaceAll(moneyRegex, "$1,"); //
		// 转换后的结果如下：1234-->1，234
		return moneyStr;
	}

	public static String getItemMoney(long money) {
		String temp = ("" + money).trim();
		String moneyStr = temp.replaceAll(moneyRegex, "$1,"); //
		return moneyStr;
	}

	public static void initXGpush(Activity activity) {
		/*try {
			XPush.start(activity.getApplication());

			XPush.setTag(
					activity,
					"版本",
					"版本是"
							+ String.valueOf(DdzApplication
									.getApplication()
									.getPackageManager()
									.getPackageInfo(
											DdzApplication.getApplication()
													.getPackageName(), 0).versionCode));

			XPush.setTag(
					activity,
					"包名",
					"包名是"
							+ String.valueOf(DdzApplication
									.getApplication()
									.getPackageManager()
									.getPackageInfo(
											DdzApplication.getApplication()
													.getPackageName(), 0).packageName));

			appInfo = DdzApplication
					.getApplication()
					.getPackageManager()
					.getApplicationInfo(
							DdzApplication.getApplication().getPackageName(),
							PackageManager.GET_META_DATA);
			XPush.setTag(activity, "渠道",
					"渠道是" + appInfo.metaData.getInt("UMENG_CHANNEL") + "");
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
	}
}
