package sto.android.app;

import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.zltd.industry.ScannerManager;
import com.zltd.industry.ScannerManager.IScannerStatusListener;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ZltdJNI implements StoJNI {
	private static String TAG = "ZltdJNI";
	private Context mContext;
	public static final int SINGLE_SCAN_MODE = 1;
	public static final int CONTINUOUS_SCAN_MODE = 2;
	public static boolean m_isPowerOn = false;
    public static boolean m_isScanning = false;
    private ScannerManager mScannerManager;
	private static ZltdJNI zltdJni;
	private  boolean continousScanMode=false;
	ScanCallBack mScanCB;
	private static int version =0;
	Method getMethod;
	String imei;
	private ZltdJNI(Context context) {
		this.mContext = context;
	}

	public static ZltdJNI getInstance(Context context) {
		if (zltdJni == null) {
			zltdJni = new ZltdJNI(context);
			version = Integer.valueOf(android.os.Build.VERSION.SDK);
		}

		return zltdJni;
	}
	/**
	 *
	 * @param
	 * @param
	 */
	private void continuousScanMode(boolean continuousScan) {
		// TODO Auto-generated method stub
		if(mScannerManager==null){
			mScannerManager = ScannerManager.getInstance();
		}
		if (continuousScan
				&& mScannerManager.getScanMode() == ZltdJNI.SINGLE_SCAN_MODE) {
			mScannerManager.setScanMode(ScannerManager.SCAN_CONTINUOUS_MODE);
			mScannerManager.startContinuousScan();
			continousScanMode=true;
		} else if (!continuousScan
				&& mScannerManager.getScanMode() == ZltdJNI.CONTINUOUS_SCAN_MODE) {
			mScannerManager.setScanMode(ScannerManager.SCAN_SINGLE_MODE);
			mScannerManager.stopContinuousScan();
			continousScanMode=false;
		}
	}

	/**
	 * 
	 * @param
	 * @param
	 */

	private void scanSwitch(boolean scanSwitch) {

		// TODO Auto-generated method stub
		mScannerManager.scannerEnable(scanSwitch);
	}
	@Override
	public void SetScannerOn() {
		// TODO Auto-generated method stub
		mScannerManager = ScannerManager.getInstance();
		scanSwitch(true);
		mScannerManager.setScanMode(ScannerManager.SCAN_SINGLE_MODE);
		continuousScanMode(true);
		m_isPowerOn = true;
		mScannerManager.addScannerStatusListener(mIScannerStatusListener);
		m_isScanning = true;
	}

	private IScannerStatusListener mIScannerStatusListener = new IScannerStatusListener() {

		@Override
		public void onScannerStatusChanage(int arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onScannerResultChanage(final byte[] arg0) {
			// TODO Auto-generated method stub
			// mSoundUtils.success();
			if (arg0 != null && !arg0.equals("")) { // �����ֽ�����Ĳ�Ϊ�գ��Ҷ���Ϊnull
				String result = new String(arg0);// ���ֽ�����ת��Ϊ�ַ���
				if (mScanCB != null) {
					mScanCB.onScanResults(result);
				}

			}
		}
	};
	
	
	
	@Override
	public void SetScannerStart() {
		//SetScannerOn();
	//	mScannerManager.setScanMode(ScannerManager.SCAN_SINGLE_MODE);
	//	if(m_isPowerOn) {
	//		mScannerManager.addScannerStatusListener(mIScannerStatusListener);
	//		m_isScanning = true;
	//	}
	}
	@Override
	public void SetScannerStop() {
		if(m_isPowerOn) {
		if (mScannerManager != null) {
			//scanSwitch(false);
			//mScannerManager.removeScannerStatusListener(mIScannerStatusListener);
			if(mScannerManager.getScanMode() == ZltdJNI.CONTINUOUS_SCAN_MODE){
				mScannerManager.stopContinuousScan();
			}
			mScannerManager=null;
		}
		m_isPowerOn = false;
		m_isScanning = false;
		
	 }
	}

	@Override
	public void SetScannerOff() {
		if (mScannerManager != null) {
			//scanSwitch(false);
			mScannerManager.removeScannerStatusListener(mIScannerStatusListener);
			if(mScannerManager.getScanMode() == ZltdJNI.CONTINUOUS_SCAN_MODE){
				mScannerManager.stopContinuousScan();
			}
		}
		m_isPowerOn = false;
		m_isScanning = false;
	}

	@Override
	public int SetScannerPara(int para, int value) {
		// TODO Auto-generated method stub
		return 0;
	}

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

	@Override
	public String SetScannerPara(String para, String value) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String GetScannerPara(String para) {
		// TODO Auto-generated method stub
		return null;
	}

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

	}

	@Override
	public void SetScannerTimerOut(int sec) {
		// TODO Auto-generated method stub
		continuousScanMode(true);
	}

	@Override
	public boolean GetScannerIsScanning() {
		// TODO Auto-generated method stub
		return m_isScanning;
	}

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

	}

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

	}

	@Override
	public void SetSystemTime(String mm) {
		// TODO Auto-generated method stub
		Intent intentdatatime = new Intent("android.intent.action.SET_DATETIME");
		intentdatatime.putExtra("datetime", mm);
		mContext.sendBroadcast(intentdatatime);
	}

	@Override
	public void SetMachineCode(String code) {
		// TODO Auto-generated method stub
	}

	@Override
	public String GetMachineCode() {
		
		TelephonyManager tm = (TelephonyManager) mContext
				.getSystemService(Context.TELEPHONY_SERVICE);
		if (version<20){
			imei=tm.getDeviceId();
			return imei;
		}else{
			try {
				Method getMethod=Class.forName("sto.android.os.SystemProperties").getMethod("get", String.class,String.class);
				imei=(String)getMethod.invoke(null, "gsm.mtk.imei1","");
				
			} catch (NoSuchMethodException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {                                        
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return imei;
		}
	}

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

	}

	@Override
	public void disableKeypadEnableCustom(String customstr) {
		// TODO Auto-generated method stub

	}

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

	}

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

	}

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

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

	}

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

	}

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

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

	}

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

	}

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

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

	}

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

	}

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

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

	}

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

	}

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

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

	}

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

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

	}

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

	}

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

	}

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

	}

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

	@Override
	public void StoJNI_InstallApkWithSilence(String apkPath,
			boolean runAfterFinished) {
		// TODO Auto-generated method stub

	}

	@Override
	public void TurnOnOffGPS(boolean set) {
		// TODO Auto-generated method stub

	}

	@Override
	public void TurnOnOffBrowser(boolean set) {
		// TODO Auto-generated method stub

	}

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

	}

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

	}

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

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

	}

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

	}

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

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

	}

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

	}

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

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

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

	@Override
	public int getEventFuction(int keycode) {
		Log.d(TAG, "######" + TAG + "getEventFunction keycode= " + keycode);
        switch(keycode) {
        case 19:
            return 19;
        case 20:
            return 20;
        case 21:
            return 21;
        case 22:
            return 22;
        case 24:
            return 24;
        case 25:
            return 25;
        case 66:
            return 66;
        case 67:
            return 67;
        case 82:
            return 82;
        case 119:
            return 119;
        case 249:
            return 110;
        
        default:
            return keycode;
        }
	}

	public String getPdaBrand() {
		return "ZLTD";
	}

	@Override
	public void setmScanCB(ScanCallBack scanCallBack) {

		this.mScanCB = scanCallBack;
	}

	@Override
	public void SetGPIOStatusJNI(int var1, int var2) {
		// TODO Auto-generated method stub

	}

	@Override
	public int getmScanModel() {
		// 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
		
	}

	/**
	 * 设置扫描头连扫单扫
	 * @param isContinue
	 */
/*	@Override
	public void setScanContinue(boolean isContinue) {

	}*/
}
