package com.css.simulation.resource.monitor.acl.service.impl;

import api.common.pojo.constants.DictConstants;
import api.common.util.HttpUtil;
import api.common.util.JsonUtil;
import api.common.util.ObjectUtil;
import com.css.simulation.resource.monitor.acl.entity.AlgorithmExpandAclEntity;
import com.css.simulation.resource.monitor.acl.entity.AlgorithmExpandConfigurationAclEntity;
import com.css.simulation.resource.monitor.acl.service.AlgorithmExpandAclService;
import com.css.simulation.resource.monitor.infra.redis.CustomRedisClient;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Duration;
import java.util.List;

@Slf4j
@Service
public class AlgorithmExpandAclServiceImpl implements AlgorithmExpandAclService {

    @Resource
    private AlgorithmExpandConfigurationAclEntity algorithmExpandConfigurationAclEntity;
    @Resource
    private CustomRedisClient customRedisClient;


    /**
     * 从缓存中获取token，没有才发送请求
     *
     */
    public String getToken() {
        String token = customRedisClient.get(DictConstants.ALGORITHM_PLATFORM_TOKEN_CACHE_KEY);
        if (ObjectUtil.isNull(token)) {//token失效重新获取
            String tokenUri = algorithmExpandConfigurationAclEntity.getTokenUri();
            String appid = algorithmExpandConfigurationAclEntity.getAppid();
            String secret = algorithmExpandConfigurationAclEntity.getSecret();
            String URI = tokenUri + "&appid=" + appid + "&secret=" + secret;
            String result = HttpUtil.get(HttpUtil.getHttpClient(), HttpUtil.getRequestConfig(), URI);
            JsonNode jsonNode = JsonUtil.readTree(result);
            JsonNode dataNode = jsonNode.path("data");
            if (ObjectUtil.isNotNull(dataNode)) {
                token = dataNode.path("access_token").asText();
                if (ObjectUtil.isNotNull(token)) {
                    long expiresSec = dataNode.path("expires_in_sec").asLong();
                    customRedisClient.set(DictConstants.ALGORITHM_PLATFORM_TOKEN_CACHE_KEY,token, Duration.ofSeconds(expiresSec));
                }
            }
        }
        return token;
    }

    @Override
    public List<AlgorithmExpandAclEntity> getAlgorithmList() {
        //取得token
        String token = getToken();
        if (ObjectUtil.isNull(token)) {
           throw new RuntimeException("获取算法平台token失败。");
        }
        String algorithmListUri = algorithmExpandConfigurationAclEntity.getAlgorithmListUri()+ "?sort=algorithmId-desc&page=0&size=999999&access_token=" + token;
        String httpResponseString = HttpUtil.get(HttpUtil.getHttpClient(), HttpUtil.getRequestConfig(), algorithmListUri);
        log.info("算法平台请求结果为：{}", httpResponseString);
        final String algorithmListJson = JsonUtil.getString(httpResponseString, "data.content");
        log.info("算法平台的算法列表JSON为：{}", algorithmListJson);
        final List<AlgorithmExpandAclEntity> algorithmExpandAclEntities = JsonUtil.jsonToList(algorithmListJson, AlgorithmExpandAclEntity.class);
        log.info("算法平台的算法列表为：{}", algorithmExpandAclEntities);
        return algorithmExpandAclEntities;

    }
}
