package com.tcl.factory.utils;

import com.tcl.tvmanager.TTv3DManager;
import com.tcl.tvmanager.TTvCommonManager;
import com.tcl.tvmanager.vo.EnTCL3DAspectRatio;
import com.tcl.tvmanager.vo.EnTCL3DVideo3DTo2D;
import com.tcl.tvmanager.vo.EnTCL3DVideoDisplayFormat;
import com.tcl.tvmanager.vo.EnTCLInputSource;

public class ConfigMethod {
	private final static String TAG = "TCLVideo3DController";
	public static final int SUB_MODE_3D_IND = 0;
	public static final int SUB_MODE_AUTO_IND = 1;
	public static final int SUB_MODE_2D3D_IND = 2;
	public static final int SUB_MODE_LR_IND = 3;
	public static final int SUB_MODE_UP_DOWN_IND = 4;
	public static final int SUB_MODE_HORIZONTAL_IND = 5;
	public static final int SUB_MODE_SEQ_IND = 6;

	public static boolean IS_FULL_SCREEN = true;

	/* 0(SUB_MODE_3D_IND) :3D mode valid; */
	public static boolean[] m3DSubModeArra = new boolean[] { false, false,
			false, false, false, false, false };
	public static String[] mSet3DModeArr = new String[] {
			EnTCL3DVideoDisplayFormat.EN_TCL_3D_NONE
					.toString(),
			EnTCL3DVideoDisplayFormat.EN_TCL_3D_AUTO
					.toString(),
			EnTCL3DVideoDisplayFormat.EN_TCL_3D_2DTO3D
					.toString(),
			EnTCL3DVideoDisplayFormat.EN_TCL_3D_SIDE_BY_SIDE
					.toString(),
			EnTCL3DVideoDisplayFormat.EN_TCL_3D_TOP_BOTTOM
					.toString(),
			EnTCL3DVideoDisplayFormat.EN_TCL_3D_LINE_ALTERNATIVE
					.toString(),
			EnTCL3DVideoDisplayFormat.EN_TCL_3D_FRAME_PACKING
					.toString() };
	public static int m3DUsefulTotal = 0;

	public static int get3DMode() {
		// int ret = mapAutoCurToDisplayMod(app);
		// Utils.showInfor(TAG, "get3DMode return " + ret);
		// return ret;
		return get3DModeNotMap();
	}

	public static int get3DModeNotMap() {
		int ret = 0;
		TTv3DManager tv3DManager = TTv3DManager.getInstance(null);
		if (tv3DManager != null) {
			EnTCL3DVideoDisplayFormat curFormat = tv3DManager.getDisplayFormat();
			if (curFormat != null) {
				String curModeOrd = curFormat.toString();
				for (int i = 0; i < mSet3DModeArr.length; i++) {
					if (curModeOrd.endsWith(mSet3DModeArr[i])) {
						ret = i;
						break;
					}
				}
			}
		} else {
			Debug.v(TAG, "get3DModeNotMap iS3DSkin is null!!!!");
		}
		return ret;
	}

	// private static int mapAutoCurToDisplayMod(TVRootApp app){
	// int retDisMod = ConfigMethod.SUB_MODE_3D_IND;
	// int curMod = getCurrent3DMode(app);
	// if(curMod == Enum3dType.EN_3D_NONE.ordinal()){
	// retDisMod = ConfigMethod.SUB_MODE_3D_IND;
	// }else if(curMod == Enum3dType.EN_3D_SIDE_BY_SIDE_HALF.ordinal()){
	// retDisMod = ConfigMethod.SUB_MODE_LR_IND;
	// }else if(curMod == Enum3dType.EN_3D_TOP_BOTTOM.ordinal()){
	// retDisMod = ConfigMethod.SUB_MODE_UP_DOWN_IND;
	// }else if(curMod == Enum3dType.EN_3D_LINE_ALTERNATIVE.ordinal()){
	// retDisMod = ConfigMethod.SUB_MODE_HORIZONTAL_IND;
	// }else if(curMod == Enum3dType.EN_3D_FRAME_ALTERNATIVE.ordinal()
	// || curMod == Enum3dType.EN_3D_FRAME_PACKING_1080P.ordinal()
	// || curMod == Enum3dType.EN_3D_FRAME_PACKING_720P.ordinal()){
	// retDisMod = ConfigMethod.SUB_MODE_SEQ_IND;
	// }else if(curMod == Enum3dType.EN_3D_2DTO3D.ordinal()){
	// retDisMod = ConfigMethod.SUB_MODE_2D3D_IND;
	// }else{
	//				
	// }
	// Debug.v(TAG,"mapAutoCurToDisplayMod retDisMod is "+retDisMod);
	// return retDisMod;
	// }

