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


import com.tzld.piaoquan.ad.engine.commons.redis.AlgorithmRedisHelper;
import com.tzld.piaoquan.ad.engine.commons.score.BaseLRModelScorer;
import com.tzld.piaoquan.ad.engine.commons.score.ScoreParam;
import com.tzld.piaoquan.ad.engine.commons.score.ScorerConfigInfo;
import com.tzld.piaoquan.ad.engine.service.predict.config.AdOutV1OnlineWeightConfig;
import com.tzld.piaoquan.ad.engine.service.predict.constant.RuleRedisKeyConst;
import com.tzld.piaoquan.ad.engine.service.score.ServiceBeanFactory;
import com.tzld.piaoquan.recommend.feature.domain.ad.base.AdRankItem;
import com.tzld.piaoquan.recommend.feature.domain.ad.base.UserAdFeature;
import org.apache.commons.lang3.math.NumberUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


//@Service
public class VlogAdBreakScorer extends BaseLRModelScorer {

    private AlgorithmRedisHelper redisHelper;

    public VlogAdBreakScorer(ScorerConfigInfo configInfo) {
        super(configInfo);
        this.redisHelper = ServiceBeanFactory.getBean(AlgorithmRedisHelper.class);
    }


    @Override
    public List<AdRankItem> scoring(final ScoreParam param,
                                    final UserAdFeature userFeature,
                                    final List<AdRankItem> rankItems) {
        // offline score
        String modelKey = "ad_out_v1";
        String userKeyName = RuleRedisKeyConst.KEY_NAME_PREFIX_AD_OUT_MODEL_SCORE_USER + modelKey + ":" + param.getMid();
        String itemKeyName = RuleRedisKeyConst.KEY_NAME_PREFIX_AD_OUT_MODEL_SCORE_ITEM + modelKey + ":" + param.getVideoId();
        String userScore = redisHelper.get(userKeyName);
        String itemScore = redisHelper.get(itemKeyName);
        double offlineScore = NumberUtils.toDouble(userScore, 0) + NumberUtils.toDouble(itemScore, 0);

        // online score
        Map<String, String> onlineFeatures = new HashMap<>();
        onlineFeatures.put("ctx_apptype", param.getRequestContext().getApptype());
        onlineFeatures.put("ctx_week", param.getRequestContext().getWeek());
        onlineFeatures.put("ctx_hour", param.getRequestContext().getHour());
        double onlineScore = getOnlineScore(onlineFeatures);
        double finalScore = onlineScore + offlineScore;
        double breakRate = 1.0 / (1.0 + Math.exp(0d - finalScore));

        for (int i = 0; i < rankItems.size(); i++) {
            rankItems.get(i).setBreakRate(breakRate);
        }
        return rankItems;
    }

    private double getOnlineScore(Map<String, String> onlineFeatures) {
        double score = AdOutV1OnlineWeightConfig.getWeight("bias");
        for (Map.Entry<String, String> entry : onlineFeatures.entrySet()) {
            double featureScore = AdOutV1OnlineWeightConfig.getWeight(entry.getKey() + "#" + entry.getValue());
            score += featureScore;
        }
        return score;
    }

    @Override
    public void loadModel() {
        // nothing;
    }
}