package cokr.xit.ens.modules.common.ctgy.intgrnnoti.service.support;

import cokr.xit.ens.core.aop.EnsResponseVO;
import cokr.xit.ens.core.exception.code.EnsErrCd;
import cokr.xit.ens.core.utils.MapDeserailizer;
import cokr.xit.ens.modules.common.code.IntgrnDtlStatCd;
import cokr.xit.ens.modules.common.code.PostSeCd;
import cokr.xit.ens.modules.common.ctgy.intgrnnoti.domain.IntgrnSendDetail;
import cokr.xit.ens.modules.common.domain.support.FieldError;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

public interface AcceptDataEventListener<P, SD> {


    @Async
    @EventListener
    @Transactional
    void accept(P p);

    default void acceptFail(List<IntgrnSendDetail> intgrnSendDetails, PostSeCd postSeCd, EnsErrCd ensErrCd, String errMsg) {
        intgrnSendDetails.forEach(data -> {
            data.setCurPostSe(postSeCd);                       
            data.setCurStatCd(IntgrnDtlStatCd.ACPT_FAIL);   
            data.setError(FieldError.initBuilder()
                    .errorCode(ensErrCd.getCode())
                    .errorMessage(errMsg)
                    .build());
        });
    }

    default void acceptSuccess(List<IntgrnSendDetail> intgrnSendDetails, PostSeCd postSeCd, Map<String, Long> sendDetailIdMap) {
       
        intgrnSendDetails.forEach(data -> {
            if (sendDetailIdMap.containsKey(data.getLinkedUuid())) {
                data.setCurPostSe(postSeCd);                                     
                data.setCurStatCd(IntgrnDtlStatCd.ACPT_OK);                   
                setSendDetailId(data, sendDetailIdMap.get(data.getLinkedUuid()));
                data.setError(FieldError.initBuilder().build());
            }
        });

    }

    Map<String, Long> convertSendDetailIdMap(List<SD> sendDetails);

    void setSendDetailId(IntgrnSendDetail intgrnSendDetail, Long sendDetailId);

//    EnsResponseVO fetch(Long sendMastId, List<IntgrnSendDetail> sendDetails);

    default String createFailMessage(Long intSendMastId, PostSeCd postSeCd, EnsResponseVO responseVO) {
        StringBuffer sb = new StringBuffer();
        sb.append("-.IntSendMastId: " + intSendMastId)
                .append(System.getProperty("line.separator"))
                .append("-.PostType: " + postSeCd.getCodeNm())
                .append(System.getProperty("line.separator"))
                .append(responseVO.getErrMsg());
        if (EnsErrCd.ACPT402.equals(responseVO.getErrCode())) {
            sb.append(System.getProperty("line.separator"))
                    .append("-.ValidateFails: ");
            try {
//                ((List<?>) responseVO.getResultInfo()).forEach(str -> sb.append(System.getProperty("line.separator")).append(str));
                if (responseVO.getResultInfo() instanceof List || responseVO.getResultInfo() instanceof Map){
                    Gson gson = new GsonBuilder().registerTypeAdapter(Map.class, new MapDeserailizer()).disableHtmlEscaping().create();
                    sb.append(gson.toJson(responseVO.getResultInfo()));
                }
            } catch (Exception e) {
                sb.append("VaildateFails message is create fail.. " + e.getMessage());
            }
        }
        return sb.toString();
    }

}
