package com.stm.asset.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.view.Display;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.stm.asset.base.StmApplication;

/**
 * 
 * 类名称：ViewUtil 类描述： 控件工具类 创建人：谢明峰 创建时间：2014-12-23 下午2:22:19
 * 
 * @version
 * 
 */
public class ViewUtil {

	public static int getScreenWidth(Activity activity) {
		Display display = activity.getWindow().getWindowManager().getDefaultDisplay();
		return Math.min(display.getHeight(), display.getWidth());
	}

	public static int getScreenWidth() {
		Display display = ((WindowManager) StmApplication.getInstance().getApplicationContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		return Math.min(display.getHeight(), display.getWidth());
	}

	public static int getScreenHeight() {
		Display display = ((WindowManager) StmApplication.getInstance().getApplicationContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		return Math.max(display.getHeight(), display.getWidth());
	}

	public static int getScreenHeight(Activity activity) {
		Display display = activity.getWindow().getWindowManager().getDefaultDisplay();
		return Math.max(display.getHeight(), display.getWidth());
	}

	public static int getStatusHeight(Context activity) {
		int statusHeight;
		Rect localRect = new Rect();
		((Activity) activity).getWindow().getDecorView().getWindowVisibleDisplayFrame(localRect);
		statusHeight = localRect.top;
		if (0 == statusHeight) {
			Class<?> localClass;
			try {
				localClass = Class.forName("com.android.internal.R$dimen");
				Object localObject = localClass.newInstance();
				int i5 = Integer.parseInt(localClass.getField("status_bar_height").get(localObject).toString());
				statusHeight = activity.getResources().getDimensionPixelSize(i5);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return statusHeight;
	}

	/**
	 * view转bitmap
	 */
	public static Bitmap convertViewToBitmap(View view) {
		view.setDrawingCacheEnabled(true);
		view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
		view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
		view.buildDrawingCache();
		Bitmap cacheBitmap = view.getDrawingCache();
		Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
		return bitmap;
	}

	public static void measureView(View view) {
		ViewGroup.LayoutParams p = view.getLayoutParams();
		if (p == null) {
			p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
		}
		int childWidthSpec = ViewGroup.getChildMeasureSpec(0, 0, p.width);
		int lpHeight = p.height;
		int childHeightSpec;
		if (lpHeight > 0) {
			childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY);
		} else {
			childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
		}
		view.measure(childWidthSpec, childHeightSpec);
	}

	public static int dip2px(Context context, float dipValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dipValue * scale + 0.5f);
	}

	public static int px2dip(Context context, float pxValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (pxValue / scale + 0.5f);
	}

	/**
	 * 将px值转换为sp值，保证文字大小不变
	 */
	public static int px2sp(Context context, float pxValue) {
		final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
		return (int) (pxValue / fontScale + 0.5f);
	}

	/**
	 * 将sp值转换为px值，保证文字大小不变
	 */
	public static int sp2px(Context context, float spValue) {
		final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
		return (int) (spValue * fontScale + 0.5f);
	}

	public static int getScreenWidth(Context context) {
		Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		return Math.min(display.getHeight(), display.getWidth());
	}

	public static int getScreenHeight(Context context) {
		Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		return Math.max(display.getHeight(), display.getWidth());
	}

	public static void setListViewHeight(ListView listView, int dividerHeight) {
		// 获取ListView对应的Adapter
		ListAdapter listAdapter = listView.getAdapter();
		if (listAdapter == null) {
			return;
		}
		int totalHeight = 0;
		for (int i = 0; i < listAdapter.getCount(); i++) { // listAdapter.getCount()返回数据项的数目
			View listItem = listAdapter.getView(i, null, listView);
			listItem.measure(0, 0); // 计算子项View 的宽高
			totalHeight += listItem.getMeasuredHeight(); // 统计所有子项的总高度
		}
		ViewGroup.LayoutParams params = listView.getLayoutParams();
		params.height = totalHeight + (dividerHeight + 5);
		listView.setLayoutParams(params);
	}

	/**
	 * 得到列表无数据图片params
	 */
	public static LayoutParams getNodataImgParams(Activity activity) {
		int nodataWidth = (int) (ViewUtil.getScreenWidth(activity) * 2 / 5.0);
		int nodataHeight = (int) (nodataWidth * 7 / 6.0);
		LayoutParams nodataParams = new LayoutParams(nodataWidth, nodataHeight);
		nodataParams.topMargin = (nodataWidth);
		return nodataParams;
	}

}
