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.*;

@Component
public class BasicThresholdPredictModel extends ThresholdPredictModel{

    @Autowired
    RuleParamHelper paramHelper;

    @Autowired
    AlgorithmRedisHelper redisHelper;
    @Override
    String initName() {
        return "basic";
    }

    @Override
    public Map<String, Object> predict(ThresholdPredictModelParam modelParam) {
        return predictWithRateProcess(modelParam,modelParam.getMidGroup());
    }

    public Map<String, Object> predictWithRateProcess(ThresholdPredictModelParam modelParam,String midGroup){



//        Double groupShareRate=paramHelper.getGroupShareRate(modelParam.getAbtestParam(),modelParam.getDate(),midGroup);
        Double groupShareRate=paramHelper.getGroupShareRateForBasic(modelParam.getAbtestParam(),modelParam.getDate(),midGroup);

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

        double threshold = paramHelper.getThreshold(
                modelParam.getAbtestId(),
                modelParam.getAbTestConfigTag(),
                modelParam.getAbTestCode(),
                midGroup,
                modelParam.getCareModelStatus(),
                modelParam.getAbtestParam()
        );

        ThresholdCalculateParam calculateParam=ThresholdCalculateParam.builder()
                .groupShareRate(groupShareRate)
                .videoShareRate(videoShareRate).build();

        Double midVideoPredictRes=ThresholdPredictCalculator.basicCalculate(calculateParam);

        int adPredict=midVideoPredictRes > threshold?2:1;
        Map<String, Object> result=new HashMap<>();
        result.put("mid_group", midGroup);
        result.put("group_share_rate", groupShareRate);
        result.put("video_share_rate", videoShareRate);
        result.put("mid_video_predict_res", midVideoPredictRes);
        result.put("threshold", threshold);
        result.put("ad_predict", adPredict);

        return result;
    }
}