	// public static String get3DFormat_Auto(TVRootApp app){//add by tiangai
	// 2013-06-14
	// String formatStr = app.getString(R.string.tcl_mode_off_3d);
	// TTv3DManager tv3DManager = TTv3DManager.getInstance(app);
	// if (tv3DManager != null) {
	// EnTCL3DVideoDisplayFormat curFormat =
	// tv3DManager.get3DDisplayFormatOfCurrentVideo();
	// formatStr = curFormat.toString();
	// }
	// Debug.v(TAG,"============tiangai============current mode in get3DFormat_Auto========= "+
	// formatStr);
	// return formatStr;
	// }

	public static int get3DFormat_Auto() {// add by tiangai
														// 2013-06-14
		int ret = 0;
		TTv3DManager tv3DManager = TTv3DManager.getInstance(null);
		if (tv3DManager != null) {
			EnTCL3DVideoDisplayFormat curFormat = tv3DManager
					.get3DDisplayFormatOfCurrentVideo();
			if (curFormat != null) {
				String curModeOrd = curFormat.toString();
				for (int i = 0; i < mSet3DModeArr.length; i++) {
					if (curModeOrd.endsWith(mSet3DModeArr[i])) {
						ret = i;
						break;
					}
				}
			}
		} else {
			Debug.v(TAG, "get3DModeNotMap iS3DSkin is null!!!!");
		}
		return ret;
	}

	public static boolean[] get3DModeState() {
		return new boolean[] { true, true, true, true, true, true, true };
	}

	public static boolean check3DModeSame(int setTo) {
		boolean bRet = false;
		// TvS3DManager threeManager = TvS3DManager.getInstance();
		// EnTCL3DVideoDisplayFormat setToMod =
		// EnTCL3DVideoDisplayFormat.valuesof(mSet3DModeArr[setTo]);
		// Utils.showInfor(TAG,"--------------------check3DModeSame "+setTo+" mode name is "+mSet3DModeArr[setTo]);
		// if(threeManager != null ){
		// EnumEnTCL3DVideoDisplayFormat curMode =
		// threeManager.getDisplayFormat();
		// if(setToMod == curMode){
		// bRet = true;
		// Debug.v(TAG,"check3DModeSame set the same mode=========");
		// }
		// }else{
		// Debug.v(TAG,"check3DModeSame threeManager is null");
		// }

		TTv3DManager threeManager = TTv3DManager.getInstance(null);
		EnTCL3DVideoDisplayFormat setToMod = EnTCL3DVideoDisplayFormat
				.valueOf(mSet3DModeArr[setTo]);
		if (threeManager != null) {
			EnTCL3DVideoDisplayFormat curMode = threeManager.getDisplayFormat();
			Debug.v(TAG,
					"check3DModeSame set the same mode========= setToMod is "
							+ setToMod);
			Debug.v(TAG, "check3DModeSame set the same mode========= curMode is "
					+ curMode);
			if (setToMod == curMode) {
				bRet = true;
				Debug.v(TAG, "check3DModeSame set the same mode========= ");
			}
		} else {
			Debug.v(TAG, "check3DModeSame threeManager is null");
		}
		return bRet;
	}

	public static boolean set3DModeNotCheckSame( int setTo) {// add
																			// by
																			// tiangai
																			// 强制设置3D模式
		return set3DModeCondition(setTo, false);
	}

