package com.backfire.widget;


/**   
 * @Title: AdapterExecutor.java
 * @Description: 屏幕适配策略
 * @author Potter
 * @date 2013年10月24日 上午9:03:34
 * @version V2.0   
 */
public class AdapterViewStrategy implements IAdapterView{
	private enum Adapt{
		WIDHT,
		HEIGHT,
		POSX,
		POSY
	}
	
	private static final int STANDARD_WIDTH = 1080;
	private static final int STANDARD_HEIGHT = 720;
	
	private static int screenW;
	private static int screenH;
	
	public AdapterViewStrategy(int width, int height) {
		this.screenW=width;
		this.screenH=height;
	}

	public void adapt(int[] array,Adapt type){
		for(int i=0;i<array.length;i++){
			int adaptV=array[i];
			switch (type) {
			case WIDHT:
				adaptV=adaptW(array[i]);
				break;
			case HEIGHT:
				adaptV=adaptH(array[i]);
				break;
			case POSX:
				adaptV=adaptX(array[i]);
				break;
			case POSY:
				adaptV=adaptY(array[i]);
				break;
			}
			array[i]=adaptV;
		}
	}
	
	public int adaptX(float rawX,boolean isCenter){
		float rate = adaptRate();
		int result = 0;
		if(rate >0){
			result =(int) (rawX*rate);
		}else{
			rate = -rate;
			result = (int)((screenW-STANDARD_WIDTH*rate)/2 +rawX*rate);
		}
		if(result <= 0)
		{
			result = 1;
		}
		return result;
	}
	
	// Adapt
	public int adaptX(float rawX) {
		return adaptX(rawX,false);
	}
	
	public int adaptY(float rawY) {
		return adaptY(rawY, false);
	}
	
	public  int adaptY(float rawY,boolean isCenter) {
		float rate = adaptRate();
		int result = 0;
		if(rate >0){
			if(isCenter){
				result = (int)((screenH-STANDARD_HEIGHT*rate)/2);
			}else{
				result = (int)((screenH-STANDARD_HEIGHT*rate)/2 +rawY*rate);
			}
		}else{
			rate = -rate;
			result =(int) (rawY*rate);
		}
		if(result <= 0)
		{
			result = 1;
		}
		return result;
	}
	public int adaptW(int x)
	{
		int result =(int) (x*Math.abs(adaptRate()));
		if(result <= 0)
		{
			result = 1;
		}
		return result;
	}
	
	public int adaptH(int y)
	{
		int result = (int) (y*Math.abs(adaptRate()));
		if(result <= 0)
		{
			result = 1;
		}
		return result;
	}
	public float adaptRate(){
		try {
			float rateX= ((float)screenW)/(float)STANDARD_WIDTH;
			float ratey = ((float)screenH)/(float)STANDARD_HEIGHT;
			if(rateX > ratey){
				return -ratey;
			}else return rateX;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 1;
	}
}
