package com.hh.lib_social_security_card;

import android.content.Context;
import android.os.CountDownTimer;

import com.hh.lib_base.BaseApplication;
import com.hh.lib_base.log.AppLogUtils;
import com.hh.lib_dx_base_multi.DxCardReader;
import com.hh.lib_dx_base_multi.DxInitListener;
import com.hh.lib_dx_base_multi.SimpleDxInitListener;
import com.hh.lib_dx_base_multi.bean.sscard.BaseSSCardBean;
import com.hh.lib_dx_base_multi.bean.sscard.SSCardConfig;
import com.hh.lib_dx_base_multi.listener.sscard.SSCardResultListener;
import com.hh.lib_dx_base_multi.listener.sscard.SimpleSSCardResultListener;



/**
 * @Description:
 * @author: Xia WeiHao
 * @date: 2024/6/24
 */
public class DX_SSCardReader extends BaseSSCardReader {


    private volatile static DX_SSCardReader reader;


    private volatile String mOldSSCardStr = "-2";
    private volatile long recordTime = 0L;

    private volatile SSCardResultListener onceLinstenr;

    private long readIDCardTotalTime = 15 * 1000;
    private volatile CountDownTimer readIDCardCountDownTimer; //单次读卡超时倒计时


    public static DX_SSCardReader getInstance(SSCardConfig cardConfig) {
        if (reader == null) {
            synchronized (DX_SSCardReader.class) {
                if (reader == null) {
                    reader = new DX_SSCardReader();
                    SSCardConfig config=cardConfig;
                    if (cardConfig == null) {
                        config = new SSCardConfig();
                        config.setnType(1);
                        config.setSlotPSAM(0x11);
                    }
                    reader.setConfig(config);
                    DxCardReader.getInstance().setmSSConfig(config);
                }
            }
        }
        if(cardConfig!=null){
            reader.setConfig(cardConfig);
            DxCardReader.getInstance().setmSSConfig(cardConfig);
        }
        return reader;
    }





    @Override
    public void initSSCard(SimpleDxInitListener listener) {
        switch (DxCardReader.getInstance().getStatusEnum()){
            case SUCCESS:
                if(listener!=null){
                    listener.initSuccess();
                }
                return;
            case  FAIL:
                if(listener!=null){
                    listener.initFailed(-1+"","社保卡初始化失败");
                }
                return;
            case DEFAULT:

                break;
        }
        init(listener);
    }
    /**
     *
     * @author xwh
     * Time 2024/10/10
     * Description: 初始化监听
     *
     **/
    private void init(SimpleDxInitListener listener){
        DxCardReader.getInstance().init(BaseApplication.getContext(),DX_SSCardReader.class.getName(),listener);
    }


    @Override
    public void setOnceLinstenr(SSCardResultListener onceLinstenr) {
        this.onceLinstenr=onceLinstenr;
        startCountDownTimer();
    }

    /**
     *
     * desc:启动轮询社保卡
     * @author xia weihao
     * @date 2024/6/26
     *
     */ 
    public void startRead() {
        DxCardReader.getInstance().setSimpleSSCardResultListener(new SimpleSSCardResultListener() {
            @Override
            public void onSuccess(BaseSSCardBean data) {
                String ssCardStr=data.toString();
                if (onceLinstenr != null) {
                    onceLinstenr.onSuccess(data);
                    onceLinstenr = null;
                }
                long currTime = System.currentTimeMillis();
                long intervalTime = 0L;
                if (config != null && config.getRepeatIntervalTime() > 0) {
                    intervalTime = config.getRepeatIntervalTime();
                }
                if (ssCardStr.equals(mOldSSCardStr) && currTime > 0) {
                    if (currTime - recordTime > intervalTime) {
                        mOldSSCardStr = ssCardStr;
                        recordTime = currTime;
                        if(listener!=null) {
                            listener.onSuccess(data);
                        }
                    }
                }else {
                    mOldSSCardStr = ssCardStr;
                    recordTime = currTime;
                    if(listener!=null) {
                        listener.onSuccess(data);
                    }
                }
            }

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

            @Override
            public void onInfoLog(String infoMsg) {

            }
        });

    }

    /**
     *
     * desc:停止轮询读取社保卡
     * @author xia weihao
     * @date 2024/6/26
     *
     */ 
    public void stopRead() {
        DxCardReader.getInstance().setSimpleSSCardResultListener(null);
    }



    /**
     *
     * @author xwh
     * Time 2024/9/3
     * Description: 启动倒计时：单次刷身份证
     *
     **/
    private void startCountDownTimer(){
        stopCountDownTimer();
        readIDCardCountDownTimer = new CountDownTimer(readIDCardTotalTime, 1000) {
            public void onTick(long millisUntilFinished) {
                // 每次间隔，这里更新UI，显示剩余时间
                AppLogUtils.e("身份证，倒计时--" + millisUntilFinished / 1000);
            }

            public void onFinish() {
                // 倒计时结束时的操作
                if (onceLinstenr!=null) {
                    // 未结束
                    onceLinstenr.onFail("超时");
                    onceLinstenr=null;
                }
            }
        };
        // 开始倒计时
        readIDCardCountDownTimer.start();
    }

    /**
     *
     * @author xwh
     * Time 2024/9/3
     * Description: 停止倒计时
     *
     **/
    private void stopCountDownTimer(){
        if (readIDCardCountDownTimer != null) {
            readIDCardCountDownTimer.cancel();
            readIDCardCountDownTimer=null;
        }
    }


    /**
     *
     * desc:销毁资源
     * @author xia weihao
     * @date 2024/6/26
     *
     */ 
    @Override
    public void release(Context context) {
        stopCountDownTimer();
        stopRead();
        reader = null;
    }
}
