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 Region3hHotCandidate extends IndexCandidateQueue {

    public static final String ItemType = "video";

    public Region3hHotCandidate(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<>();

        BigDecimal recallNumBigDecimal = BigDecimal.valueOf(recallNum);
        int rosNum = recallNumBigDecimal.multiply(BigDecimal.valueOf(0.2)).intValue();
        int rovNum = recallNumBigDecimal.multiply(BigDecimal.valueOf(0.5)).intValue();
        int shortRovNum = recallNumBigDecimal.multiply(BigDecimal.valueOf(0.2)).intValue();
        int strNum = recallNumBigDecimal.multiply(BigDecimal.valueOf(0.1)).intValue();


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

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

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

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

        candidates.putAll(simplifiedCandidates);
        return currRecall;
    }

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