package com.hh.lib_dx_multi_qr;

import static com.hh.lib_base.Constants.LogcatValue.LOG_NODE_QR;

import com.hh.ib_base_qr.BaseQRScan;
import com.hh.ib_base_qr.QRInitListener;
import com.hh.lib_base.BaseApplication;
import com.hh.lib_base.log.UploadLogUtils;
import com.hh.lib_dx_base_multi.DxCardReader;
import com.hh.lib_dx_base_multi.DxInitListener;
import com.hh.lib_dx_base_multi.listener.qr.QrResultListener;

/**
 * Description: 东信多合一二维码模块
 * @author xwh
 * time 2024/9/30
 **/
public class DxMultiQRScan extends BaseQRScan {

    private static volatile DxMultiQRScan instance = null;

    @Override
    public void onInit(QRInitListener listener) {
        init(listener);
        switch (DxCardReader.getInstance().getStatusEnum()){
            case SUCCESS:

                if(listener!=null){
                    listener.onInitSuccess();
                }
                break;
            case  FAIL:
                if(listener!=null){
                    listener.onInitFail(-1,"");
                }
                break;
            case DEFAULT:

                break;
        }
        DxCardReader.getInstance().setQrResultListener(new QrResultListener() {
            @Override
            public void onSuccess(String qrCode) {
                onScanSuccess(qrCode);
            }

            @Override
            public void onFail(String code, String errMsg) {
                 onScanFail(errMsg);
            }
        });
    }

    /**
     *
     * @author xwh
     * Time 2024/10/10
     * Description: 初始化监听
     *
     **/
    private void init(QRInitListener listener){
        DxCardReader.getInstance().init(BaseApplication.getContext(), DxMultiQRScan.class.getName(), new DxInitListener() {
            @Override
            public void onLogInfo(String msg) {

            }

            @Override
            public void initSuccess() {
                if(listener!=null){
                    listener.onInitSuccess();
                }

            }

            @Override
            public void initFailed(String code, String msg) {
                if(listener!=null){
                    listener.onInitFail(-1,"");
                }
            }
        });
    }


    //提供一个公共的访问方法，用于获取该类的唯一实例
    public static DxMultiQRScan getInstance() {
        //如果instance为空，就进行实例化
        if (instance == null) {
            //保证多线程下只有一个线程进行实例化
            synchronized (DxMultiQRScan.class) {
                //第二次判断，避免多线程下创建多个实例
                if (instance == null) {
                    instance = new DxMultiQRScan();
                }
            }
        }
        return instance;
    }


    @Override
    public void onScanSuccess(String qrCode) {
        if(qrResultCallback!=null){
            qrResultCallback.onScanSuccess(qrCode);
        }
    }

    @Override
    public void onScanFail(String errMsg) {
        if(qrResultCallback!=null){
            qrResultCallback.onScanFail();
        }
    }


    @Override
    public void onRelease() {
        DxCardReader.getInstance().setQrResultListener(null);
        qrResultCallback = null;
        instance = null;
    }
}
