package sto.android.app;

import android.content.Context;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
/**
 * 荣信接口实现
 * @author Don
 *
 */
public class RongXinJNI implements StoJNI{

	private static RongXinJNI rongXinJNI;
	private static String TAG = "rongXinJNI";
	private Context mContext; 
	private final String DEV_C40="C40";
	private String strDevModel;
	 
	
	private RongXinJNI(Context context) {
		mContext = context;
	}

	public static RongXinJNI getInstance(Context context) {
		if (rongXinJNI == null) {
			rongXinJNI = new RongXinJNI(context);
		}
		return rongXinJNI;
	}


	@Override
	public void SetScannerOn() { 
		if( getDevModel().equals(DEV_C40 )){
			RXC40Scanner.getInstance().SetScannerOn();
		} else {
			RX70Scanner.getInstance().Barcode_open();
		}

		
	}


	@Override
	public void SetScannerOff() { 
		if( getDevModel().equals(DEV_C40 )){
			RXC40Scanner.getInstance().SetScannerOff();
		} else {
			RX70Scanner.getInstance().Barcode_Close();
		}
	}


	@Override
	public void SetGPIOStatusJNI(int gpio, int status) {
		
		
	}


	@Override
	public void SetScannerStart() {
		if( getDevModel().equals(DEV_C40 )){
			RXC40Scanner.getInstance().SetScannerStart();
		} else {
			RX70Scanner.getInstance().Barcode_StartScan();
		}
		
		
	}


