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

import com.tzld.piaoquan.ad.engine.commons.redis.AlgorithmRedisHelper;
import com.tzld.piaoquan.ad.engine.service.predict.calculator.ThresholdPredictCalculator;
import com.tzld.piaoquan.ad.engine.service.predict.helper.RuleParamHelper;
import com.tzld.piaoquan.ad.engine.service.predict.param.ThresholdCalculateParam;
import com.tzld.piaoquan.ad.engine.service.predict.param.ThresholdPredictModelParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

@Component
public class MultiplyThresholdPredictModel  extends ThresholdPredictModel{

    @Autowired
    RuleParamHelper paramHelper;

    @Autowired
    AlgorithmRedisHelper redisHelper;
    /**
     * setModelName
     */
    @Override
    String initName() {
        return "multiply";
    }

    @Override
    public Map<String, Object> predict(ThresholdPredictModelParam modelParam) {

        Map<String, Object> result = new HashMap<>();

        // Get group and video share rates
        Double groupShareRate = paramHelper.getGroupShareRate(modelParam.getAbtestParam(),modelParam.getDate(),modelParam.getMidGroup());

        Double videoShareRate = paramHelper.getVideoShareRate(modelParam.getAbtestParam(), modelParam.getDate(),modelParam.getVideoId());

        // Get group and video out rates
        Double groupOutRate = paramHelper.getGroupOutRate(modelParam.getAbtestParam(),modelParam.getDate(),modelParam.getMidGroup());

        Double videoOutRate = paramHelper.getVideoOutRate(modelParam.getAbtestParam(), modelParam.getDate(),modelParam.getVideoId());

        // Calculate mid-video prediction result
        if (groupShareRate == null || videoShareRate == null || groupOutRate == null || videoOutRate == null) {
            return null;
        }
        ThresholdCalculateParam calculateParam=ThresholdCalculateParam.builder()
                .groupOutRate(groupOutRate)
                .groupShareRate(groupShareRate)
                .videoOutRate(videoOutRate)
                .videoShareRate(videoShareRate).build();
        double midVideoPredictRes = ThresholdPredictCalculator.multiplyCalculate(calculateParam);

        // Get the threshold
        double threshold = paramHelper.getThreshold(
                modelParam.getAbtestId(),
                modelParam.getAbTestConfigTag(),
                modelParam.getAbTestCode(),
                modelParam.getMidGroup(),
                modelParam.getCareModelStatus(),
                modelParam.getAbtestParam()
        );

        // Threshold check
        int adPredict=midVideoPredictRes > threshold?2:1;
//            if (midVideoPredictRes > threshold) {
//                // Greater than the threshold, show the ad
//                adPredict = 2;
//            } else {
//                // Otherwise, do not show the ad
//                adPredict = 1;
//            }
        result.put("mid_group", modelParam.getMidGroup());
        result.put("group_share_rate", groupShareRate);
        result.put("video_share_rate", videoShareRate);
        result.put("group_out_rate", groupOutRate);
        result.put("video_out_rate", videoOutRate);
        //中间计算参数 返回结果目前ad用不到
//        result.put("group_rate", groupRate);
//        result.put("video_rate", videoRate);
        result.put("mid_video_predict_res", midVideoPredictRes);
        result.put("threshold", threshold);
        result.put("ad_predict", adPredict);
        return result;
    }
}
