package com.tzld.piaoquan.recommend.server.framework.merger;


import com.google.common.collect.Sets;
import com.google.common.base.Function;
import com.tzld.piaoquan.recommend.server.common.base.RankItem;
import org.apache.commons.lang3.tuple.Pair;

import javax.annotation.Nullable;


public class SimilarityUtils {

    public static Function<Pair<RankItem, RankItem>, Boolean> getIsSameUserTagOrCategoryFunc() {
        return new Function<Pair<RankItem, RankItem>, Boolean>() {
            @Nullable
            @Override
            public Boolean apply(@Nullable Pair<RankItem, RankItem> itemPair) {
                return isSameCategory(itemPair.getLeft(), itemPair.getRight()) || isSameUserTag(itemPair.getLeft(), itemPair.getRight());
            }
        };
    }

    public static Function<Pair<RankItem, RankItem>, Boolean> getIsSameTitleTagFunc() {
        return new Function<Pair<RankItem, RankItem>, Boolean>() {
            @Nullable
            @Override
            public Boolean apply(@Nullable Pair<RankItem, RankItem> itemPair) {
                return isSameTitleTag(itemPair.getLeft(), itemPair.getRight());
            }
        };
    }

    public static Function<Pair<RankItem, RankItem>, Boolean> getIsSameUserTagFunc() {
        return new Function<Pair<RankItem, RankItem>, Boolean>() {
            @Nullable
            @Override
            public Boolean apply(@Nullable Pair<RankItem, RankItem> itemPair) {
                return isSameUserTag(itemPair.getLeft(), itemPair.getRight());
            }
        };
    }

    public static Function<Pair<RankItem, RankItem>, Boolean> getIsSameCategoryFunc() {
        return new Function<Pair<RankItem, RankItem>, Boolean>() {
            @Nullable
            @Override
            public Boolean apply(@Nullable Pair<RankItem, RankItem> itemPair) {
                return isSameCategory(itemPair.getLeft(), itemPair.getRight());
            }
        };
    }

    public static Function<Pair<RankItem, RankItem>, Boolean> getIsUserTagFunc() {
        return new Function<Pair<RankItem, RankItem>, Boolean>() {
            @Nullable
            @Override
            public Boolean apply(@Nullable Pair<RankItem, RankItem> itemPair) {
                return isSameUserTag(itemPair.getLeft(), itemPair.getRight());
            }
        };
    }

    public static boolean isSameCategory(RankItem rankerItem1, RankItem rankerItem2) {
        if (rankerItem1 == null || rankerItem2 == null ||
                rankerItem1.getRankItemCategories() == null ||
                rankerItem2.getRankItemCategories() == null) {
            return false;
        }

        return !Sets.intersection(rankerItem1.getRankItemCategories().keySet(),
                        rankerItem2.getRankItemCategories().keySet())
                .isEmpty();
    }

    public static boolean isSameTitleTag(RankItem rankerItem1, RankItem rankerItem2) {
        if (rankerItem1 == null || rankerItem2 == null ||
                rankerItem1.getRankItemTitleTags() == null ||
                rankerItem2.getRankItemTitleTags() == null) {
            return false;
        }
        return !Sets.intersection(rankerItem1.getRankItemTitleTags().keySet(),
                        rankerItem2.getRankItemTitleTags().keySet())
                .isEmpty();
    }

    public static boolean isSameUserTag(RankItem rankerItem1, RankItem rankerItem2) {
        if (rankerItem1 == null || rankerItem2 == null ||
                rankerItem1.getRankItemUserTags() == null ||
                rankerItem2.getRankItemUserTags() == null) {
            return false;
        }
        return !Sets.intersection(rankerItem1.getRankItemUserTags().keySet(),
                        rankerItem2.getRankItemUserTags().keySet())
                .isEmpty();
    }

    public static boolean isSameQueue(RankItem rankerItem1, RankItem rankerItem2) {
        if (rankerItem1 == null || rankerItem2 == null ||
                rankerItem1.getQueue() == null) {
            return false;
        }
        return rankerItem1.getQueue().equals(rankerItem2.getQueue());
    }

    public static boolean isSameSubCategory(RankItem item1, RankItem item2) {
        if (item1 == null || item2 == null || item1.getRankItemSubCategories() == null
                || item2.getRankItemSubCategories() == null) {
            return false;
        }

        return !Sets.intersection(item1.getRankItemSubCategories().keySet(),
                        item2.getRankItemSubCategories().keySet())
                .isEmpty();
    }
}
