package com.tzld.piaoquan.ad.engine.service.predict.v2;

import com.alibaba.fastjson.JSONObject;
import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValue;
import com.tzld.piaoquan.ad.engine.commons.util.JSONUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

@Slf4j
@Service
public class PredictStrategyBy819 extends BasicPredict {

    @ApolloJsonValue("${experiment.819.root.session.id.tail.config:[]}")
    private List<RootSessionIdTailConfigItem> configItems;

    @Override
    public String name() {
        return "819";
    }

    @Override
    public Map<String, Object> predict(PredictContext ctx) {
        Map<String, Object> returnMap = new HashMap<>();
        String rootSessionId = ctx.getRootSessionId();
        String userLayer = ctx.getUserLayer();
        String shareType = ctx.getShareType();
        returnMap.put("ecpm_model","819");
        if (CollectionUtils.isEmpty(configItems) || StringUtils.isAnyBlank(rootSessionId, userLayer, shareType)) {
            returnMap.put("ecpm_msg","no_config_error");
            return returnMap;
        }
        String tail = rootSessionId.substring(rootSessionId.length() - 1);
        shareType = ctx.getShareType()
                .replace("return", "")
                .replace("mids", "");

        for (RootSessionIdTailConfigItem item : configItems) {
            if (item.getTail().contains(tail)) {
                String thresholdKey = "";
                // 生成可用的Key列表
                List<String> keys = Arrays.asList(userLayer + "_" + shareType, "default_" + shareType, "default_threshold");
                for (String key : keys) {
                    if (item.getConfig().containsKey(key)) {
                        thresholdKey = key;
                        break;
                    }
                }

                double ecpm = item.getConfig().getOrDefault(thresholdKey, -1.0);
                returnMap.put("ecpm_value", ecpm);
                returnMap.put("ecpm_thresholdKey", thresholdKey);
                returnMap.put("ecpm_userLayer", userLayer);
                returnMap.put("ecpm_rootSessionId", ctx.getRootSessionId());
                JSONObject logJson = new JSONObject();
                logJson.putAll(returnMap);
                logJson.put("mid", ctx.getMid());
                logJson.put("appType", ctx.getAppType());
                logJson.put("rootSessionIdTail", tail);
                logJson.put("shareType", shareType);

                logJson.put("expId", "userLayerRootSessionIdTailExp");
                logJson.put("thresholdParamKey", thresholdKey);
                logJson.put("adPlatformType", ctx.getAdPlatformType());
                logJson.put("abCode", ctx.getAdAbCode());
                logJson.put("configItem", item);
                logJson.put("rootSessionId", ctx.getRootSessionId());

                log.info("广告跳出选择 -- 819实验结果: {}, 参数: {}",
                        JSONUtils.toJson(returnMap), logJson.toJSONString());

                return returnMap;
            }
        }
        returnMap.put("ecpm_msg","未命中");
        return returnMap;
    }

}
