package com.hh.arome.common.scan.idcard.base;

import android.content.Context;

import com.hh.arome.common.scan.idcard.abstractidcard.IIDCardScan;
import com.hh.arome.common.scan.idcard.callback.IDCardInitResultCallback;
import com.hh.arome.common.scan.idcard.callback.IDCardScanResultCallback;
import com.hh.arome.common.scan.idcard.manymodels.TbIDCardScan;
import com.hh.arome.common.scan.idcard.manymodels.NfcIDCardScan;
import com.hh.arome.common.scan.idcard.manymodels.NullIDCardScan;
import com.hh.arome.common.scan.idcard.manymodels.XzxIDCardScan;
import com.hh.arome.utlis.log.AppLogUtils;


/**
 * Created by L470 on 2020/6/8.
 */

public class IDCardScanEngine
{

    Context mContext;
    IIDCardScan mIIDCardScan;
    IDCardScanResultCallback mIdCardScanResultCallback;

    private static volatile IDCardScanEngine instance;

    public static IDCardScanEngine getInstance(Context context,int idCardReaderBrand) {
        if (instance == null) {
            synchronized (IDCardScanEngine.class) {
                if (instance == null) {
                    instance = new IDCardScanEngine(context,idCardReaderBrand);
                }
            }
        }
        return instance;
    }

    int idCardReaderBrand;

    public IDCardScanEngine(Context context,int idCardReaderBrand) {
        this.mContext = context;

        mIIDCardScan = getIDCardScanEngine(context,idCardReaderBrand);
    }

    /**
     * 根据机型，初始化不同的身份证读取实现
     *
     * @param context
     * @return
     */
    public IIDCardScan getIDCardScanEngine(Context context,int idCardReaderBrand) {

//        String model = Build.MODEL;
//        AppLogUtils.e("model:" + model);
//
//        if (Constants.DeviceModel.D2.equals(model)) {
//            // D2
//            return D2IDCardScan.getInstance(context);
//        } else if (Constants.DeviceModel.K8.equals(model)) {
//            // K8
//            return K8IDCardScan.getInstance(context);
//        } else {
//            // 未适配的设备，空实现
//            return NullIDCardScan.getInstance(context);
//        }

        AppLogUtils.e("idCardReaderBrand:" + idCardReaderBrand);
        /**
         * NFC(1, "NFC在线阅读器"),
         * TB(2, "天波阅读器"),
         * DX(3, "东信阅读器"),
         * XZX(4, "新中新阅读器"),
         * DK(5, "德卡阅读器"),
         * JL(6, "精伦阅读器"),
         * HS(7, "华视阅读器"),
         * ZAWL(8, "中安未来离线护照机阅读器模块"),
         */
        if (1 == idCardReaderBrand) {
            // NFC在线阅读器，适用于K8
            return NfcIDCardScan.getInstance(context);
        } else if (2 == idCardReaderBrand) {
            // 天波阅读器，适用于D2
            return TbIDCardScan.getInstance(context);
        } else if (4 == idCardReaderBrand) {
            // 新中新
            return XzxIDCardScan.getInstance(context);
        } else {
            // 未适配的设备，空实现
            return NullIDCardScan.getInstance(context);
        }


    }

    // 初始化
    IDCardInitResultCallback idCardInitResultCallback;

    public void initIDCard(IDCardInitResultCallback idCardInitResultCallback) {

        this.idCardInitResultCallback = idCardInitResultCallback;

        mIIDCardScan.initIDCard(idCardInitResultCallback);
    }



    // 开始扫拿结果
    public void startScanIDCard(IDCardScanResultCallback idCardScanResultCallback) {

        this.mIdCardScanResultCallback = idCardScanResultCallback;

        mIIDCardScan.startIDCardScan(idCardScanResultCallback);
    }

    public void resumeScanIDCard() {
        AppLogUtils.e("IDCardScanEngine--resumeScanIDCard");
        mIIDCardScan.resumeIDCardScan();
    }

    public void pauseScanIDCard(){
        AppLogUtils.e("IDCardScanEngine--pauseScanIDCard");
        mIIDCardScan.pauseIDCardScan();
    }

    public void stopScanIDCard() {
        AppLogUtils.e("IDCardScanEngine--stopScanIDCard");
        mIIDCardScan.stopIDCardScan();
    }

    public void destoryIDCardScan() {
        AppLogUtils.e("IDCardScanEngine--destoryIDCardScan");
        mIIDCardScan.destoryIDCardScan();
        instance = null;
    }

}