	public static boolean set3DMode(int setTo) {
		return set3DModeCondition(setTo, true);
	}

	public static boolean set3DModeCondition(int setTo,
			boolean checkSame) {
		boolean bRet = false;
		TTv3DManager tv3DManager = TTv3DManager.getInstance(null);
		EnTCL3DVideoDisplayFormat setToMod = EnTCL3DVideoDisplayFormat
				.valueOf(mSet3DModeArr[setTo]);
		if (tv3DManager != null) {
			if (checkSame == false || check3DModeSame(setTo) == false) {
				bRet = tv3DManager.setDisplayFormat(setToMod);
			} else {
				Debug.v(TAG, "set3DMode faile checkSame " + checkSame
						+ " setToMod " + setToMod);
			}
		} else {
			Debug.v(TAG, "set3DModeCondition tvManager is null!!!");
		}
		if (bRet == false) {
			Debug.v(TAG, "set3DMode " + setTo + " fail!!!!!!!!!!!");
		}
		Debug.v(TAG, "IS_FULL_SCREEN is (set3DMode) " + IS_FULL_SCREEN
				+ " !!!!!!!!!!!");
		Debug.v(TAG, "getScaleMode(app) is (set3DMode) " + getScaleMode()
				+ " !!!!!!!!!!!");
		/*if (IS_FULL_SCREEN && getScaleMode(app) != PS.PS_FULL) {
			setScaleMode(app, PS.PS_FULL);
		}*/
		return bRet;
	}

	/* about switch left and right */
	public class SWITCH_LR {
		public static final int OFF = 0/*
										 * EN_ThreeD_Video_LRVIEWSWITCH.DB_ThreeD_Video_LRVIEWSWITCH_NOTEXCHANGE
										 * .ordinal()
										 */;
		public static final int ON = 1;
		public static final int COUNT = 2;
	}

	public static int getSwitchLeftRight() {// add by tiangai
														// 获取左右交换模式
		int ret = 0;
		TTv3DManager tv3DManager = TTv3DManager.getInstance(null);
		if (tv3DManager != null) {
			boolean curLr = tv3DManager.getLrViewSwitch();
			Debug.v(TAG, "========getSwitchLeftRight " + curLr);
			if (curLr) {
				ret = SWITCH_LR.ON;
			} else {
				ret = SWITCH_LR.OFF;
			}
		} else {
			Debug.v(TAG, "getSwitchLeftRight s3DManager is null!!!!!");
		}
		return ret;
	}

	public static boolean setSwitchLeftRight(int setTo) {// add by
																		// tiangai
																		// 设置左右交换
		boolean ret = false;
		TTv3DManager tv3DManager = TTv3DManager.getInstance(null);
		if (tv3DManager != null) {
			Debug.v(TAG, "setSwitchLeftRight setTo " + setTo);
			if (setTo == SWITCH_LR.ON) {
				ret = tv3DManager.setLrViewSwitch(true);
			} else if (setTo == SWITCH_LR.OFF) {
				ret = tv3DManager.setLrViewSwitch(false);
			} else {
				Debug.v(TAG, "setSwitchLeftRight setTo is out SWITCH_LR!!!!!");
			}
		} else {
			Debug.v(TAG, "setSwitchLeftRight tvManager is null!!!!!");
		}
		Debug.v(TAG, "setSwitchLeftRight  end == ret is " + ret);
		return ret;
	}

	/* about depth */
	public static int getDepthValue() {// add by tiangai 获取景深
		int ret = 0;
		TTv3DManager tv3DManager = TTv3DManager.getInstance(null);
		if (tv3DManager != null) {
			ret = tv3DManager.get3DDepthMode();
		} else {
			Debug.v(TAG, "getDepthValue tvManager is null!!!!");
		}
		return ret;
	}

	public static int getDepthMax() {
		int ret = 30;
		return ret;
	}

	public static int getDepthMin() {
		int ret = 0;
		return ret;
	}

