package cn.net.caoyao.cpzshandset.common.util;

import android.content.Context;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.GridLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TableRow;

public class ScreenManager {
	public static final int STANDARD_WITH = 480;// 标准宽的像素
	public static final int STANDARD_HEIGTH = 854;// 标准高的像素
	private float WIDTH_RATE; // 适配宽比
	private float HEIGHT_RATE; // 适配高比
	private float CHANGE_RATE = 1.5f; // dip转px转换比例
	/**
	 * 基准屏幕密度
	 */
	public static final double STANDARD_DENSITY = 160;

	/**
	 * 当前屏幕密度
	 */
	public double CURRENT_DENSITY;

	public static Rect GetDisplaySize(Context context) {
		DisplayMetrics metrics = context.getResources().getDisplayMetrics();
		return new Rect(0, 0, metrics.widthPixels, metrics.heightPixels);
	}

	/**
	 * 屏幕密度比例
	 */
	private double DENSITY_RATIO;

	public ScreenManager(Context context) {

		DisplayMetrics metrics = context.getResources().getDisplayMetrics();

		int large = (metrics.widthPixels > metrics.heightPixels) ? metrics.widthPixels : metrics.heightPixels;
		int small = (metrics.widthPixels < metrics.heightPixels) ? metrics.widthPixels : metrics.heightPixels;

		WIDTH_RATE = small * 1f / STANDARD_WITH;
		HEIGHT_RATE = large * 1f / STANDARD_HEIGTH;

		CURRENT_DENSITY = metrics.densityDpi;
		DENSITY_RATIO = STANDARD_DENSITY / CURRENT_DENSITY;

		/*
		 * float density = metrics.density; Toast.makeText(context,
		 * "density="+density, Toast.LENGTH_SHORT).show();
		 */
	}

	/**
	 * 关于相对布局适配 不用适配传值为“0”
	 * 
	 * @param view
	 *            适配的对象
	 * @param width
	 *            宽 dip
	 * @param hight
	 *            高 dip
	 * @param topMargin
	 *            顶距离 dip
	 * @param leftMargin
	 *            左距离 dip
	 * @param rightMargin
	 *            右距离 dip
	 * @param bottomMargin
	 *            底距离 dip
	 */
	public void LinearLayoutParams(View view, float width, float hight, float topMargin, float leftMargin,
			float rightMargin, float bottomMargin) {
		LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
		if (width != 0) {
			params.width = (int) (width * CHANGE_RATE * WIDTH_RATE);
		}
		if (hight != 0) {
			params.height = (int) (hight * CHANGE_RATE * HEIGHT_RATE);
		}
		if (topMargin != 0) {
			params.topMargin = (int) (topMargin * CHANGE_RATE * HEIGHT_RATE);
		}
		if (bottomMargin != 0) {
			params.bottomMargin = (int) (bottomMargin * CHANGE_RATE * HEIGHT_RATE);
		}
		if (leftMargin != 0) {
			params.leftMargin = (int) (leftMargin * CHANGE_RATE * WIDTH_RATE);
		}
		if (rightMargin != 0) {
			params.rightMargin = (int) (rightMargin * CHANGE_RATE * WIDTH_RATE);
		}
	}

	/**
	 * 关于相对布局适配 不用适配传值为“0”
	 * 
	 * @param view
	 *            适配的对象
	 * @param width
	 *            宽 dip
	 * @param hight
	 *            高 dip
	 * @param topMargin
	 *            顶距离 dip
	 * @param leftMargin
	 *            左距离 dip
	 * @param rightMargin
	 *            右距离 dip
	 * @param bottomMargin
	 *            底距离 dip
	 */
	public void RelativeLayoutParams(View view, float wight, float hight, float topMargin, float leftMargin,
			float rightMargin, float bottomMargin) {
		RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view.getLayoutParams();
		if (wight != 0) {
			params.width = (int) (wight * CHANGE_RATE * WIDTH_RATE);
		}
		if (hight != 0) {
			params.height = (int) (hight * CHANGE_RATE * HEIGHT_RATE);
		}
		if (topMargin != 0) {
			params.topMargin = (int) (topMargin * CHANGE_RATE * HEIGHT_RATE);
		}
		if (bottomMargin != 0) {
			params.bottomMargin = (int) (bottomMargin * CHANGE_RATE * HEIGHT_RATE);
		}
		if (leftMargin != 0) {
			params.leftMargin = (int) (leftMargin * CHANGE_RATE * WIDTH_RATE);
		}
		if (rightMargin != 0) {
			params.rightMargin = (int) (rightMargin * CHANGE_RATE * WIDTH_RATE);
		}
	}

