package com.hh.nfc_readcard;

import android.content.Intent;

import com.hh.lib_base.BaseApplication;
import com.hh.lib_base.utils.DoubClickUtils;
import com.hh.lib_idcard_base.BaseIDCardScan;
import com.hh.lib_idcard_base.IDCardConfig;
import com.hh.lib_idcard_base.bean.IDCardError;
import com.hh.lib_idcard_base.listener.IDCardInfoLogListener;
import com.hh.lib_idcard_base.listener.ReadIDCardListener;

/**
 *
 *
 * @author xwh
 * time 2024/9/3
 * Description: NFC身份证阅读器
 **/
public class NfcIDCardScan  extends BaseIDCardScan {

    private static volatile NfcIDCardScan instance = null;

    private final int MIN_CLICK_DELAY_TIME = 3000; // 点击间隔时间，单位毫秒
    private long lastClickTime = 0;

    public volatile IDCardInfoLogListener onILogListener; //初始化

    public NfcIDCardScan(IDCardConfig config) {
        super(config);
    }


    @Override
    public void initIDCard(IDCardInfoLogListener listener) {
        onILogListener = listener;
        ReadCardManagerNfc.initEid(BaseApplication.getContext());
        if(listener!=null) {
            if (ReadCardManagerNfc.eid == null) {
                listener.initFailed(new IDCardError(-1, "在线身份证---初始化失败---eid对象为空，请初始化sdk成功后再使用sdk功能---"));
            } else {
                // 初始化成功
                listener.initSuccess();
            }
        }
    }

    @Override
    public void startPollRead(ReadIDCardListener listener) {

    }

    @Override
    public void stopPollRead() {

    }

    @Override
    public void onceReadIDCardInfo(ReadIDCardListener listener) {
        if(onDoubClick()){
            return;
        }
        Intent intent = new Intent(BaseApplication.getContext(), IDCardScanNfcActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        BaseApplication.getContext().startActivity(intent);
    }

    @Override
    public void stopOnceReadIDCardInfo() {

    }

    @Override
    public void getIDCardUid(ReadIDCardListener listener) {

    }

    @Override
    public void onRelease() {
        instance = null;
    }

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



    public  boolean onDoubClick() {
        if (System.currentTimeMillis() - lastClickTime < MIN_CLICK_DELAY_TIME) {
            // 如果两次点击小于设定的点击间隔时间，则不执行点击事件
            return true;
        }
        // 执行按钮点击后的操作
        // ...

        // 更新最后一次点击时间
        lastClickTime = System.currentTimeMillis();
        return false;

    }
}
