package sto.android.app;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.KeyEvent;

/**
 * 网风实现类
 * 
 * @author Don
 *
 */
public class WangFengJNI implements StoJNI {

	private static WangFengJNI wangfengJNI;
	private static String TAG = "wangfengJNI";
	private Context mContext;
	ScanCallBack mScanCB;
	String mScanCode;
	private static final String NLS_MT60 = "NLS-MT60";
	private static final String MT60 = "MT60";
	private String model;
	public static String year = "";
	public static String month = "";
	public static String day = "";
	public static String hour = "";
	public static String minute = "";
	public static String second = "";
	public String UPtime;// 从服务器获取的时间

	//扫描相关状态
	public static boolean m_isPowerOn = false;
	public static boolean m_isScanning = false;
	public static int mScanPower;
	public static int mScanMode;
	public static int mScanAutoEnter;
	public static int mScanNotySnd;
	public static int mScanNotyVib;
	public static int mScanNotyLed;
	public static int mScanTrigMode;
	
	public int mStatusBarExpand;
	
	//扫描设置项
	public static final int SCANNER_CFG_SCAN_ENABLE = 1;
	public static final int SCANNER_CFG_SCAN_MODE = 2;
	public static final int SCANNER_CFG_AUTO_ENTER = 3;
	public static final int SCANNER_CFG_NOTY_SND = 4;
	public static final int SCANNER_CFG_NOTY_VIB = 5;
	public static final int SCANNER_CFG_NOTY_LED = 6;
	public static final int SCANNER_CFG_TRIG_MODE = 7;
	
	private WangFengJNI(Context context) {
		model = android.os.Build.MODEL;
		mContext = context;
	}

