package com.tzld.piaoquan.ad.engine.service.predict.model.threshold;

import com.alibaba.fastjson.JSONObject;
import com.tzld.piaoquan.ad.engine.commons.util.JSONUtils;
import com.tzld.piaoquan.ad.engine.service.predict.container.RandWContainer;
import com.tzld.piaoquan.ad.engine.service.predict.param.ThresholdPredictModelParam;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 676实验
 */
@Component
public class RandomPredict673Model extends ThresholdPredictModel {
    private final static Logger log = LoggerFactory.getLogger(RandomPredict673Model.class);

    @Override
    String initName() {
        return "random673";
    }


    @Override
    public Map<String, Object> predict(ThresholdPredictModelParam modelParam) {
        // 如果用户的外部来源和分享所属层为空，则跳过
        if (StringUtils.isAnyBlank(modelParam.getUserExternalSource(), modelParam.getShareLayer())) {
            return null;
        }

        //  如果配置不存在，则跳过
        String abParamKey = String.format("%s:%s", modelParam.getUserExternalSource(), modelParam.getShareLayer());
        if (!modelParam.getExtraParam().containsKey(abParamKey)) {
            return null;
        }

        Map<String, Object> abParam = new HashMap<>();
        try {
            abParam.putAll((Map<String, ?>) modelParam.getExtraParam().get(abParamKey));
        } catch (Exception e) {
            log.error("get abParam error: ", e);
        }
        int hash = modelParam.getMid().hashCode();
        hash = hash < 0 ? -hash : hash;
        double score = (hash + RandWContainer.getRandW()) % 100 / 100d;

        String keySuffix = modelParam.getUserExtraFuture("shareType").toString()
                .replace("return", "")
                .replace("mids", "");
        Integer appType = modelParam.getAppType();

        String thresholdParamKey = appType + "_" + keySuffix;

        double threshold = Double.parseDouble(abParam.getOrDefault(thresholdParamKey, -1).toString());
        if (threshold < 0d) {
            thresholdParamKey = "default_" + keySuffix;
            threshold = Double.parseDouble(abParam.getOrDefault(thresholdParamKey, "-1").toString());
        }
        Map<String, Object> result = new HashMap<>();
        result.put("ad_predict", score < threshold ? 2 : 1);
        result.put("score", score);
        result.put("threshold", threshold);
        result.put("model", this.initName());

        JSONObject logJson = new JSONObject();
        logJson.putAll(result);
        logJson.put("mid", modelParam.getMid());
        logJson.put("appType", appType);
        logJson.put("expId", "673");
        logJson.put("userExternalSource", modelParam.getUserExternalSource());
        logJson.put("shareType", modelParam.getUserExtraFuture("shareType").toString());
        logJson.put("shareLayer", modelParam.getShareLayer());
        logJson.put("thresholdParamKey", thresholdParamKey);
        logJson.put("adPlatformType", modelParam.getAdPlatformType());

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


        return result;
    }

}