	/**
	 * 
	 * @param view
	 * @param wight
	 * @param hight
	 * @param topMargin
	 * @param leftMargin
	 * @param rightMargin
	 * @param bottomMargin
	 */
	public void FragmentLayoutParams(View view, float wight, float hight, float topMargin, float leftMargin,
			float rightMargin, float bottomMargin) {
		FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) view.getLayoutParams();
		if (wight != 0) {
			params.width = (int) (wight * CHANGE_RATE * WIDTH_RATE);
		}
		if (hight != 0) {
			params.height = (int) (hight * CHANGE_RATE * HEIGHT_RATE);
		}
		if (topMargin != 0) {
			params.topMargin = (int) (topMargin * CHANGE_RATE * HEIGHT_RATE);
		}
		if (bottomMargin != 0) {
			params.bottomMargin = (int) (bottomMargin * CHANGE_RATE * HEIGHT_RATE);
		}
		if (leftMargin != 0) {
			params.leftMargin = (int) (leftMargin * CHANGE_RATE * WIDTH_RATE);
		}
		if (rightMargin != 0) {
			params.rightMargin = (int) (rightMargin * CHANGE_RATE * WIDTH_RATE);
		}
	}

	public void GridLayoutParams(View view, float wight, float hight, float topMargin, float leftMargin,
			float rightMargin, float bottomMargin) {
		GridLayout.LayoutParams params = (GridLayout.LayoutParams) view.getLayoutParams();
		if (wight != 0) {
			params.width = (int) (wight * CHANGE_RATE * WIDTH_RATE);
		}
		if (hight != 0) {
			params.height = (int) (hight * CHANGE_RATE * HEIGHT_RATE);
		}
		if (topMargin != 0) {
			params.topMargin = (int) (topMargin * CHANGE_RATE * HEIGHT_RATE);
		}
		if (bottomMargin != 0) {
			params.bottomMargin = (int) (bottomMargin * CHANGE_RATE * HEIGHT_RATE);
		}
		if (leftMargin != 0) {
			params.leftMargin = (int) (leftMargin * CHANGE_RATE * WIDTH_RATE);
		}
		if (rightMargin != 0) {
			params.rightMargin = (int) (rightMargin * CHANGE_RATE * WIDTH_RATE);
		}
	}

	public void TableLayoutParams(View view, float wight, float hight, float topMargin, float leftMargin,
			float rightMargin, float bottomMargin) {
		TableLayout.LayoutParams params = (TableLayout.LayoutParams) view.getLayoutParams();
		if (wight != 0) {
			params.width = (int) (wight * CHANGE_RATE * WIDTH_RATE);
		}
		if (hight != 0) {
			params.height = (int) (hight * CHANGE_RATE * HEIGHT_RATE);
		}
		if (topMargin != 0) {
			params.topMargin = (int) (topMargin * CHANGE_RATE * HEIGHT_RATE);
		}
		if (bottomMargin != 0) {
			params.bottomMargin = (int) (bottomMargin * CHANGE_RATE * HEIGHT_RATE);
		}
		if (leftMargin != 0) {
			params.leftMargin = (int) (leftMargin * CHANGE_RATE * WIDTH_RATE);
		}
		if (rightMargin != 0) {
			params.rightMargin = (int) (rightMargin * CHANGE_RATE * WIDTH_RATE);
		}
	}

	public void TableRowParams(View view, float wight, float hight, float topMargin, float leftMargin,
			float rightMargin, float bottomMargin) {
		TableRow.LayoutParams params = (TableRow.LayoutParams) view.getLayoutParams();
		if (wight != 0) {
			params.width = (int) (wight * CHANGE_RATE * WIDTH_RATE);
		}
		if (hight != 0) {
			params.height = (int) (hight * CHANGE_RATE * HEIGHT_RATE);
		}
		if (topMargin != 0) {
			params.topMargin = (int) (topMargin * CHANGE_RATE * HEIGHT_RATE);
		}
		if (bottomMargin != 0) {
			params.bottomMargin = (int) (bottomMargin * CHANGE_RATE * HEIGHT_RATE);
		}
		if (leftMargin != 0) {
			params.leftMargin = (int) (leftMargin * CHANGE_RATE * WIDTH_RATE);
		}
		if (rightMargin != 0) {
			params.rightMargin = (int) (rightMargin * CHANGE_RATE * WIDTH_RATE);
		}
	}

	/**
	 * 设置单个距离的大小按比例变换
	 * 
	 * @param size
	 * @return
	 */
	public int changeParams(float size) {
		return (int) (size * CHANGE_RATE * HEIGHT_RATE);
	}

	/**
	 * 宽度转换
	 * 
	 * @param size
	 *            单位dip/dp
	 * @return
	 */
	public int changeDipWidth(float size) {
		return (int) (size * CHANGE_RATE * WIDTH_RATE);
	}

	/**
	 * 宽度转换
	 * 
	 * @param size
	 *            单位pix
	 * @return
	 */
	public int changePixWidth(float size) {
		return (int) (size * WIDTH_RATE);
	}

	/**
	 * 高度转换
	 * 
	 * @param size
	 *            单位dip
	 * @return
	 */
	public int changeDipHeight(float size) {

		return (int) (size * CHANGE_RATE * HEIGHT_RATE);
	}

	/**
	 * 高度转换
	 * 
	 * @param size
	 *            单位pix
	 * @return
	 */
	public int changePixHeight(float size) {

		return (int) (size * HEIGHT_RATE);
	}

	/**
	 * 设置字体大小
	 * 
	 * @param standardSize
	 *            原始大小
	 * @return int
	 */
	public int setTextSize(int standardSize) {
		int currentSize;

		currentSize = (int) (standardSize * HEIGHT_RATE * DENSITY_RATIO);

		return currentSize;
	}

	/**
	 * 根据高度获取宽高等比例的宽度
	 * 
	 * @param width
	 * @param height
	 * @return
	 */
	public int changeDipWidthProportional(float width, float height) {
		float h = height * CHANGE_RATE * HEIGHT_RATE;
		return (int) (width * h / height);
	}

	/**
	 * 根据宽度获取宽高等比例的高度
	 * 
	 * @param width
	 * @param height
	 * @return
	 */
	public int changeDipHeightProportional(float width, float height) {
		float w = width * CHANGE_RATE * HEIGHT_RATE;
		return (int) (w * height / width);
	}
}
