package com.kong.zxreader.common;

import java.lang.reflect.Field;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Configuration;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.kong.zxreader.app.AppConfig;
import com.kong.zxreader.app.AppContext;

public class AppUtils {

	private static AppUtils appUtils = new AppUtils();
	private AppContext appContext;

	private AppUtils() {
	}

	public static AppUtils getAppUtils(AppContext appContext) {
		appUtils.appContext = appContext;
		return appUtils;
	}

	/**
	 * 隐藏软键盘
	 * 
	 * @param mActivity
	 */
	public static void hideSoftInput(Activity mActivity, EditText editText) {
		InputMethodManager imm = (InputMethodManager) mActivity
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
	}

	/**
	 * 显示软键盘
	 * 
	 * @param mActivity
	 */
	public static void showSoftInput(View v) {
		InputMethodManager imm = (InputMethodManager) v.getContext()
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		if (imm.isActive()) { // 这里可以判断也可以不判断
			imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
		}
	}

	/**
	 * 获取App安装包信息
	 * 
	 * @return
	 */
	public PackageInfo getPackageInfo() {
		PackageInfo info = null;
		try {
			info = appContext.getPackageManager().getPackageInfo(
					appContext.getPackageName(), 0);
		} catch (NameNotFoundException e) {
			e.printStackTrace(System.err);
		}
		if (info == null)
			info = new PackageInfo();
		return info;
	}

	/**
	 * 获取App唯一标识
	 * 
	 * @return
	 */
	public String getAppId() {
		String uniqueID = appContext.getProperty(AppConfig.CONF_APP_UNIQUEID);
		if (TextUtils.isEmpty(uniqueID)) {
			uniqueID = new DeviceUuidFactory(appContext).getDeviceUuid()
					.toString();
			appContext.setProperty(AppConfig.CONF_APP_UNIQUEID, uniqueID);
		}
		return uniqueID;
	}

	/**
	 * 获取网络类型 -1:错误 0:无连接 1:wifi 2:3g
	 * 
	 * @return
	 */
	public int getNetworkType() {
		try {
			ConnectivityManager connectivity = (ConnectivityManager) appContext
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			if (connectivity != null) {
				NetworkInfo info = connectivity.getActiveNetworkInfo();
				if (info != null && info.isConnected()) {
					if (info.getState() == NetworkInfo.State.CONNECTED) {
						if (info.getType() == ConnectivityManager.TYPE_WIFI) {
							return Constants.NETWORK_STATE_WIFI;
						} else {
							return Constants.NETWORK_STATE_3G;
						}
					} else {
						return Constants.NETWORK_STATE_NO_CONNECTION;
					}
				} else {
					return Constants.NETWORK_STATE_NO_CONNECTION;
				}
			}
		} catch (Exception e) {
			return Constants.NETWORK_STATE_ERROR;
		}
		return Constants.NETWORK_STATE_ERROR;
	}

	/**
	 * 判断横竖屏
	 * @return
	 */
	public boolean isScreenLandscape(Activity activity) {

		Configuration mConfiguration = activity.getResources().getConfiguration(); // 获取设置的配置信息
		int ori = mConfiguration.orientation; // 获取屏幕方向

		if (ori == mConfiguration.ORIENTATION_LANDSCAPE) {
			// 横屏
			return true;
		} else if (ori == mConfiguration.ORIENTATION_PORTRAIT) {

			// 竖屏
			return false;
		}
		return false;
	}
	
	/**
	 * 判断是否是平板
	 * @param activity
	 * @return
	 */
	public static boolean isTabletDevice(Context activity) {  
        TelephonyManager telephony = (TelephonyManager) activity.getSystemService(Context.TELEPHONY_SERVICE);  
        int type = telephony.getPhoneType();  
        if (type == TelephonyManager.PHONE_TYPE_NONE) {  
            return true;  
        } else {  
            return false;  
        }  
    }  
	
	/**
	 * 获取状态栏高度 
	 * @return
	 */
	public static int getStatusBarHeight(Context context){  
//      Rect rect= new Rect();  
//        
//      Window window= ((Activity) context).getWindow();  
//      window.getDecorView().getWindowVisibleDisplayFrame(rect);  
//      System.out.println(rect.top);  
//      return rect.top;  
          
        Class<?> c = null;   
        Object obj = null;   
        Field field = null;   
        int x = 0, sbar = 0;   
        try{       
            c = Class.forName("com.android.internal.R$dimen");       
            obj = c.newInstance();       
            field = c.getField("status_bar_height");       
            x = Integer.parseInt(field.get(obj).toString());       
            sbar = context.getResources().getDimensionPixelSize(x);   
        } catch(Exception e1) {       
            e1.printStackTrace();   
        }  
        return sbar;  
    }  
}
