package com.weaver.seconddev.chapanda.esign.esb;

import com.alibaba.fastjson.JSONObject;
import com.weaver.common.base.entity.result.WeaResult;
import com.weaver.esb.api.rpc.EsbServerlessRpcRemoteInterface;
import com.weaver.seconddev.chapanda.esign.entity.OkHttpDto;
import com.weaver.seconddev.chapanda.esign.util.Esb2EsignBuildTemplatDoceUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@Service("Esb2EsignQueryPreviewUrlAction")
public class Esb2EsignQueryPreviewUrlAction implements EsbServerlessRpcRemoteInterface {
    private final static Logger log = LoggerFactory.getLogger(Esb2EsignQueryPreviewUrlAction.class);

    @Autowired
    Esb2EsignBuildTemplatDoceUtil esb2EsignBuildTemplatDoceUtil;

    @Override
    public WeaResult<Map<String, Object>> execute(Map<String, Object> params) {

        Iterator<Map.Entry<String, Object>> it = params.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, Object> entry = it.next();
            log.error("Key:{},Value:{}",entry.getKey(), entry.getValue());
        }

        String preViewUrl = "";
        String fileKey = "";
        String templateId = String.valueOf(params.get("templateId"));
        String form_data_id = String.valueOf(params.get("form_data_id"));
        if(StringUtils.isNotBlank(templateId)){
            OkHttpDto templateOkHttpDto = esb2EsignBuildTemplatDoceUtil.queryTemplateFileKey(templateId,params);
            if(templateOkHttpDto.getResponseCode() == 200){
                if(templateOkHttpDto.getDataCode() == 0 ){
                    JSONObject dataJson = templateOkHttpDto.getDataJson();
                    if(dataJson.containsKey("fileKey")){
                        fileKey = dataJson.getString("fileKey");
                    }
                    log.error("fileKey:{}",fileKey);
                    if(StringUtils.isNotBlank(fileKey)){
                        OkHttpDto preOkHttpDto = esb2EsignBuildTemplatDoceUtil.queryPreviewUrl(fileKey);
                        if(preOkHttpDto.getResponseCode() == 200){
                            if(preOkHttpDto.getDataCode() == 0){
                                JSONObject preJson = preOkHttpDto.getDataJson();
                                log.error("preJson:{}",preJson.toJSONString());
                                if(preJson.containsKey("preViewUrl")){
                                    preViewUrl = preJson.getString("preViewUrl");
                                }
                            }else{
                                return WeaResult.fail(500,"获取预览地址异常,"+preOkHttpDto.getDataMsg());
                            }
                        }else{
                            return WeaResult.fail(500,"文件预览地址接口异常,"+preOkHttpDto.getDataMsg());
                        }
                    }
                }else{
                    return WeaResult.fail(500,"模板生成文件异常,"+templateOkHttpDto.getDataMsg());
                }
            }else{
                return WeaResult.fail(500,"调用模板生产文件接口址异常,"+templateOkHttpDto.getDataMsg());
            }

//            dataMap = sealTemplateService.queryPreviewUrl(templateId,params);
//            String fileKey = String.valueOf(dataMap.get("filekey"));
//            String preViewUrl = String.valueOf(dataMap.get("previewurl"));
        }


        Map<String, Object> actionMap = new HashMap<String, Object>();
        actionMap.put("code",200);
        actionMap.put("filekey",fileKey);
        actionMap.put("previewurl",preViewUrl);
        actionMap.put("form_data_id",form_data_id);
        return WeaResult.success(actionMap);
    }
}
