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

import com.tzld.piaoquan.ad.engine.commons.dto.AdPlatformCreativeDTO;
import com.tzld.piaoquan.ad.engine.commons.param.RankRecommendRequestParam;
import com.tzld.piaoquan.ad.engine.commons.score.ScoreParam;
import com.tzld.piaoquan.ad.engine.commons.score.ScorerUtils;
import com.tzld.piaoquan.ad.engine.commons.util.CommonCollectionUtils;
import com.tzld.piaoquan.ad.engine.service.remote.FeatureRemoteService;
import com.tzld.piaoquan.ad.engine.service.score.RankService;
import com.tzld.piaoquan.ad.engine.service.score.convert.RequestConvert;
import com.tzld.piaoquan.recommend.feature.domain.ad.base.AdRankItem;
import com.tzld.piaoquan.recommend.feature.domain.ad.base.UserAdFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class RankServiceThompsonImpl implements RankService {

    private final static Logger log = LoggerFactory.getLogger(RankServiceThompsonImpl.class);

    @Autowired
    FeatureRemoteService featureRemoteService;

    public AdRankItem adItemRank(RankRecommendRequestParam request) {

        ScoreParam param = RequestConvert.requestConvert(request);
        UserAdFeature userAdFeature = new UserAdFeature();
        List<AdRankItem> rankItems = featureRemoteService.getAllAdFeatureList(
                CommonCollectionUtils.toList(request.getAdIdList(), creativeDTO -> creativeDTO.getCreativeId().toString())
        );
        List<AdRankItem> rankResult = ScorerUtils
                .getScorerPipeline(ScorerUtils.THOMPSON_CONF)
                .scoring(param, userAdFeature, rankItems);

        if (!CollectionUtils.isEmpty(rankResult)) {
            return rankResult.get(0);
        } else {
            return null;
        }
    }

    public AdRankItem adItemRankWithVideoAdThompson(RankRecommendRequestParam request) {

        ScoreParam param = RequestConvert.requestConvert(request);
        UserAdFeature userAdFeature = new UserAdFeature();
        List<AdRankItem> rankItems = featureRemoteService.getAllAdFeatureList(
                CommonCollectionUtils.toList(request.getAdIdList(), creativeDTO -> creativeDTO.getCreativeId().toString())
        );
        List<AdRankItem> rankResult = ScorerUtils
                .getScorerPipeline(ScorerUtils.THOMPSON_CONF)
                .scoring(param, userAdFeature, rankItems);

        if (!CollectionUtils.isEmpty(rankResult)) {
            return rankResult.get(0);
        } else {
            return null;
        }
    }


    public AdRankItem adItemRankV2(RankRecommendRequestParam request) {

        ScoreParam param = RequestConvert.requestConvert(request);
        UserAdFeature userAdFeature = new UserAdFeature();
        List<AdRankItem> rankItems = featureRemoteService.getAllAdFeatureList(
                CommonCollectionUtils.toList(request.getAdIdList(), creativeDTO -> creativeDTO.getCreativeId().toString())
        );
        List<AdRankItem> rankResult = ScorerUtils
                .getScorerPipeline(ScorerUtils.THOMPSON_CONF)
                .scoring(param, userAdFeature, rankItems);

        if (!CollectionUtils.isEmpty(rankResult)) {
            return rankResult.get(0);
        } else {
            return null;
        }
    }

    @Override
    public AdPlatformCreativeDTO adBidRank(BidRankRecommendRequestParam request) {
        return null;
    }

    @Override
    public AdPlatformCreativeDTO adBidRankNewPid(BidRankRecommendRequestParam request) {
        return null;
    }

    public List<AdRankItem> rank (ScoreParam param, UserAdFeature userAdFeature, List<AdRankItem> rankItems,String configFile){
        if(configFile==null){
            configFile=ScorerUtils.THOMPSON_CONF;
        }
        List<AdRankItem> rankResult = ScorerUtils
                .getScorerPipeline(configFile)
                .scoring(param, userAdFeature, rankItems);
        return rankResult;
    }

}
