package cilico.tools.barcode;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.util.Log;

import com.seuic.scanner.Scanner;
import com.seuic.scanner.ScannerFactory;

/**
 * @Description: java类作用描述 东大手持机 scan 设置
 * @Author: winstronzeng
 * @CreateDate: 2020/4/15 16:44
 * @UpdateUser: 更新者：
 * @UpdateDate: 2020/4/15 16:44
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class SeuicScanSetting {
	private final static String MANUFACTURER = "SEUIC";
	private final static String MANUFACTURER_AUTOID = "AUTOID";
	private final static String MODEL = "CRUISE Ge";
	private String acceptKey = "BARCODE";
	private ScanCodeBroadcastReceiver receiver;
	private SeuicScanCodeResult scanCodeResult;
	private String broadCastName = "com.lib.seuiclibrary.broadcast";
	private boolean isShow = true;
	private static Scanner scanner;

	public static void init(Context context) {
		if (isHandPhone()) {
			scanner = ScannerFactory
					.getScanner(context.getApplicationContext());
	
			boolean open = scanner.open();
			Log.e("open", "open=" + open);
		}
	}

	public static void closeScan() {
		if (null != scanner) {
			scanner.close();
		}
	}

	public SeuicScanSetting onCreate(Activity activity,
                                     SeuicScanCodeResult scanCodeResult) {
		String manufacturer = Build.MANUFACTURER;
		String model = Build.MODEL;
		Log.e("MODEL", "MODEL==" + model + "，manufacturer=" + manufacturer);
		if (manufacturer.contains(MANUFACTURER)
				|| manufacturer.contains(MANUFACTURER_AUTOID)
				|| model.contains(MODEL)) {
			// 东大集成的手持机扫码
			setScanModel(activity);
			setReceiverKeyModel(activity);
			this.scanCodeResult = scanCodeResult;
			receiver = new ScanCodeBroadcastReceiver();
			IntentFilter intentFilter = new IntentFilter(broadCastName);
			activity.registerReceiver(receiver, intentFilter);
		}
		return this;
	}

	/**
	 * “FOCUS”: 焦点录入 “BROADCAST”: 广播 “EMUKEY”: 模拟按键 “CLIPBOARD”: 剪贴板
	 * 
	 * @param activity
	 */
	private void setScanModel(Activity activity) {
		Intent intent = new Intent("com.android.scanner.service_settings");
		intent.putExtra("barcode_send_mode", "BROADCAST");
		activity.sendBroadcast(intent);
	}

	/**
	 * 条码广播设置
	 */
	private void setReceiverKeyModel(Activity activity) {
		Intent intent = new Intent("com.android.scanner.service_settings");
		intent.putExtra("action_barcode_broadcast", broadCastName);
		intent.putExtra("key_barcode_broadcast", acceptKey);
		activity.sendBroadcast(intent);
	}

	public void onStop(Activity activity) {
		isShow = false;
	}

	public void onResume(Activity activity) {
		isShow = true;
	}

	public void onDestroy(Activity activity) {
		if (null != activity && null != receiver) {
			activity.unregisterReceiver(receiver);
		}
	}

	private class ScanCodeBroadcastReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			if (isShow) {
				if (null != intent) {
					if (broadCastName.equals(intent.getAction())) {
						String codeSource = intent.getStringExtra(acceptKey);
						if (null != scanCodeResult) {
							scanCodeResult
									.scanCodeSeuicResultByBroadcastReceiver(codeSource);
						}
					}
				}
			}
		}
	}

	public interface SeuicScanCodeResult {
		/**
		 * 扫描结果
		 * 
		 * @param code
		 *            code
		 */
		void scanCodeSeuicResultByBroadcastReceiver(String code);
	}

	/**
	 * 是否是手机
	 * 
	 * @return boolean
	 */
	public static boolean isHandPhone() {
		String manufacturer = Build.MANUFACTURER;
		String model = Build.MODEL;
		return manufacturer.contains(MANUFACTURER)
				|| manufacturer.contains(MANUFACTURER_AUTOID)
				|| model.contains(MODEL);
	}

	/**
	 * 调用手持机红外扫描
	 */
	public static void startHandScan() {
		if (null != scanner) {
			scanner.startScan();
			// 通过广播形式接收code
			// scanner.setDecodeInfoCallBack(new DecodeInfoCallBack() {
			// @Override
			// public void onDecodeComplete(DecodeInfo decodeInfo) {
			// scanner.stopScan();
			// if (null != scanCodeResult) {
			// scanCodeResult.scanCodeSeuicResultByBroadcastReceiver(decodeInfo.barcode);
			// }
			// }
			// });
			// Toast.makeText(context, "该机型不支持手动红外扫描请使用物理按钮！",
			// Toast.LENGTH_SHORT).show();
		}
	}
}
