package com.tzld.piaoquan.ad.engine.service.remote;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tzld.piaoquan.recommend.feature.domain.ad.base.AdActionFeature;
import com.tzld.piaoquan.recommend.feature.domain.ad.base.AdRankItem;
import com.tzld.piaoquan.recommend.feature.domain.ad.base.UserAdFeature;
import com.tzld.piaoquan.ad.engine.commons.util.CommonCollectionUtils;
import com.tzld.piaoquan.ad.engine.service.score.convert.FeatureConvert;
import com.tzld.piaoquan.recommend.feature.client.FeatureClient;
import com.tzld.piaoquan.recommend.feature.model.feature.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;


/**
 * @author dyp
 */

@Slf4j
@Service
public class FeatureRemoteService {

    @Autowired
    private FeatureClient client;

    // 有性能问题再增加localCache
    public UserAdFeature getUserAdFeature(String mid) {
        if (StringUtils.isBlank(mid)) {
            return null;
        }
        UserAdFeatureProto proto = client.getUserAdFeature(mid);
        UserAdFeature adFeature=convert(proto);

        return adFeature;
    }

    private UserAdFeature convert(UserAdFeatureProto proto) {
        if (proto == null) {
            return null;
        }

        return FeatureConvert.convertToJava(proto);
    }

    private AdActionFeature convert(AdActionFeatureProto proto) {
        return FeatureConvert.convertToJava(proto);
    }

    public AdRankItem getAdFeature(Long adId) {
        if (adId == null) {
            return null;
        }
        AdItemFeatureProto proto = client.getAdItemFeature(adId.toString());
        return convert(proto);
    }

    public Map<Long, AdRankItem> getAdFeatureMap(List<String> adIdList) {
        if (CollectionUtils.isEmpty(adIdList)) {
            return Collections.emptyMap();
        }
        List<AdItemFeatureProto> protoList = client.getAllAdItemFeature(adIdList);
        return CommonCollectionUtils.toMap(protoList, p -> NumberUtils.toLong(p.getAdId(), 0L), this::convert);
    }

    public List<AdRankItem> getAllAdFeatureList(List<String> adIdList) {
        if (CollectionUtils.isEmpty(adIdList)) {
            return Collections.emptyList();
        }
        List<AdItemFeatureProto> protoList = client.getAllAdItemFeature(adIdList);
        return CommonCollectionUtils.toList(protoList, p -> convert(p));
    }


    private AdRankItem convert(AdItemFeatureProto proto) {
        if (proto == null) {
            return null;
        }
        AdRankItem feature = new AdRankItem();

        feature.setAdId(Long.parseLong(proto.getAdId()));
        feature.setItemFeature(FeatureConvert.convertToJava(proto));

        return feature;
    }


}
