package com.tzld.piaoquan.ad.engine.commons.util;

import com.tzld.piaoquan.ad.engine.commons.helper.NewExpHelper;
import org.apache.commons.collections4.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

public class AbUtil {

    public static final List<String> adAlgExpCode = Arrays.asList("679", "680", "683", "687", "688");

    public static Set<String> unfoldAllExpCode(List<Map<String, String>> adAbMap) {
        if (CollectionUtils.isEmpty(adAbMap)) {
            return new HashSet<>();
        }
        return adAbMap.stream()
                .map(m -> m.get("abExpCode"))
                .filter(Objects::nonNull)
                .map(Object::toString)
                .collect(Collectors.toSet());
    }

    /**
     * 判断用户是否在某个实验中，包含在新实验系统和旧实验系统
     */
    public static boolean isInAbExp(Collection<String> expCodeSet, Object appType, int newExpGroup, String expCode) {
        if (CollectionUtils.isEmpty(expCodeSet)) {
            return false;
        }
        if (expCodeSet.contains(expCode)) {
            return true;
        }

        if (expCodeSet.contains(NewExpHelper.flagId)) {
            return NewExpHelper.checkInNewExpGroup(appType.toString(), newExpGroup, expCode);
        }
        return false;
    }

    public static String chooseAdAlgExpCode(Set<String> expCodeSet, Object appType, int newExpGroup) {
        for (String expCode : adAlgExpCode) {
            if (AbUtil.isInAbExp(expCodeSet, appType, newExpGroup, expCode)) {
                return expCode;
            }
        }
        return "";
    }
}