	@Override
	public void SetScannerStop() {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().SetScannerStop();
		} else {
			RX70Scanner.getInstance().Barcode_StopScan();
		}
		
	}


	@Override
	public int SetScannerPara(int para, int value) {
		if (getDevModel().equals(DEV_C40)) {
			return RXC40Scanner.getInstance().SetScannerPara(para, value);
		} else {
			return 0;
		}
	}

	@Override
	public int GetScannerPara(int para) {
		if (getDevModel().equals(DEV_C40)) {
			return RXC40Scanner.getInstance().GetScannerPara(para);
		}
		return 0;
	}


	@Override
	public String SetScannerPara(String para, String value) {
		if (getDevModel().equals(DEV_C40)) {
			if(para.startsWith("0x")  || para.startsWith("0X")){
				para=para.substring(2);
			}
			if(value.startsWith("0x")  || value.startsWith("0X")){
				value=value.substring(2);
			}
			
			return RXC40Scanner.getInstance().SetScannerPara(para,value);
		}
		return null;
	}


	@Override
	public String GetScannerPara(String para) {
		if (getDevModel().equals(DEV_C40)) {
			byte[] byteArray=  RXC40Scanner.getInstance().GetScannerPara(para);
			String str=bytesToHexString(byteArray);
			return str;
		}
		return null;
	}


	@Override
	public void SetScannerRetriger() {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().SetScannerRetriger();
		} else {
			RX70Scanner.getInstance().Barcode_StartScan();
		}
		
	}
	public   String bytesToHexString(byte[] src){  
	    StringBuilder stringBuilder = new StringBuilder("");  
	    if (src == null || src.length <= 0) {  
	        return null;  
	    }  
	    for (int i = 0; i < src.length; i++) {  
	        int v = src[i] & 0xFF;  
	        String hv = Integer.toHexString(v);  
	        if (hv.length() < 2) {  
	            stringBuilder.append(0);  
	        }  
	        stringBuilder.append(hv);  
	    }  
	    return stringBuilder.toString();  
	}  
	
	@Override
	public void SetScannerTimerOut(int sec) {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().SetScannerTimerOut(sec);
		}
	}

	@Override
	public boolean GetScannerIsScanning() {
		if (getDevModel().equals(DEV_C40)) {
			return RXC40Scanner.getInstance().GetScannerIsScanning();
		} 
		return false;
	}


	@Override
	public int getmScanModel() {
		if (getDevModel().equals(DEV_C40)) {
			return RXC40Scanner.getInstance().getmScanModel();
		} 
		return 0;
	}


	@Override
	public void SetSystemReboot() {

		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().SetSystemReboot(mContext);
		} else {
			// /RX70Scanner.getInstance().
		}
	}

	@Override
	public void SetSystemShutdown() {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().SetSystemShutdown(mContext);
		} else {
			// /RX70Scanner.getInstance().
		}

	}


	@Override
	public void SetSystemTime(String mm) {

		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().SetSystemTime(mContext, mm);
		} else {
			RX70Scanner.getInstance().SetSystemTime(mContext, mm);
		}
	}

	@Override
	public void SetMachineCode(String code) {
		
		
	}


	@Override
	public String GetMachineCode() {
		if (getDevModel().equals(DEV_C40)) {
			return RXC40Scanner.getInstance().GetMachineCode();
		} else {
			return RX70Scanner.getInstance().GetMachineCode(mContext);
		}

	}

	@Override
	public void disableKeypad() {
		if (getDevModel().equals(DEV_C40)) {
			  RXC40Scanner.getInstance().setInterruptKeys(mContext,true);
		} else {
			
		}
	}

	@Override
	public void disableKeypadEnableCustom(String customstr) {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().disableKeypadEnableCustom(mContext,
					customstr);
		} else {
			
		}
	}

	@Override
	public void enableKeypad() {
		if (getDevModel().equals(DEV_C40)) {
			  RXC40Scanner.getInstance().setInterruptKeys(mContext,false);
		} else {
			
		}
	}

	@Override
	public void enableKeydownTone() {
		if (getDevModel().equals(DEV_C40)) {
			  RXC40Scanner.getInstance().setKeyDownTone(mContext,true);
		} else {
			
		}
	}

	@Override
	public int GetStatusKeydownTone() {
		if (getDevModel().equals(DEV_C40)) {
			return RXC40Scanner.getInstance().GetKeydownTone();
		} else {
		return 0;
		}
		 
	}

	@Override
	public void disableKeydownTone() {
		if (getDevModel().equals(DEV_C40)) {
			  RXC40Scanner.getInstance().setKeyDownTone(mContext,false);
		} else {
			
		}
	}

	@Override
	public void TurnOffStatusBarExpand() {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().TurnOffStatusBar(mContext, false);
		} else {

		}

	}

	@Override
	public int GetStatusBarExpand() {

		if (getDevModel().equals(DEV_C40)) {
			return RXC40Scanner.getInstance().GetStatusBarExpand();
		} else {
			return 0;
		}
	}

	@Override
	public void TurnOnStatusBarExpand() {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().TurnOffStatusBar(mContext, true);
		} else {

		}
	}

	@Override
	public void TurnOffUSBDebug() {

		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().SetUSBDebug(mContext, false);
		} else {

		}
	}

	@Override
	public int GetStatusUSBDebug() {

		return android.provider.Settings.Secure.getInt(
				mContext.getContentResolver(), Settings.Global.ADB_ENABLED, 0) > 0 ? 1
				: 0;
	}

	@Override
	public void TurnOnUSBDebug() {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().SetUSBDebug(mContext, true);
		} else {

		}
	}

	@Override
	public void TurnOffInstallManager() {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().InstallApkEn(mContext, false);
		} else {

		}
	}

	@Override
	public int GetStatusInstallManager() {

		if (getDevModel().equals(DEV_C40)) {
		 return 	RXC40Scanner.getInstance().GetStatusInstallManager( );
		} else {
			return 0;
		}
	}

	@Override
	public void TurnOnInstallManager() {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().InstallApkEn(mContext, true);
		} else {

		}
	}

	@Override
	public void TurnOnScreenLock() {
		if (getDevModel().equals(DEV_C40)) {
			  RXC40Scanner.getInstance().SetScreenLock(mContext,true );
		} else {
			 
		}
	}

	@Override
	public int GetStatusScreenLock() {
		if (getDevModel().equals(DEV_C40)) {
			return RXC40Scanner.getInstance().GetStatusScreenLock( );
		} else {
			return 0;
		}
	}

	@Override
	public void TurnOffScreenLock() {
		if (getDevModel().equals(DEV_C40)) {
			  RXC40Scanner.getInstance().SetScreenLock(mContext,false );
		} else {
			 
		}
	}

	@Override
	public int GetStatusWakeUp() {
		if (getDevModel().equals(DEV_C40)) {
			return RXC40Scanner.getInstance().GetStatusWakeUp();
		} else
			return 0;
	}


	@Override
	public void CfgPowerKeyWakeUp() {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().setWakeup(mContext, true);
		}
	}

	@Override
	public void CfgAnyKeyWakeUp() {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().setWakeup(mContext, false);
		}
	}

	@Override
	public void Disable_touch_screen() {
		if (getDevModel().equals(DEV_C40)) {
			if(RXC40Scanner.getInstance().GetTouchStatus()==0){
				return ;
			}
			
			RXC40Scanner.getInstance().setTouchStatusEn(mContext, false);
		}
	}

	@Override
	public void Enable_touch_screen() {
		if (getDevModel().equals(DEV_C40)) {
			if(RXC40Scanner.getInstance().GetTouchStatus()==1){
				return ;
			}
			RXC40Scanner.getInstance().setTouchStatusEn(mContext, true);
		}
	}

	@Override
	public int GetStatusTouch_screen() {
		if (getDevModel().equals(DEV_C40)) {
			return RXC40Scanner.getInstance().GetTouchStatus();
		}
		return 0;
	}


	@Override
	public void StoJNI_InstallApkWithSilence(String apkPath,
			boolean runAfterFinished) {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().installApkWithSilence(mContext, apkPath,
					runAfterFinished);
		}
		
		
	}

	@Override
	public void TurnOnOffGPS(boolean set) {
		if (getDevModel().equals(DEV_C40)) {
			 boolean isOpened=RXC40Scanner.getInstance().isGpsOpened(mContext);
			 if(isOpened ==set){
				 return ;
			 }
			 RXC40Scanner.getInstance().turnOnOffGPS(mContext, set);
			 
		} else {
			
		}
	}

	@Override
	public void TurnOnOffBrowser(boolean set) {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().TurnOnOffBrowser(mContext, set);
		}
	}

	@Override
	public void Disable_home_touch_screen() {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance(). disableHomeKeys(mContext);
		} else {
			
		}
	}

	@Override
	public void Enable_home_touch_screen() {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().setKeyboardEn(mContext,
					android.view.KeyEvent.KEYCODE_HOME);
		} else {

		}
	}

	@Override
	public int GetStatusTouch_Home_screen() {
		if (getDevModel().equals(DEV_C40)) {
			return RXC40Scanner.getInstance().GetHomeKeyStatus();
		} else {
			return 0;
		}
		
	}

	@Override
	public void Disable_menu_touch_screen() {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().disableKeypadEnableCustom(mContext,
					String.valueOf(android.view.KeyEvent.KEYCODE_MENU));
		} else {
			
		}
	}

	@Override
	public void Enable_menu_touch_screen() {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().setKeyboardEn(mContext,
					android.view.KeyEvent.KEYCODE_MENU);
		} else {

		}
	}

	@Override
	public int GetStatusTouch_Menu_screen() {

		if (getDevModel().equals(DEV_C40)) {
			return RXC40Scanner.getInstance().GetMenuStatus();
		} else {
			return 0;
		}
	}

	@Override
	public void Disable_back_touch_screen() {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().disableKeypadEnableCustom(mContext,
					String.valueOf(android.view.KeyEvent.KEYCODE_BACK));
		} else {
			
		}
	}

	@Override
	public void Enable_back_touch_screen() {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().setKeyboardEn(mContext,
					android.view.KeyEvent.KEYCODE_BACK);
		} else {

		}
	}

	@Override
	public int GetStatusTouch_Back_screen() {

		if (getDevModel().equals(DEV_C40)) {
			return RXC40Scanner.getInstance().GetBackStatus();
		} else {
			return 0;
		}
	}

	private String getDevModel() {
		if (strDevModel == null || TextUtils.isEmpty(strDevModel)) {
			strDevModel = android.os.Build.MODEL;
		}
		return strDevModel;
	}


	@Override
	public void setmScanCB(ScanCallBack mScanCB) {
		if (getDevModel().equals(DEV_C40)) {
			RXC40Scanner.getInstance().scanCallBack = mScanCB;
		} else {
			RX70Scanner.getInstance().scanCallBack = mScanCB;
		}
 
		
	}

	
