package com.tzld.piaoquan.recommend.server.implement.candidate;

import com.tzld.piaoquan.recommend.server.framework.candidiate.Candidate;
import com.tzld.piaoquan.recommend.server.framework.candidiate.QueueName;
import com.tzld.piaoquan.recommend.server.framework.common.User;
import com.tzld.piaoquan.recommend.server.framework.merger.IndexCandidateQueue;
import com.tzld.piaoquan.recommend.server.framework.merger.StrategyQueueConfig;
import com.tzld.piaoquan.recommend.server.framework.merger.StrategyQueueInfo;
import com.tzld.piaoquan.recommend.server.gen.recommend.RecommendRequest;

import java.math.BigDecimal;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class Region24hHotCandidate extends IndexCandidateQueue {

    public static final String ItemType = "video";

    public Region24hHotCandidate(StrategyQueueInfo strategyQueueInfo, StrategyQueueConfig strategyQueueConfig) {
        super(strategyQueueInfo, strategyQueueConfig);
    }

    @Override
    public int addCandidateKey(Map<String, Candidate> candidates, int recallNum, User user, RecommendRequest requestData, int requestIndex, int expId) {
        int currRecall = 0;
        String region = user.getRegion();
        Map<String, Candidate> simplifiedCandidates = new ConcurrentHashMap<String, Candidate>();


        BigDecimal recallNumBigDecimal = BigDecimal.valueOf(recallNum);
        int combinationNum = recallNumBigDecimal.multiply(BigDecimal.valueOf(0.2)).intValue();
        int rosNum = recallNumBigDecimal.multiply(BigDecimal.valueOf(0.3)).intValue();
        int rovNum = recallNumBigDecimal.multiply(BigDecimal.valueOf(0.4)).intValue();
        int strNum = recallNumBigDecimal.multiply(BigDecimal.valueOf(0.1)).intValue();

        // index key  video:queue:type=region24h:region=北京:ordering=rov
        QueueName queueName = new QueueName(ItemType, "rov")
                .addMatch("type", "region24h")
                .addMatch("region", region);
        currRecall += addCandidateKey(simplifiedCandidates, queueName, rovNum, getStrategyQueueInfo().getQueueName());

        // index key  video:queue:type=region24h:region=北京:ordering=str
        queueName = new QueueName(ItemType, "short_ros")
                .addMatch("type", "region24h")
                .addMatch("region", region);
        currRecall += addCandidateKey(simplifiedCandidates, queueName, rosNum,
                getStrategyQueueInfo().getQueueName());

        // index key  video:queue:type=region24h:region=北京:ordering=combination
        queueName = new QueueName(ItemType, "combination")
                .addMatch("type", "region24h")
                .addMatch("region", region);
        currRecall += addCandidateKey(simplifiedCandidates, queueName, combinationNum,
                getStrategyQueueInfo().getQueueName());

        // index key  video:queue:type=region24h:region=北京:ordering=str
        queueName = new QueueName(ItemType, "str")
                .addMatch("type", "region24h")
                .addMatch("region", region);
        currRecall += addCandidateKey(simplifiedCandidates, queueName, strNum,
                getStrategyQueueInfo().getQueueName());

        candidates.putAll(simplifiedCandidates);
        return currRecall;

    }

    @Override
    public String toString() {
        return "Region24hHotCandidate{" +
                "myCandidates=" + myCandidates +
                ", rankerItemsList=" + rankerItemsList +
                ", children=" + children +
                ", items=" + items +
                '}';
    }
}