package com.friday.custody.api.service;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import com.friday.custody.api.dto.gjdf.HtscProductInfo;
import com.friday.custody.api.dto.gjdf.HtscResult;
import com.friday.custody.api.feignclient.HtscApi;
import com.friday.custody.service.basicService.TenantEscrowAuthorizationInfoService;
import com.friday.custody.entity.TenantEscrowAuthorizationInfoEntity;
import com.friday.custody.enums.DaasTrustNoEnum;
import com.friday.custody.exception.RRException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 021790
 */
@Service
@Slf4j
public class HtscService {
    @Autowired
    private HtscApi api;
    @Autowired
    private TenantEscrowAuthorizationInfoService authorizationInfoService;

    private final ObjectMapper objectMapper = new ObjectMapper()
            .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
            .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
            .findAndRegisterModules()
            .setSerializationInclusion(JsonInclude.Include.NON_NULL);

    public List<HtscProductInfo> productInfo(String admin){
        List<TenantEscrowAuthorizationInfoEntity> authorizationList = authorizationInfoService.queryData(DaasTrustNoEnum.HTSC.getCode(), admin, "api");
        if (CollectionUtils.isNotEmpty(authorizationList)){
            TenantEscrowAuthorizationInfoEntity tenantEscrowAuthorizationInfoEntity = authorizationList.get(0);
            String appId = tenantEscrowAuthorizationInfoEntity.getAppKey();
            String rawStringResponse = api.queryProductInfo(Integer.valueOf(appId));
            try {
                List<HtscProductInfo> response = toResp(rawStringResponse, new TypeReference<HtscResult<List<HtscProductInfo>>>() {
                });
                return response;
            } catch (IOException e) {
                try {
                    HtscResult eResp = objectMapper.readValue(rawStringResponse, new TypeReference<HtscResult>() {
                    });
                    log.warn("request api failed:{},{}",eResp.getCode(),eResp.getMsg());
                    throw new RRException(eResp.getCode(), eResp.getMsg());
                } catch (IOException ex) {
                    log.error("parse json failed:{}",rawStringResponse);
                    throw new RRException("json parse error",ex);
                }
            }
        }
        return new ArrayList<>();
    }

    protected <T> T toResp(String response) throws IOException {
        return toResp(response, new TypeReference<HtscResult<T>>() {
        });
    }

    protected <E> E toResp(String response, TypeReference<HtscResult<E>> typeReference) throws IOException {
        HtscResult<E> eResp = objectMapper.readValue(response, typeReference);

        if (eResp.getCode() != 200) {
            log.warn("request api failed:{},{}",eResp.getCode(),eResp.getMsg());
            throw new RRException(eResp.getCode(), eResp.getMsg());
        }
        return eResp.getData();
    }
}