	public static WangFengJNI getInstance(Context context) {
		if (wangfengJNI == null) {
			wangfengJNI = new WangFengJNI(context);
		}
		return wangfengJNI;
	}

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

	}

	@Override
	public void SetScannerOn() {
		IntentFilter mFilter = new IntentFilter("ACTION_BAR_SCAN");
		if (mContext != null) {
			mContext.registerReceiver(mReceiver, mFilter);
		}

		Intent tmpIntent = new Intent();
		tmpIntent = new Intent("ACTION_STO_EXTRA_POWER_ON");
		mContext.sendBroadcast(tmpIntent);
		m_isPowerOn = true;
	}

	private BroadcastReceiver mReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			final String scanResult = intent.getStringExtra("EXTRA_SCAN_DATA");
			final String scanStatus = intent.getStringExtra("EXTRA_SCAN_STATE");
				if ("ok".equals(scanStatus)) {
					// 成功
					ScanResults(scanResult, 1);
				} else {
					// 失败
					ScanResults(scanResult, 0);
				}
			

		}
	};

	@Override
	public void SetScannerStart() {
		// TODO Auto-generated method stub
		if (m_isPowerOn == true) {
			Intent tmpIntent = new Intent("ACTION_BAR_TRIGSCAN");
			if (mContext != null) {
				mContext.sendBroadcast(tmpIntent);
				m_isScanning = true;
			}
		}

	}

	@Override
	public void SetScannerStop() {
		if (m_isPowerOn == true) {
			Intent tmpIntent = new Intent("ACTION_BAR_TRIGSCAN");
			if (mContext != null) {
				mContext.sendBroadcast(tmpIntent);
				m_isScanning = false;
			}
		}
	}

	@Override
	public void SetScannerOff() {
		// TODO Auto-generated method stub
		Intent tmpIntent = new Intent("ACTION_STO_EXTRA_POWER_OFF");
		if (mContext != null) {
			mContext.sendBroadcast(tmpIntent);
		}
		m_isPowerOn = false;
                m_isScanning = false;
	}

	@Override
	public int SetScannerPara(int para, int value) {
		// TODO Auto-generated method stub
                int ret = 0;
		        Intent tmpIntent = new Intent("ACTION_BAR_SCANCFG");
      
                switch(para){
                  case SCANNER_CFG_SCAN_ENABLE:
                         mScanPower = value;
                         tmpIntent.putExtra("EXTRA_SCAN_POWER", mScanPower);     
                         break;   
                  case SCANNER_CFG_SCAN_MODE:
                         mScanMode = value;
                         tmpIntent.putExtra("EXTRA_SCAN_MODE", mScanMode);     
                         break;  
                  case SCANNER_CFG_AUTO_ENTER:
                         mScanAutoEnter = value;
                         tmpIntent.putExtra("EXTRA_SCAN_AUTOENT", mScanAutoEnter);     
                         break;  
                  case SCANNER_CFG_NOTY_SND:
                         mScanNotySnd = value;
                         tmpIntent.putExtra("EXTRA_SCAN_NOTY_SND", mScanNotySnd);     
                         break;
                  case SCANNER_CFG_NOTY_VIB:
                         mScanNotyVib = value;
                         tmpIntent.putExtra("EXTRA_SCAN_NOTY_VIB", mScanNotyVib);     
                         break;
                  case SCANNER_CFG_NOTY_LED:
                         mScanNotyLed = value;
                         tmpIntent.putExtra("EXTRA_SCAN_NOTY_LED", mScanNotyLed);     
                         break;
                  case SCANNER_CFG_TRIG_MODE:
                         mScanTrigMode = value;
                         tmpIntent.putExtra("EXTRA_TRIG_MODE", mScanTrigMode);     
                         break;
                 
                  default:
                         ret = -1;
                         break;
                }

		if (mContext != null && ret != -1) {
			mContext.sendBroadcast(tmpIntent);
		}
		return ret;
	}

	@Override
	public int GetScannerPara(int para) {
		// TODO Auto-generated method stub
                int ret;
                switch(para){
                  case SCANNER_CFG_SCAN_ENABLE:
                         ret = mScanPower;     
                         break;   
                  case SCANNER_CFG_SCAN_MODE:
                         ret = mScanMode;     
                         break;  
                  case SCANNER_CFG_AUTO_ENTER:
                         ret = mScanAutoEnter;    
                         break;  
                  case SCANNER_CFG_NOTY_SND:
                         ret = mScanNotySnd;   
                         break;
                  case SCANNER_CFG_NOTY_VIB:
                         ret = mScanNotyVib;     
                         break;
                  case SCANNER_CFG_NOTY_LED:
                         ret = mScanNotyLed;     
                         break;
                  case SCANNER_CFG_TRIG_MODE:
                         ret = mScanTrigMode;     
                         break;
                 
                  default:
                         ret = -1;
                         break;
                }
		return ret;
	}

	@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() {
		Intent tmpIntent = new Intent("ACTION_BAR_TRIGSCAN");
		if (mContext != null) {
			mContext.sendBroadcast(tmpIntent);
                        m_isScanning = true;
		}
	}

	@Override
	public void SetScannerTimerOut(int sec) {
		Intent intent = new Intent("ACTION_BAR_TRIGSCAN");
		intent.putExtra("timeout", sec);// 单位为秒，值为int 类型，且不超过9 秒
		if (mContext != null) {
			mContext.sendBroadcast(intent);// TODO Auto-generated method stub
                        m_isScanning = true;
		}
	}

	@Override
	public boolean GetScannerIsScanning() {

		return m_isScanning;
	}

	@Override
	public int getmScanModel() {

		return Settings.System.getInt(mContext.getContentResolver(), "scanner_barcode_model", 0);
	}

	@Override
	public void SetSystemReboot() {
		// TODO Auto-generated method stub
		Intent it = new Intent("android.intent.action.SET_REBOOT");
		if (mContext != null) {
			mContext.sendBroadcast(it);// TODO Auto-generated method stub
		}
	}

	@Override
	public void SetSystemShutdown() {
		// TODO Auto-generated method stub
		Intent it = new Intent("android.intent.action.SET_SHUTDOWN");
		if (mContext != null) {
			mContext.sendBroadcast(it);// TODO Auto-generated method stub
		}
	}


	@Override
	public void SetSystemTime(String mm) {
			Intent it = new Intent("android.intent.action.SET_TIME");
			it.putExtra("time", String.valueOf(mm));
			if (mContext != null) {
				mContext.sendBroadcast(it);// TODO Auto-generated method stub
			}
		

	}

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

	}

	@Override
	public String GetMachineCode() {
		TelephonyManager tm = (TelephonyManager) mContext.getSystemService(mContext.TELEPHONY_SERVICE);
		if (tm.getDeviceId() == null) {
			return "巴枪ID为空";
			// return "770098156987532";
		} else {
			Log.i("============id \t", tm.getDeviceId());
			return tm.getDeviceId();// 359996020119782
		}
	}

	@Override
	public void disableKeypad() {
			Intent intent = new Intent("com.android.action.HOMEKEY_SWITCH_STATE");// 屏蔽Home键
			intent.putExtra("enable", false);
			if (mContext != null) {
				mContext.sendBroadcast(intent);
			}		
	}

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

	}

	@Override
	public void enableKeypad() {
			Intent intent = new Intent("com.android.action.HOMEKEY_SWITCH_STATE");// 打开Home键
			intent.putExtra("enable", true);
			if (mContext != null) {
				mContext.sendBroadcast(intent);
			}
		

	}

	@Override
	public void enableKeydownTone() {
		// TODO Auto-generated method stub
			Intent intent = new Intent("com.android.action.KEYDOWN_TONE_STATE");
			intent.putExtra("enable", true);
			if (mContext != null) {
				mContext.sendBroadcast(intent);
			}
		
	}

	@Override
	public int GetStatusKeydownTone() {
		// TODO Auto-generated method stub
		return Settings.System.getInt(mContext.getContentResolver(),Settings.System.SOUND_EFFECTS_ENABLED, 1);
	}

	@Override
	public void disableKeydownTone() {
		// TODO Auto-generated method stub
			Intent intent = new Intent("com.android.action.KEYDOWN_TONE_STATE");
			intent.putExtra("enable", false);
			if (mContext != null) {
				mContext.sendBroadcast(intent);
			}
		

	}

	@Override
	public void TurnOffStatusBarExpand() {
		Intent intent = new Intent("com.android.action.STATUSBAR_SWITCH_STATE");
		intent.putExtra("enable", false);
		if (mContext != null) {
			mContext.sendBroadcast(intent);
			mStatusBarExpand = 0;
		}

	}

	@Override
	public int GetStatusBarExpand() {
		// TODO Auto-generated method stub
	       //return SystemProperties.getInt("persist.sys.vendor.STATUSBAR", 0);
		   return mStatusBarExpand;
	}

	@Override
	public void TurnOnStatusBarExpand() {
		Intent intent = new Intent("com.android.action.STATUSBAR_SWITCH_STATE");
		intent.putExtra("enable", true);
		if (mContext != null) {
			mContext.sendBroadcast(intent);
			mStatusBarExpand = 1;
		}
	}

	@Override
	public void TurnOffUSBDebug() {
		// TODO Auto-generated method stub
		Intent intent = new Intent("android.intent.action.USB_DEBUG_STATE");
		intent.putExtra("enable", false);
		if (mContext != null) {
			mContext.sendBroadcast(intent);
		}
	}

	@Override
	public int GetStatusUSBDebug() {
		// TODO Auto-generated method stub
		return Settings.System.getInt(mContext.getContentResolver(),Settings.Global.ADB_ENABLED, 1);
	}

	@Override
	public void TurnOnUSBDebug() {
		// TODO Auto-generated method stub
		Intent intent = new Intent("android.intent.action.USB_DEBUG_STATE");
		intent.putExtra("enable", true);
		if (mContext != null) {
			mContext.sendBroadcast(intent);
		}

	}

	@Override
	public void TurnOffInstallManager() {
		// TODO Auto-generated method stub
		Intent intent = new Intent("android.intent.action.SET_INSALL_MANAGER");
		intent.putExtra("enable", false);
		if (mContext != null) {
			mContext.sendBroadcast(intent);
		}
	}

	@Override
	public int GetStatusInstallManager() {
		// TODO Auto-generated method stub
                return Settings.System.getInt(mContext.getContentResolver(), "install_manager",1);
	}

	@Override
	public void TurnOnInstallManager() {
		// TODO Auto-generated method stub
		Intent intent = new Intent("android.intent.action.SET_INSALL_MANAGER");
		intent.putExtra("enable", true);
		if (mContext != null) {
			mContext.sendBroadcast(intent);
		}
	}

	@Override
	public void TurnOnScreenLock() {
		// TODO Auto-generated method stub
		Intent intent = new Intent("android.intent.action.LOCKSCREEN_STATE");
		intent.putExtra("enable", true);
		if (mContext != null) {
			mContext.sendBroadcast(intent);
		}
	}

	@Override
	public int GetStatusScreenLock() {
		// TODO Auto-generated method stub
 		return Settings.System.getInt(mContext.getContentResolver(), "lockscreen_state",1);
	}

	@Override
	public void TurnOffScreenLock() {
		// TODO Auto-generated method stub
		Intent intent = new Intent("android.intent.action.LOCKSCREEN_STATE");
		intent.putExtra("enable", false);
		if (mContext != null) {
			mContext.sendBroadcast(intent);
		}
	}

	@Override
	public int GetStatusWakeUp() {
		// TODO Auto-generated method stub
                return Settings.System.getInt(mContext.getContentResolver(), "anykey_wakeup",0);
	}

	@Override
	public void CfgPowerKeyWakeUp() {
		// TODO Auto-generated method stub
		Intent intent = new Intent("android.intent.action.ANYKEY_WAKEUP_STATE");
		intent.putExtra("enable", false);
		if (mContext != null) {
			mContext.sendBroadcast(intent);
		}
	}

	@Override
	public void CfgAnyKeyWakeUp() {
		// TODO Auto-generated method stub
		Intent intent = new Intent("android.intent.action.ANYKEY_WAKEUP_STATE");
		intent.putExtra("enable", true);
		if (mContext != null) {
			mContext.sendBroadcast(intent);
		}
	}

	@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
		Intent intent = new Intent("android.intent.action.GPS_STATE");
		intent.putExtra("enable", set);
		if (mContext != null) {
			mContext.sendBroadcast(intent);
		}
	}

	@Override
	public void TurnOnOffBrowser(boolean set) {
		// TODO Auto-generated method stub
		Intent intent = new Intent("android.intent.action.BROWSER_STATE");
		intent.putExtra("enable", set);
		if (mContext != null) {
			mContext.sendBroadcast(intent);
		}
	}

	

	@Override
	public void setmScanCB(ScanCallBack mScanCB) {
		// TODO Auto-generated method stub
		this.mScanCB = mScanCB;

	}

	public void ScanResults(String str, int type) {

		System.out.println("#############扫描结果" + str);
		if (mScanCB != null) {
			mScanCB.onScanResults(str, type);
			mScanCB.onScanResults(str);
			m_isScanning = false;
		}
	}

	public void ScanResults(String str) {

		System.out.println("#############扫描结果" + str);
		if (mScanCB != null) {
			mScanCB.onScanResults(str);
				m_isScanning = false;
		}
	}

	@Override
	public String getPdaBrand() {
		// TODO Auto-generated method stub
		return "wangfeng";
	}

	@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;
		//scan
		case KeyEvent.KEYCODE_F9:
		//左侧键盘
		case KeyEvent.KEYCODE_F10:
		//右侧键盘
		case KeyEvent.KEYCODE_F11:
				return StoPdaKeyEvent.KEYCODE_SCAN;
		//Back
		case KeyEvent.KEYCODE_BACK:
				return StoPdaKeyEvent.KEYCODE_BACK;
		//Enter
		case KeyEvent.KEYCODE_ENTER:
				return StoPdaKeyEvent.KEYCODE_ENTER;
		//拨号
		case KeyEvent.KEYCODE_CALL:
				return StoPdaKeyEvent.KEYCODE_CALL;
		//F1
		case KeyEvent.KEYCODE_F1:
			return StoPdaKeyEvent.KEYCODE_F1;
		//F3
		case KeyEvent.KEYCODE_VOLUME_DOWN:
			return StoPdaKeyEvent.KEYCODE_F3;
		//上
		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;
		//F2
		case KeyEvent.KEYCODE_F2:
			return StoPdaKeyEvent.KEYCODE_F2;
		//F4
		case KeyEvent.KEYCODE_VOLUME_UP:
			return StoPdaKeyEvent.KEYCODE_F4;
		//挂机
		case KeyEvent.KEYCODE_ENDCALL:
			return StoPdaKeyEvent.KEYCODE_ENDCALL;
		//0/空格
		case KeyEvent.KEYCODE_0:
			return StoPdaKeyEvent.KEYCODE_0;
		//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;
		//1-AB
		case KeyEvent.KEYCODE_F6:
			return StoPdaKeyEvent.KEYCODE_AB;
		//*
		case KeyEvent.KEYCODE_PERIOD:
			return StoPdaKeyEvent.KEYCODE_STAR;
		//Delete
		case KeyEvent.KEYCODE_DEL:
			return StoPdaKeyEvent.KEYCODE_DEL;
		//TAB
		case KeyEvent.KEYCODE_TAB:
			return StoPdaKeyEvent.KEYCODE_TAB;
		//#
		case KeyEvent.KEYCODE_MINUS:
			return StoPdaKeyEvent.KEYCODE_POUND;
		default:return 0;
		}
	}

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

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

	@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 void modifyAudio() {
		// TODO Auto-generated method stub
		
	}

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

}
