package com.jwipc.sys.controller;

import com.jwipc.api.constant.RestCode;
import com.jwipc.api.constant.SignFlowStatus;
import com.jwipc.api.dto.ApiResult;
import com.jwipc.api.dto.SignBackDTO;
import com.jwipc.api.interceptor.SignBackLogic;
import com.jwipc.constant.SignProcessType;
import com.jwipc.log.annotation.ApiOperateLog;
import com.jwipc.log.constant.ApiDirection;
import com.jwipc.log.constant.ThirdSysType;
import com.jwipc.sys.logic.ESignLogic;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/sign/api")
@RequiredArgsConstructor
@Slf4j
public class SignBackController {
    private final ESignLogic eSignLogic;

    @Autowired
    private List<SignBackLogic> signBackLogics;

    @PostMapping("callback")
    @ApiOperateLog(code = "callBack", name = "e签宝回调SRM", direction = ApiDirection.OTHER_TO_SRM,
            thirdSysType = ThirdSysType.SIGN)
    public ApiResult callBack(@RequestBody SignBackDTO signBackDTO) {
//        return eSignLogic.callBack(signBackDTO);
//        log.info("{}", signBackDTO);
        for (SignBackLogic service : signBackLogics) {
            final SignProcessType signProcessType = service.getProcessType();
            if (signBackDTO.getCallBackProcessVO().businessNo.equals(SignProcessType.CONTRACT.getBusinessNo())){
                if (signBackDTO.getCallBackProcessVO().businessNo.equals(signProcessType.getBusinessNo()) ) {
                    String signDownloadOuterUrl = "";
                    if(signBackDTO.getCallBackProcessVO().flowStatus==SignFlowStatus.Completed.getStatus()){
                        List<SignBackDTO.SignerList> signFileLists = signBackDTO.getCallBackProcessVO().getSignerList();
                        if(signFileLists!=null&&signFileLists.size()==1){
                            signDownloadOuterUrl = signFileLists.get(0).getSignFileList().get(0).getSignedFileKey();
                        }else{
                            Map<Integer,List<SignBackDTO.SignerList>> maps = signFileLists.stream().collect(Collectors.groupingBy(SignBackDTO.SignerList::getSignOrder));
                            signDownloadOuterUrl = maps.get(2).get(0).getSignFileList().get(0).getSignedFileKey();
                        }
                    }
                    return service.processor(signBackDTO.getCallBackProcessVO().processId,
                            SignFlowStatus.forVal(signBackDTO.getCallBackProcessVO().flowStatus),
                            signBackDTO.getCallBackProcessVO().businessNo,signDownloadOuterUrl);
                }
            }else {
                return eSignLogic.callBack(signBackDTO);
            }

        }
        return ApiResult.builder().code(RestCode.SIGN_SUCCESS.getCode()).message("未找到业务处理逻辑").build();
    }
}
