package com.hh.lib_social_security_card;

import android.content.Context;

import com.alipay.arome.aromecli.AromeServiceInvoker;
import com.google.gson.JsonObject;
import com.hh.lib_ampe.bean.BaseAmpeAppletResultBean;
import com.hh.lib_ampe.bean.BaseAmpeRequestBean;
import com.hh.lib_ampe.utils.AromeInvoker;
import com.hh.lib_base.BaseApplication;
import com.hh.lib_base.Constants;
import com.hh.lib_base.bean.event.ConfigStatusTextEvent;
import com.hh.lib_base.log.AppLogUtils;
import com.hh.lib_base.log.UploadLogUtils;
import com.hh.lib_base.utils.GsonUtils;
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.listener.sscard.SimpleSSCardResultListener;

import org.greenrobot.eventbus.EventBus;

/**
 *
 * @author xwh
 * time 2024/9/12
 * Description: 社保卡调用器
 **/
public class SSCardInvoker {

    private volatile static SSCardInvoker invoker;
    private DX_SSCardReader dx_ssCardReader;

    /**
     *
     * @author xwh
     * Time 2024/9/12
     * Description: 社保卡功能交互入口
     *
    **/
    public void onCalled(Context context, BaseAmpeRequestBean dataBean, AromeServiceInvoker.BridgeCallback bridgeCallback) {
        if (dataBean == null) {
            return;
        }
        switch (dataBean.getAction()){
            case Constants.Ampe.ACTION_SOCIALSECURITYCARDINFO:
                startSSCardReader();
                onceSSCardInfo(dataBean);
                break;
            case Constants.Ampe.ACTION_SOCIALSECURITYCARDCONFIG:
                startSSCardReader();
                break;
        }
    }
    /**
     *
     * desc:启动社保卡读卡器
     * @author xia weihao
     * @date 2024/6/25
     *
     */
    private void startSSCardReader() {
        initSSCardReader();
        dx_ssCardReader.startRead();
    }

    /**
     *
     * @author xwh
     * Time 2024/9/12
     * Description: 单次获取社保卡信息
     *
    **/
    private void onceSSCardInfo(BaseAmpeRequestBean ampeRequestBean) {
        if (ampeRequestBean == null) {
            return;
        }
        dx_ssCardReader.setOnceLinstenr(new SimpleSSCardResultListener() {
            @Override
            public void onSuccess(BaseSSCardBean data) {
                BaseAmpeAppletResultBean<BaseSSCardBean> resultBean=new BaseAmpeAppletResultBean();
                resultBean.setAction(Constants.Ampe.ACTION_SOCIALSECURITYCARDINFO);
                resultBean.setEvent(ampeRequestBean.getEvent());
                resultBean.setTaskId(ampeRequestBean.getTaskId());
                resultBean.setSuccess(true);
                resultBean.setMessage("成功");
                resultBean.setData(data);

                try {
                    JsonObject jsonObject = GsonUtils.getGson().toJsonTree(resultBean).getAsJsonObject();
                    if(ampeRequestBean.getBridgeCallback()!=null){
                        ampeRequestBean.getBridgeCallback().callback(jsonObject.toString());
                    }
                    String logLine=    (new Throwable().getStackTrace())[0].toString();
                    StringBuilder logContentSb = new StringBuilder();
                    logContentSb.append("data = ")
                            .append(jsonObject.toString());
                    UploadLogUtils.sendLogs(Constants.LogcatValue.LOG_SOCIAL_SECURITY_CARD,
                            Constants.Ampe.ACTION_SOCIALSECURITYCARDINFO,logLine,
                            logContentSb.toString());

                }catch (Exception e){
                    AppLogUtils.e(e.toString());
                }
            }

            @Override
            public void onFail(String errMsg) {
                BaseAmpeAppletResultBean resultBean=new BaseAmpeAppletResultBean();
                resultBean.setAction(Constants.Ampe.ACTION_SOCIALSECURITYCARDINFO);
                resultBean.setEvent(ampeRequestBean.getEvent());
                resultBean.setTaskId(ampeRequestBean.getTaskId());
                resultBean.setSuccess(false);
                resultBean.setMessage(errMsg);
                try {
                    JsonObject jsonObject = GsonUtils.getGson().toJsonTree(resultBean).getAsJsonObject();
                    if(ampeRequestBean.getBridgeCallback()!=null){
                        ampeRequestBean.getBridgeCallback().callback(jsonObject.toString());
                    }
                    String logLine=    (new Throwable().getStackTrace())[0].toString();
                    StringBuilder logContentSb = new StringBuilder();
                    logContentSb.append("data = ")
                            .append(jsonObject.toString());
                    UploadLogUtils.sendLogs(Constants.LogcatValue.LOG_SOCIAL_SECURITY_CARD,
                            Constants.Ampe.ACTION_SOCIALSECURITYCARDINFO,logLine,
                            logContentSb.toString());

                }catch (Exception e){
                    AppLogUtils.e(e.toString());
                }
            }
            @Override
            public void onInfoLog(String infoMsg) {

            }

        });
    }


    private void initSSCardReader(){
        if(dx_ssCardReader==null){
            synchronized (SSCardInvoker.class){
                if(dx_ssCardReader==null){
                    dx_ssCardReader=DX_SSCardReader.getInstance(null);
                    dx_ssCardReader.initSSCard(new SimpleDxInitListener() {
                        @Override
                        public void initSuccess() {
                            EventBus.getDefault().post(new ConfigStatusTextEvent("社保卡-" + "初始化成功。"));
                        }

                        @Override
                        public void initFailed(String code, String msg) {
                            EventBus.getDefault().post(new ConfigStatusTextEvent("社保卡-" + "初始化失败。"));
                        }
                    });
                    dx_ssCardReader.setLoopListener(new SimpleSSCardResultListener() {
                        @Override
                        public void onSuccess(BaseSSCardBean data) {
                            if (data == null) {
                                return;
                            }
                            //   AppLogUtils.i("_____________123"+data.getAllInfoJson());
                            AromeInvoker.getAromeInvoker().sendMessageToApplet(Constants.Ampe
                                    .EVENT_SOCIALSECURITYCARD_LISTENER,data.getAllInfoJson());
                        }

                        @Override
                        public void onInfoLog(String infoMsg) {
                        }
                    });
                }
            }
        }

    }
    public static SSCardInvoker getInstance() {
        if (invoker == null) {
            synchronized (SSCardInvoker.class) {
                if (invoker == null) {
                    invoker = new SSCardInvoker();
                }
            }
        }
        return invoker;
    }

    public void onRelease() {
        if(dx_ssCardReader!=null){
            dx_ssCardReader.release(BaseApplication.getContext());
        }
        invoker = null;
    }
}
