package com.engine.htsc.seal.common;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.cus.HTUtil;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * @author K1810015
 * @version 1.0.0
 * @ClassName Sendesealbuild.java
 * @createTime 2022年03月15日 10:22:00
 * //实现类
 */
public class SendesealBuilder  {
    public String bodyvalue;
    public String responsevalue;
    private HTUtil htUtil=new HTUtil();
    private SendesealProduct sealproduct;

    private boolean flag=false;
    public SendesealBuilder Sendesealurl(){
        JAVATools.SetCmdwritelog("调用统一用印登记单接口参数-prams:"+bodyvalue);

        return this;
    }

    public SendesealBuilder sendeseal() {
        JAVATools.SetCmdwritelog("通用送用印flag"+this.flag);
        if(!this.flag){
         JAVATools.SetCmdwritelog("通用送用印bodyvalue"+bodyvalue);
        String response = htUtil.ManualSignPost(bodyvalue);
        sealproduct= SendesealProduct.of(response);
            this.responsevalue=response;
        this.flag=true;
        }
        return this;
    }

    public boolean callback(String obj,Map<String,Object> map) {
        JAVATools.SetCmdwritelog("通用送用印callback:"+map);
        if(map==null){
            map=new HashMap<>();
        }
        map.put("response",sealproduct.getesealstatus());
        boolean flag=false;
        try {
            if(StringUtils.isBlank(obj)){
                return false;
            }

            //callback
            String classname = new Exception().getStackTrace()[1].getClassName();
            /** 获取调用者的类名    */
            Object objcet= Class.forName(classname).newInstance();
            Method m1=null;
            m1= this.getClass().getDeclaredMethod(obj,Map.class);
            flag=  (boolean)m1.invoke(objcet);

        }catch (Exception e){
            return false;
        }
        return true;
    }

   public static SendesealBuilder getSendeseal(String value){
        try {
            SendesealBuilder sendeseal= new SendesealBuilder();
            sendeseal.bodyvalue=value;
            return sendeseal;
        }catch (Exception e){
            return null;
        }
    }


  
}



