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

import com.google.common.reflect.TypeToken;
import com.tzld.piaoquan.ad.engine.commons.util.JSONUtils;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class NewExpHelper {
    private final static Logger log = LoggerFactory.getLogger(NewExpHelper.class);
    public static Map<String, Set<String>> appExpIdCache = new HashMap<>();
    public static Map<String, Exp> expIdAndRangeCache = new HashMap<>();

    public static String flagId;

    @Value("${ad.new.exp.flag.id:0}")
    private void setFlagId(String flagId) {
        NewExpHelper.flagId = flagId;
    }

    @Value("${new.exp,config.v2:[]}")
    private void setNewExpInfo(String str) {
        List<ExpConfig> expConfigs = JSONUtils.fromJson(str, new TypeToken<List<ExpConfig>>() {
        }, Collections.emptyList());
        for (ExpConfig expConfig : expConfigs) {
            String appId = expConfig.getAppType();
            for (Layer layer : expConfig.layers) {
                if (layer.layerId.equals("ad-server")) {
                    Set<String> expIdSet = new HashSet<>();
                    for (Exp exp : layer.getExps()) {
                        expIdSet.add(exp.getExpId());
                        expIdAndRangeCache.put(exp.getExpId(), exp);
                    }
                    appExpIdCache.put(appId, expIdSet);
                }
            }
        }
    }

    public static boolean checkInNewExpGroup(String appId, int groupNumber, String expId) {
        try {
            if (appExpIdCache.get(appId) == null || !appExpIdCache.get(appId).contains(expId)) {
                return false;
            }
            if (expIdAndRangeCache.get(expId).isAllEnter() && groupNumber < 0) {
                return true;
            }
            return expIdAndRangeCache.get(expId).getRange()[0] <= groupNumber && expIdAndRangeCache.get(expId).getRange()[1] >= groupNumber;
        } catch (Exception e) {
            return false;
        }
    }

    public static boolean checkInNewExpGroup(String appId, int groupNumber, Collection<String> expIds) {
        if (CollectionUtils.isEmpty(expIds)) {
            return false;
        }
        for (String expId : expIds) {
            try {
                if (appExpIdCache.get(appId) == null || !appExpIdCache.get(appId).contains(expId)) {
                    continue;
                }
                if (expIdAndRangeCache.get(expId).isAllEnter() && groupNumber < 0) {
                    return true;
                }
                if (expIdAndRangeCache.get(expId).getRange()[0] <= groupNumber && expIdAndRangeCache.get(expId).getRange()[1] >= groupNumber) {
                    return true;
                }
            } catch (Exception e) {
                log.warn("svc=checkInNewExpGroup appId={} groupNumber={} expId={}", appId, groupNumber, expId);
            }
        }
        return false;
    }

    @Data
    public static class ExpConfig {
        private String appType;
        private List<Layer> layers;

    }

    @Data
    public static class Layer {
        private String layerId;
        private int bucketNum;
        private List<Exp> exps;
        private Map<String, String> groupRule;

    }

    @Data
    public static class Exp {
        private String expId;
        private int[] range;
        private boolean allEnter = false;
        private Map<String, Object> param;
    }

}
