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.eteams.file.client.file.FileObj;
import com.weaver.seconddev.chapanda.esign.entity.OkHttpDto;
import com.weaver.seconddev.chapanda.esign.util.Esb2EsignDownlUrlUtil;
import com.weaver.seconddev.chapanda.esign.util.Esb2EsignFileUploadUtil;
import com.weaver.seconddev.chapanda.esign.util.Esb2EsignPreviewUrlUtil;
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.Map;

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

    @Autowired
    Esb2EsignDownlUrlUtil esb2EsignDownlUrlUtil;

    @Autowired
    Esb2EsignFileUploadUtil esb2EsignFileUploadUtil;

    @Autowired
    private Esb2EsignPreviewUrlUtil esb2EsignPreviewUrlUtil;

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

        String signFlowId = String.valueOf(params.get("signFlowId"));
        String fileName = String.valueOf(params.get("fileName"));
        String folderid = String.valueOf(params.get("folderid"));
        log.error("signFlowId:{}",signFlowId);
        if(StringUtils.isBlank(signFlowId)){
            return WeaResult.fail(500,"入参申请流程为空");
        }

        if(StringUtils.isBlank(fileName)){
            return WeaResult.fail(500,"入参文件名称为空");
        }

        if(StringUtils.isBlank(folderid)){
            return WeaResult.fail(500,"入参附件目录为空");
        }

        Long longSignFlowId = convertStringToLong(folderid);
        if(longSignFlowId == 0L){
            return WeaResult.fail(500,"入参附件目录转换失败");
        }

        String downloadDocUrl = "";
        String docName = "";
        String docFileKey = "";
        String signDocId = "";
        OkHttpDto okHttpDto = esb2EsignDownlUrlUtil.queryDownlUrl(signFlowId);
        if(okHttpDto.getResponseCode() == 200){
            if(okHttpDto.getDataCode() == 0){
                JSONObject dataJson = okHttpDto.getDataJson();
                log.error("dataJson:{}",dataJson.toJSONString());
                if(dataJson.containsKey("downloadDocUrl")){
                    downloadDocUrl = dataJson.getString("downloadDocUrl");
                }

                if(dataJson.containsKey("docName")){
                    docName = dataJson.getString("docName");
                }

                if(dataJson.containsKey("downloadDocUrl")){
                    docFileKey = dataJson.getString("docFileKey");
                }

                if(dataJson.containsKey("downloadDocUrl")){
                    signDocId = dataJson.getString("docId");
                }
            }else{
                return WeaResult.fail(500,"签署文件获取异常,"+okHttpDto.getDataMsg());
            }
        }else{
            return WeaResult.fail(500,"签署文件获取接口异常,"+okHttpDto.getDataMsg());
        }

        log.error("downloadDocUrl:{}",downloadDocUrl);
        if(StringUtils.isBlank(downloadDocUrl)){
            return WeaResult.fail(500,"获取文件下载地址失败");
        }

        Long docId = 0L;
        Long fileid =  0L;
        if(StringUtils.isNotBlank(downloadDocUrl)){
            FileObj fileObj = esb2EsignFileUploadUtil.uploadFile(downloadDocUrl,fileName,longSignFlowId);
            log.error("fileObj:{}",fileObj.toString());
            docId = fileObj.getDocId();
            fileid = fileObj.getId();
        }
        log.error("docId:{}",docId);
        log.error("fileid:{}",fileid);

        if(fileid <= 0){
            return WeaResult.fail(500,"创建文档失败");
        }

        if(StringUtils.isBlank(docFileKey)){
            return WeaResult.fail(500,"获取签署文件失败");
        }

        String preViewUrl = "";
        if(StringUtils.isNotBlank(docFileKey)){
            OkHttpDto preOkHttpDto = esb2EsignPreviewUrlUtil.queryPreviewUrl(docFileKey);
            if(preOkHttpDto.getResponseCode() == 200){
                if(preOkHttpDto.getDataCode() == 0){
                    JSONObject preJson = preOkHttpDto.getDataJson();
                    if(preJson.containsKey("preViewUrl")){
                        preViewUrl = preJson.getString("preViewUrl");
                    }
                }else{
                    return WeaResult.fail(500,"获取签署文件预览地址异常,"+preOkHttpDto.getDataMsg());
                }
            }else{
                return WeaResult.fail(500,"签署文件预览地址接口异常,"+preOkHttpDto.getDataMsg());
            }
        }
        log.error("preViewUrl:{}",preViewUrl);

        Map<String, Object> actionMap = new HashMap<String, Object>();
        actionMap.put("code",200);
        actionMap.put("docId",docId);
        actionMap.put("fileid",fileid);
        actionMap.put("downloadDocUrl",downloadDocUrl);
        actionMap.put("docName",docName);
        actionMap.put("docFileKey",docFileKey);
        actionMap.put("signDocId",signDocId);
        actionMap.put("preViewUrl",preViewUrl);

        return WeaResult.success(actionMap);
    }


    public long convertStringToLong(String str) {
        try {
            return Long.parseLong(str);
        } catch (NumberFormatException e) {
            return 0;
        }
    }


}