/*	public   String bytesToHexString(byte[] src){  
	    StringBuilder stringBuilder = new StringBuilder("");  
	    if (src == null || src.length <= 0) {  
	        return null;  
	    }  
	    for (int i = 0; i < src.length; i++) {  
	        int v = src[i] & 0xFF;  
	        String hv = Integer.toHexString(v);  
	        if (hv.length() < 2) {  
	            stringBuilder.append(0);  
	        }  
	        stringBuilder.append(hv);  
	    }  
	    return stringBuilder.toString();  
	} */
	@Override
	public String getPdaBrand() {
		// TODO Auto-generated method stub
		return "rongxin";
	}

	@Override
	public int getEventFuction(int keycode) {
		// TODO Auto-generated method stub
		Log.d(TAG, "######"+TAG+"getEventFunction");
		switch(keycode){
			
		//Menu
		case KeyEvent.KEYCODE_MENU:
			return StoPdaKeyEvent.KEYCODE_MENU;
		//Home
		case KeyEvent.KEYCODE_HOME:
			return StoPdaKeyEvent.KEYCODE_HOME;
		//DELETE
		case KeyEvent.KEYCODE_DEL:
			return StoPdaKeyEvent.KEYCODE_DEL;
		//BACK
		case KeyEvent.KEYCODE_BACK:
			return StoPdaKeyEvent.KEYCODE_BACK;
		//AB切换
		case KeyEvent.KEYCODE_UNKNOWN:
			return StoPdaKeyEvent.KEYCODE_AB;
		//上
		case KeyEvent.KEYCODE_DPAD_UP:
			return StoPdaKeyEvent.KEYCODE_DPAD_UP;
		//下
		case KeyEvent.KEYCODE_DPAD_DOWN:
			return StoPdaKeyEvent.KEYCODE_DPAD_DOWN;
		//左
		case KeyEvent.KEYCODE_DPAD_LEFT:
			return StoPdaKeyEvent.KEYCODE_DPAD_LEFT;
		//右
		case KeyEvent.KEYCODE_DPAD_RIGHT:
			return StoPdaKeyEvent.KEYCODE_DPAD_RIGHT;
		//Enter
		case KeyEvent.KEYCODE_ENTER:
			return StoPdaKeyEvent.KEYCODE_ENTER;
		//拨号
		case KeyEvent.KEYCODE_CALL:
			return StoPdaKeyEvent.KEYCODE_CALL;
		//挂机
		case KeyEvent.KEYCODE_ENDCALL:
			return StoPdaKeyEvent.KEYCODE_ENDCALL;
		//VOL +
		case KeyEvent.KEYCODE_VOLUME_UP:
			return StoPdaKeyEvent.KEYCODE_VOLUME_UP;
		//VOL -
		case KeyEvent.KEYCODE_VOLUME_DOWN:
			return StoPdaKeyEvent.KEYCODE_VOLUME_DOWN;
		//num-1
		case KeyEvent.KEYCODE_1:
			return StoPdaKeyEvent.KEYCODE_1;
		//num-2
		case KeyEvent.KEYCODE_2:
			return StoPdaKeyEvent.KEYCODE_2;
		//num-3
		case KeyEvent.KEYCODE_3:
			return StoPdaKeyEvent.KEYCODE_3;
		//num-4
		case KeyEvent.KEYCODE_4:
			return StoPdaKeyEvent.KEYCODE_4;
		//num-5
		case KeyEvent.KEYCODE_5:
			return StoPdaKeyEvent.KEYCODE_5;
		//num-6
		case KeyEvent.KEYCODE_6:
			return StoPdaKeyEvent.KEYCODE_6;
		//num-7
		case KeyEvent.KEYCODE_7:
			return StoPdaKeyEvent.KEYCODE_7;
		//num-8
		case KeyEvent.KEYCODE_8:
			return StoPdaKeyEvent.KEYCODE_8;
		//num-9
		case KeyEvent.KEYCODE_9:
			return StoPdaKeyEvent.KEYCODE_9;
		//*
		case KeyEvent.KEYCODE_PERIOD:
			return StoPdaKeyEvent.KEYCODE_STAR;
		//#
		case KeyEvent.KEYCODE_POUND:
			return StoPdaKeyEvent.KEYCODE_POUND;
		//scan
		//左侧键盘
		//右侧键盘
		case KeyEvent.KEYCODE_MUTE:
			return StoPdaKeyEvent.KEYCODE_SCAN;
		case KeyEvent.KEYCODE_F1:
			return StoPdaKeyEvent.KEYCODE_F1;
		case KeyEvent.KEYCODE_F2:
			return StoPdaKeyEvent.KEYCODE_F2;
		case KeyEvent.KEYCODE_F3:
			return StoPdaKeyEvent.KEYCODE_F3;
		case KeyEvent.KEYCODE_F4:
			return StoPdaKeyEvent.KEYCODE_F4;
		default:return 0;
		}
	}

	@Override
	public String getJniVer() {
		// TODO Auto-generated method stub
		return  "rongxin-1.0.0.2";
	}

	@Override
	public int getPlatform() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void modifyAudio() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setRegister() {
		// TODO Auto-generated method stub
		
	}

}