	public static boolean setDepthValue(int setTo) {
		boolean ret = false;
		TTv3DManager tv3DManager = TTv3DManager.getInstance(null);
		if (tv3DManager != null) {
			ret = tv3DManager.set3DDepthMode(setTo);
		} else {
			Debug.v(TAG, "getDepthValue threeManager is null!!!!");
		}
		Debug.v(TAG, "setDepthValue end ret is ==========" + ret);
		return ret;
	}

	/* translate 3d to 2d */
	public class P3D_2D {
//		public static final int P3D_2D_NONE = 0;
//		public static final int P3D_2D_SBS = 1;
//		public static final int P3D_2D_TB = 2;
//		public static final int P3D_2D_FP = 3;
//		public static final int P3D_2D_LA = 4;
//		public static final int P3D_2D_COUNT = 5;
		public static final int P3DTO2D_OFF = 0;
		public static final int P3DTO2D_LEFT = 1;
		public static final int P3DTO2D_RIGHT = 2;
		public static final int P3DTO2D_MAX = 3;
		}


	public static int getSwitch3dTo2d() {// add by tiangai ???3D??D
		int ret = 0;
		TTv3DManager tv3DManager = TTv3DManager.getInstance(null);
		if (tv3DManager != null) {
			EnTCL3DVideo3DTo2D curMode = tv3DManager.getDisplay3DTo2DMode();
//			if (curMode == EnTCL3DVideo3DTo2D.EN_TCL_3DTO2D_NONE
//					|| curMode == EnTCL3DVideo3DTo2D.EN_TCL_3DTO2D_AUTO) {
//				ret = P3D_2D.P3D_2D_NONE;
//			} else if (curMode == EnTCL3DVideo3DTo2D.EN_TCL_3DTO2D_SIDE_BY_SIDE) {
//				ret = P3D_2D.P3D_2D_SBS;
//			} else if (curMode == EnTCL3DVideo3DTo2D.EN_TCL_3DTO2D_TOP_BOTTOM) {
//				ret = P3D_2D.P3D_2D_TB;
//			} else if (curMode == EnTCL3DVideo3DTo2D.EN_TCL_3DTO2D_FRAME_PACKING) {
//				ret = P3D_2D.P3D_2D_FP;
//			} else if (curMode == EnTCL3DVideo3DTo2D.EN_TCL_3DTO2D_LINE_ALTERNATIVE) {
//				ret = P3D_2D.P3D_2D_LA;

			if (curMode == EnTCL3DVideo3DTo2D.EN_TCL_3DTO2D_OFF || curMode == EnTCL3DVideo3DTo2D.EN_TCL_3DTO2D_MAX) {
				ret = P3D_2D.P3DTO2D_OFF;
			} else if (curMode == EnTCL3DVideo3DTo2D.EN_TCL_3DTO2D_LEFT) {
				ret = P3D_2D.P3DTO2D_LEFT;
			} else if (curMode == EnTCL3DVideo3DTo2D.EN_TCL_3DTO2D_RIGHT) {
				ret = P3D_2D.P3DTO2D_RIGHT;
			} else {
				Debug.v(TAG, "getSwitch3dTo2d curMode is wrong!!!!");
			}
		} else {
			Debug.v(TAG, "getSwitch3dTo2d threeManager is null!!!!");
		}
		return ret;
	}

