package com.tzld.longarticle.recommend.test.merger;

import com.tzld.piaoquan.recommend.feature.client.FeatureClient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.redis.RedisReactiveAutoConfiguration;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter;



import com.tzld.piaoquan.recommend.server.common.base.RankItem;
import com.tzld.piaoquan.recommend.server.framework.common.User;
import com.tzld.piaoquan.recommend.server.framework.merger.MergeRule;
import com.tzld.piaoquan.recommend.server.framework.merger.MergeUtils;
import com.tzld.piaoquan.recommend.server.framework.merger.SimilarityUtils;
import com.tzld.piaoquan.recommend.server.gen.recommend.RecommendRequest;
import org.apache.commons.lang3.tuple.Pair;
//import org.junit.Assert;
//import org.junit.Test;
import com.google.common.base.Function;

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

public class MergeUtilsTest {

//    @Test
    public void mergeWithProtectionTest() {
        User user = new User();

        Map<String, Pair<MergeRule, List<RankItem>>> rankerItemsList = new HashMap<String, Pair<MergeRule, List<RankItem>>>();
        MergeRule rule1 = new MergeRule();
        rule1.queueName = "rule1";
        rule1.minMergeNum = 2;
        // 构建一个队列的ranklist
        List<RankItem> items1 = new LinkedList<RankItem>();
        RankItem item11 = new RankItem();
        item11.setScore(0.1);
        items1.add(item11);
        rankerItemsList.put(rule1.queueName, Pair.of(rule1, items1));

        List<RankItem> resultItems = new LinkedList<RankItem>();
        MergeUtils.simpleMergeWithProtection(resultItems, rankerItemsList, 2, 0);
//        Assert.assertTrue(resultItems.size() == 1);


        RankItem item12 = new RankItem();
        item12.setScore(0.2);
        // list增加第二个
        items1.add(item12);
        resultItems.clear();
        MergeUtils.simpleMergeWithProtection(resultItems, rankerItemsList, 2, 1);

        // 判断是否受到最小合并控制， 插入两个，召回两个
//        Assert.assertTrue(resultItems.size() == 2);
        resultItems.clear();

        // 插入两个，召回三个
        MergeUtils.simpleMergeWithProtection(resultItems, rankerItemsList, 3, 0);
//        Assert.assertTrue(resultItems.size() == 2);

        // 最小合并条目保护,因为min mergenum 是2
        resultItems.clear();
        MergeUtils.simpleMergeWithProtection(resultItems, rankerItemsList, 1, 0);
//        Assert.assertTrue(resultItems.size() == 2);

        // 第二个list
        List<RankItem> items2 = new LinkedList<RankItem>();
        RankItem item21 = new RankItem();
        // 一个item
        item21.setScore(0.12);
        items2.add(item21);

        // 第二个rules
        MergeRule rule2 = new MergeRule();
        rule2.queueName = "rule2";
        rule2.minMergeNum = 0;
        // 加入第二个rule
        rankerItemsList.put(rule2.queueName, Pair.of(rule2, items2));
        resultItems.clear();
        // 共同合并，因为rule2 min MergeNum =0，合并完只有两个
        MergeUtils.simpleMergeWithProtection(resultItems, rankerItemsList, 2, 0);
//        Assert.assertTrue(resultItems.size() == 2);

        // 共同合并，rule2最小合并1个，因为rule1是1个，所以合并完是3个
        rule2.minMergeNum = 1;
        resultItems.clear();
        MergeUtils.simpleMergeWithProtection(resultItems, rankerItemsList, 2, 0);
//        Assert.assertTrue(resultItems.size() == 3);

        // 第三个list， 3个item
        List<RankItem> items3 = new LinkedList<RankItem>();
        RankItem item31 = new RankItem();
        item31.setScore(0.113);
        items3.add(item31);
        RankItem item32 = new RankItem();
        item32.setScore(0.114);
        items3.add(item31);
        RankItem item33 = new RankItem();
        item33.setScore(0.123);
        items3.add(item31);

        // 第三个rule，最小merge 1
        MergeRule rule3 = new MergeRule();
        rule3.queueName = "rule3";
        rule3.minMergeNum = 1;
        rankerItemsList.put(rule3.queueName, Pair.of(rule3, items3));
        resultItems.clear();
        // rule1 2个，rule2 1个，rule3 1个，所以合并完是4个
        MergeUtils.simpleMergeWithProtection(resultItems, rankerItemsList, 2, 0);
//        Assert.assertTrue(resultItems.size() == 4);
    }

