package com.tzld.piaoquan.ad.engine.service.score.strategy;

import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValue;
import com.tzld.piaoquan.ad.engine.commons.dto.AdPlatformCreativeDTO;
import com.tzld.piaoquan.ad.engine.commons.enums.RedisPrefixEnum;
import com.tzld.piaoquan.ad.engine.commons.param.RankRecommendRequestParam;
import com.tzld.piaoquan.ad.engine.commons.redis.AdRedisHelper;
import com.tzld.piaoquan.ad.engine.commons.score.ScoreParam;
import com.tzld.piaoquan.ad.engine.service.feature.Feature;
import com.tzld.piaoquan.ad.engine.service.feature.FeatureService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
public abstract class RankStrategyBasic implements RankStrategy {

    @ApolloJsonValue("${no.postback.conversion.adverids:[]}")
    private Set<String> noPostbackConversionAdVerIds;

    @ApolloJsonValue("${creative.model.score.coefficient:{}}")
    private Map<Long, Double> creativeScoreCoefficient;

    @Autowired
    private FeatureService featureService;
    @Autowired
    protected AdRedisHelper adRedisHelper;

    protected Feature getFeature(ScoreParam param, RankRecommendRequestParam request) {
        List<AdPlatformCreativeDTO> adIdList = request.getAdIdList();
        List<String> cidList = adIdList.stream()
                .map(AdPlatformCreativeDTO::getCreativeId)
                .map(Object::toString)
                .collect(Collectors.toList());

        List<String> adVerIdList = adIdList.stream()
                .map(AdPlatformCreativeDTO::getAdVerId)
                .filter(StringUtils::isNotBlank)
                .distinct()
                .collect(Collectors.toList());
        return featureService.getFeature(cidList, adVerIdList, param);
    }

    protected Set<String> getNoApiAdVerIds() {
        Set<String> noApiAdVerIds = adRedisHelper.sMembers(RedisPrefixEnum.ADVER_IS_API_EQ_0_IDS.getPrefix());
        log.info("Redis中缓存的没有回传广告主ID集合: {}", noApiAdVerIds);
        if (CollectionUtils.isEmpty(noApiAdVerIds)) {
            noApiAdVerIds = noPostbackConversionAdVerIds;
        }
        return noApiAdVerIds;
    }

    protected Map<Long, Double> getCreativeScoreCoefficient() {
        if (Objects.isNull(creativeScoreCoefficient)) {
            return new HashMap<>();
        }
        return creativeScoreCoefficient;
    }

    protected static class Tuple5 {
        public String f1;
        public String f2;
        public String f3;
        public String f4;
        public String f5;

        public Tuple5(String f1, String f2, String f3, String f4, String f5) {
            this.f1 = f1;
            this.f2 = f2;
            this.f3 = f3;
            this.f4 = f4;
            this.f5 = f5;
        }
    }

    protected static class TupleMapEntry<T> {
        public String key;
        public T value;

        public TupleMapEntry(String key, T value) {
            this.key = key;
            this.value = value;
        }
    }

    protected static class Tuple2<F1, F2> {
        public F1 f1;

        public F2 f2;

        public Tuple2(F1 first, F2 name) {
            this.f1 = first;
            this.f2 = name;
        }

    }
}
