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 com.olc.scan.ScanManager;

/**
 * @Description: java类作用描述 富力叶手持机 scan 设置
 * @Author: winstronzeng
 * @CreateDate: 2020/4/15 16:44
 * @UpdateUser: 更新者：
 * @UpdateDate: 2020/4/15 16:44
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class FlyScanSetting {
    private final static String MANUFACTURER = "alps";
    private final static String MODEL = "Android Handheld Terminal";
    private final String SCAN_KEY = "com.barcode.sendBroadcast";
    private ScanCodeBroadcastReceiver receiver;
    private ScanCodeResult scanCodeResult;

    public FlyScanSetting onCreate(Activity activity, ScanCodeResult scanCodeResult) {
        String manufacturer = Build.MANUFACTURER;
        String model = Build.MODEL;
        if (manufacturer.contains(MANUFACTURER) || model.contains(MODEL)) {
            setScanModel(activity);
        }
        this.scanCodeResult = scanCodeResult;
        receiver = new ScanCodeBroadcastReceiver();
        final IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(SCAN_KEY);
        activity.getApplicationContext().registerReceiver(receiver, intentFilter);
        return this;
    }

    private void setScanModel(Activity activity) {
        String serviceName = "olc_service_scan";
        ScanManager sm = (ScanManager) activity.getSystemService(serviceName);
        if (null == sm) {
            return;
        }
        sm.setScanSwitchLeft(true);
        sm.setScanSwitchMiddle(true);
        sm.setScanSwitchRight(true);
        sm.setBarcodeReceiveModel(2);
    }

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

    private class ScanCodeBroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (null != intent) {
                if (SCAN_KEY.equals(intent.getAction())) {
                    String codeKey = "BARCODE";
                    String codeSource = intent.getStringExtra(codeKey);
                    if (null != scanCodeResult) {
                        scanCodeResult.scanCodeResultByBroadcastReceiver(codeSource);
                    }
                }
            }
        }
    }

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

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

    /**
     * 调用手持机红外扫描
     */
    public static void startHandScan(Context activity) {
        Intent intent = new Intent();
        intent.setAction("com.barcode.sendBroadcastScan");
        activity.sendBroadcast(intent);
    }
}