    private List<RankItem> getItems() {
        List<RankItem> items = new LinkedList<RankItem>();
        RankItem item11 = new RankItem();
        item11.setId("11");
        item11.setScore(100.0);
        Map<String, Double> categories11 = new HashMap<String, Double>();
        categories11.put("美女", 100.0);
        item11.setRankItemCategories(categories11);
        items.add(item11);

        item11 = new RankItem();
        item11.setId("22");
        item11.setScore(99.0);
        categories11 = new HashMap<String, Double>();
        categories11.put("动物", 99.0);
        item11.setRankItemCategories(categories11);
        items.add(item11);

        item11 = new RankItem();
        item11.setId("33");
        item11.setScore(98.0);
        categories11 = new HashMap<String, Double>();
        categories11.put("美女", 98.0);
        item11.setRankItemCategories(categories11);
        items.add(item11);

        item11 = new RankItem();
        item11.setScore(97.0);
        item11.setId("44");
        categories11 = new HashMap<String, Double>();
        categories11.put("美女", 97.0);
        item11.setRankItemCategories(categories11);
        items.add(item11);

        item11 = new RankItem();
        item11.setId("55");
        item11.setScore(96.0);
        categories11 = new HashMap<String, Double>();
        categories11.put("动物", 96.0);
        item11.setRankItemCategories(categories11);
        items.add(item11);

        item11 = new RankItem();
        item11.setId("66");
        item11.setScore(95.0);
        categories11 = new HashMap<String, Double>();
        categories11.put("美女", 95.0);
        item11.setRankItemCategories(categories11);
        items.add(item11);

        item11 = new RankItem();
        item11.setId("77");
        item11.setScore(94.0);
        categories11 = new HashMap<String, Double>();
        categories11.put("动物", 94.0);
        item11.setRankItemCategories(categories11);
        items.add(item11);

        item11 = new RankItem();
        item11.setId("88");
        item11.setScore(93.0);
        categories11 = new HashMap<String, Double>();
        categories11.put("美女", 93.0);
        item11.setRankItemCategories(categories11);
        items.add(item11);

        item11 = new RankItem();
        item11.setId("99");
        item11.setScore(92.0);
        categories11 = new HashMap<String, Double>();
        categories11.put("动物", 92.0);
        item11.setRankItemCategories(categories11);
        items.add(item11);

        item11 = new RankItem();
        item11.setId("100");
        item11.setScore(91.0);
        categories11 = new HashMap<String, Double>();
        categories11.put("动物", 91.0);
        item11.setRankItemCategories(categories11);
        items.add(item11);

        item11 = new RankItem();
        item11.setId("101");
        item11.setScore(90.0);
        categories11 = new HashMap<String, Double>();
        categories11.put("美女", 90.0);
        item11.setRankItemCategories(categories11);
        items.add(item11);

        item11 = new RankItem();
        item11.setId("102");
        item11.setScore(89.0);
        categories11 = new HashMap<String, Double>();
        categories11.put("动物", 89.0);
        item11.setRankItemCategories(categories11);
        items.add(item11);

        item11 = new RankItem();
        item11.setId("103");
        item11.setScore(88.0);
        categories11 = new HashMap<String, Double>();
        categories11.put("美女", 88.0);
        item11.setRankItemCategories(categories11);
        items.add(item11);

        item11 = new RankItem();
        item11.setId("104");
        item11.setScore(87.0);
        categories11 = new HashMap<String, Double>();
        categories11.put("美女", 87.0);
        item11.setRankItemCategories(categories11);
        items.add(item11);

        return items;
    }

//    @Test
    public void testDiversityRerank() {
        List<RankItem> items = new LinkedList<RankItem>();

        Function<Pair<RankItem, RankItem>, Boolean> isSameCategoryFunc = SimilarityUtils.getIsSameCategoryFunc();

        RankItem item11 = new RankItem();
        Map<String, Double> categories11 = new HashMap<String, Double>();
        categories11.put("1", 0.1);
        item11.setRankItemCategories(categories11);
        items.add(item11);

        MergeUtils.diversityRerank(items, isSameCategoryFunc, 1, 1, 1);
        MergeUtils.diversityRerank(items, isSameCategoryFunc, 2, 1, 1);
        MergeUtils.diversityRerank(items, isSameCategoryFunc, 2, 2, 1);

        RankItem item12 = new RankItem();
        Map<String, Double> categories12 = new HashMap<String, Double>();
        categories12.put("1", 0.1);
        item12.setRankItemCategories(categories12);
        item12.setScore(0.2);
        items.add(item12);

        MergeUtils.diversityRerank(items, isSameCategoryFunc, 2, 2, 1);
//        Assert.assertTrue(SimilarityUtils.isSameCategory(items.get(0), items.get(1)));

        RankItem item13 = new RankItem();
        Map<String, Double> categories13 = new HashMap<String, Double>();
        categories13.put("2", 0.1);
        item13.setRankItemCategories(categories13);
        items.add(item13);

        MergeUtils.diversityRerank(items, isSameCategoryFunc, 2, 2, 2);
//        Assert.assertTrue(SimilarityUtils.isSameCategory(items.get(0), items.get(1)));

        RankItem item14 = new RankItem();
        Map<String, Double> categories14 = new HashMap<String, Double>();
        categories14.put("1", 0.1);
        item14.setRankItemCategories(categories14);
        items.add(item14);

        MergeUtils.diversityRerank(items, isSameCategoryFunc, 3, 2, 2);
//        Assert.assertTrue(SimilarityUtils.isSameCategory(items.get(0), items.get(1)));

        MergeUtils.diversityRerank(items, isSameCategoryFunc, 3, 2, 1);
//        Assert.assertTrue(SimilarityUtils.isSameCategory(items.get(0), items.get(1)) == false);

        RankItem item15 = new RankItem();
        Map<String, Double> categories15 = new HashMap<String, Double>();
        categories15.put("2", 0.1);
        item15.setRankItemCategories(categories15);
        items.add(item15);

        MergeUtils.diversityRerank(items, isSameCategoryFunc, 4, 2, 2);
//        Assert.assertTrue(SimilarityUtils.isSameCategory(items.get(0), items.get(3)));

        MergeUtils.diversityRerank(items, isSameCategoryFunc, 4, 3, 2);
//        Assert.assertTrue(SimilarityUtils.isSameCategory(items.get(0), items.get(3)));

        MergeUtils.diversityRerank(items, isSameCategoryFunc, 4, 4, 2);
//        Assert.assertTrue(SimilarityUtils.isSameCategory(items.get(0), items.get(3)) == false);

        items = getItems();
        MergeUtils.diversityRerank(items, isSameCategoryFunc, 8, 8, 2);
//        Assert.assertTrue(SimilarityUtils.isSameCategory(items.get(0), items.get(3)) == false);
//        Assert.assertTrue(SimilarityUtils.isSameCategory(items.get(3), items.get(5)) == false);
    }
}
