package cn.jymf.scan.idatascanlibrary;

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;

/**
 * @Description: java类作用描述 idata 手持机
 * @Author: winstronzeng
 * @CreateDate: 2021/06/11 9:28
 * @UpdateUser: 更新者：
 * @UpdateDate: 2021/06/11 9:28
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class IdataScanSetting {
    private final static String MANUFACTURER = "iData";
    private final static String MANUFACTURER_AUTOID = "AUTOID";
    private final static String MODEL = "T1";
    private final static String MODEL2 = "Z1";
    private static ScannerInterface scanner;
    private String acceptKey = "value";
    private ScanCodeBroadcastReceiver receiver;
    private ScanCodeResult scanCodeResult;
    private String broadCastName = "android.intent.action.SCANRESULT";
    private boolean isShow = true;

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

    private void initScanner(Context context) {
        scanner = new ScannerInterface(context);

        //scanner.open();//注意：扫描引擎上电，该接口请勿频繁调用，频繁关闭串口会导致程序卡死
        //scanner.resultScan();//注意：恢复iScan默认设置，频繁重置串口会导致程序卡死
        //scanner.close();//注意：恢复关闭扫描引擎电源，频繁重置串口会导致程序卡死

        //***********重要
        scanner.setOutputMode(1);
        /**设置扫描结果的输出模式，参数为0和1：
         * 0为模拟输出，同时广播扫描数据（在光标停留的地方输出扫描结果同时广播扫描数据）;
         * 1为广播输出（只广播扫描数据）；
         * 2为模拟按键输出；
         * */

//        //***********重要
//        //扫描结果的意图过滤器action一定要使用"android.intent.action.SCANRESULT"
//        intentFilter = new IntentFilter();
//        intentFilter.addAction(RES_ACTION);
//
//        //**********重要
//        //注册广播接受者
//        scanReceiver = new ScannerResultReceiver();
//        registerReceiver(scanReceiver, intentFilter);
    }

    public IdataScanSetting onCreate(Activity activity, ScanCodeResult 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)|| model.contains(MODEL2)) {
            initScanner(activity.getApplicationContext());
            this.scanCodeResult = scanCodeResult;
            receiver = new ScanCodeBroadcastReceiver();
            IntentFilter intentFilter = new IntentFilter(broadCastName);
            activity.registerReceiver(receiver, intentFilter);
        }
        return this;
    }


    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);
            closeScan();
        }
    }

    /**
     * 是否是手持机
     *
     * @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)|| model.contains(MODEL2);
    }

    /**
     * 调用手持机红外扫描
     */
    public static void startHandScan() {
        if (null != scanner) {
            scanner.scan_start();
        }
    }

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

    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.scanCodeIdataResultByBroadcastReceiver(codeSource);
                        }
                    }
                }
            }
        }
    }
}