	public static boolean setSwitch3dTo2d(int setTo) {// add by
																	// tiangai
																	// 设置3D??D
		boolean ret = false;
		TTv3DManager tv3DManager = TTv3DManager.getInstance(null);
		if (tv3DManager != null) {
//			EnTCL3DVideo3DTo2D setMode = EnTCL3DVideo3DTo2D.EN_TCL_3DTO2D_NONE;
//			switch (setTo) {
//			case P3D_2D.P3D_2D_NONE:
//				setMode = EnTCL3DVideo3DTo2D.EN_TCL_3DTO2D_NONE;
//				break;
//			case P3D_2D.P3D_2D_SBS:
//				setMode = EnTCL3DVideo3DTo2D.EN_TCL_3DTO2D_SIDE_BY_SIDE;
//				break;
//			case P3D_2D.P3D_2D_TB:
//				setMode = EnTCL3DVideo3DTo2D.EN_TCL_3DTO2D_TOP_BOTTOM;
//				break;
//			case P3D_2D.P3D_2D_FP:
//				setMode = EnTCL3DVideo3DTo2D.EN_TCL_3DTO2D_FRAME_PACKING;
//				break;
//			case P3D_2D.P3D_2D_LA:
//				setMode = EnTCL3DVideo3DTo2D.EN_TCL_3DTO2D_LINE_ALTERNATIVE;
//				break;
//			}
			EnTCL3DVideo3DTo2D setMode = EnTCL3DVideo3DTo2D.EN_TCL_3DTO2D_OFF;
			switch (setTo) {
			case P3D_2D.P3DTO2D_MAX:
			case P3D_2D.P3DTO2D_OFF:
				setMode = EnTCL3DVideo3DTo2D.EN_TCL_3DTO2D_OFF;
				break;
			case P3D_2D.P3DTO2D_LEFT:
				setMode = EnTCL3DVideo3DTo2D.EN_TCL_3DTO2D_LEFT;
				break;
			case P3D_2D.P3DTO2D_RIGHT:
				setMode = EnTCL3DVideo3DTo2D.EN_TCL_3DTO2D_RIGHT;
				break;
				}
			ret = tv3DManager.set3DTo2D(setMode);
		} else {
			Debug.v(TAG, "setSwitch3dTo2d s3DManager is null!!!");
		}
		return ret;
	}

	/* about scale mode */
	public class PS {
		public static final int PS_FULL = 0;
		public static final int PS_CENTER = 1;
		public static final int PS_AUTO = 2;
		public static final int PS_NUM = 3;
	}

	public static int getScaleMode() {// add by tiangai 获取屏幕比例模式
		int ret = 0;
		TTv3DManager tv3DManager = TTv3DManager.getInstance(null);
		if (tv3DManager != null) {
			EnTCL3DAspectRatio mod = tv3DManager.get3DOutputAspectMode();
			if (mod == EnTCL3DAspectRatio.EN_TCL_3D_16X9) {
				ret = PS.PS_AUTO;
			} else if (mod == EnTCL3DAspectRatio.EN_TCL_3D_4X3) {
				ret = PS.PS_CENTER;
			} else {
				ret = PS.PS_FULL;
			}
		} else {
			Debug.v(TAG, "getScaleMode pictureDesk is null");
		}
		return ret;
	}

	public static boolean setScaleMode(int setTo) {// add by
																// tiangai
																// 设置屏幕比例模式
		boolean ret = false;
		TTv3DManager tv3DManager = TTv3DManager.getInstance(null);
		if (tv3DManager != null) {
			
			EnTCL3DAspectRatio set = EnTCL3DAspectRatio.EN_TCL_3D_16X9;
			switch (setTo) {
			case PS.PS_FULL:
				set = EnTCL3DAspectRatio.EN_TCL_3D_16X9;
				break;
			case PS.PS_CENTER:
				set = EnTCL3DAspectRatio.EN_TCL_3D_4X3;
				break;
			case PS.PS_AUTO:
				set = EnTCL3DAspectRatio.EN_TCL_3D_AUTO;
				break;
			}
			Debug.v(TAG, "setScaleMode setVideoArc setTo " + set.toString());
			ret = tv3DManager.set3DOutputAspectMode(set);
		} else {
			Debug.v(TAG, "getScaleMode pictureDesk is null");
		}
		return ret;
	}

	public static boolean autoPlay3D() {
		boolean bRet = false;
		int type = get3DMode();
		if (type != 0) {
			bRet = true;
		}
		return bRet;
	}

	public static EnTCLInputSource getCurrentInputSource() {
		TTvCommonManager tvManager = TTvCommonManager.getInstance(null);
		EnTCLInputSource curSource;
		curSource = tvManager.getCurrentInputSource();
		Debug.v(TAG, "getCurrentInputSource return " + curSource.name());
		return curSource;
	}
}
