package com.tzld.piaoquan.recommend.server.service.rank.strategy;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValue;
import com.google.common.reflect.TypeToken;
import com.tzld.piaoquan.recommend.server.common.base.RankItem;
import com.tzld.piaoquan.recommend.server.model.Video;
import com.tzld.piaoquan.recommend.server.service.rank.RankParam;
import com.tzld.piaoquan.recommend.server.service.rank.RankResult;
import com.tzld.piaoquan.recommend.server.service.rank.RankService;
import com.tzld.piaoquan.recommend.server.service.rank.bo.UserSRBO;
import com.tzld.piaoquan.recommend.server.service.rank.extractor.ExtractorUtils;
import com.tzld.piaoquan.recommend.server.service.rank.extractor.RankExtractorItemTags;
import com.tzld.piaoquan.recommend.server.service.rank.processor.RankProcessorBoost;
import com.tzld.piaoquan.recommend.server.service.rank.processor.RankProcessorDensity;
import com.tzld.piaoquan.recommend.server.service.rank.processor.RankProcessorInsert;
import com.tzld.piaoquan.recommend.server.service.rank.processor.RankProcessorTagFilter;
import com.tzld.piaoquan.recommend.server.util.JSONUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zhangbo
 * @desc 地域召回融合 流量池汤姆森
 */
@Service
@Slf4j
public abstract class RankStrategy4RegionMergeModelBasic extends RankService {
    @ApolloJsonValue("${RankStrategy4DensityFilterV2:}")
    private Map<String, Map<String, Map<String, String>>> filterRules = new HashMap<>();

    @ApolloJsonValue("${RankReduceByMergeCateConfig:{}}")
    private Map<String, Map<String, List<Map<String, String>>>> rankReduceConfig = new HashMap<>();


    @Value("${new.flow.pool.select.rate:1}")
    private double newFlowPoolSelectRate;

    @ApolloJsonValue("${RankReduceByFestiveConfig:{}}")
    private Map<String, String> rankReduceByFestiveConfig = new HashMap<>();

    @ApolloJsonValue("${str.plus.calibration.merge.cate2:[]}")
    private Set<String> strPlusCalibrationMergeCate2;

    public static final String STR_PLUS_CALIBRATION_ATTRIBUTE_KEY_PREFIX = "alg_rec_model_calibration_str:";
    public static final String prioriProvinceWeightPrefix = "priori_province_weight";

    String CLASS_NAME = this.getClass().getSimpleName();

    public void duplicate(Set<Long> setVideo, List<Video> videos) {
        Iterator<Video> iterator = videos.iterator();
        while (iterator.hasNext()) {
            Video v = iterator.next();
            if (setVideo.contains(v.getVideoId())) {
                iterator.remove();
            } else {
                setVideo.add(v.getVideoId());
            }
        }
    }

    @Override
    public RankResult mergeAndSort(RankParam param, List<Video> rovVideos, List<Video> flowVideos, List<Video> douHotFlowPoolVideos) {

        // 1 兜底策略，rov池子不足时，用冷启池填补。直接返回。
        if (CollectionUtils.isEmpty(rovVideos)) {
            if (param.getSize() < flowVideos.size()) {
                return new RankResult(flowVideos.subList(0, param.getSize()));
            } else {
                return new RankResult(flowVideos);
            }
        }

        // 2 根据实验号解析阿波罗参数。
        Set<String> abExpCodes = param.getAbExpCodes();
        Map<String, Map<String, String>> rulesMap = Collections.emptyMap();

        Map<String, List<Map<String, String>>> rankReduceRulesMap = Collections.emptyMap();

        if (CollectionUtils.isNotEmpty(abExpCodes)) {
            for (Map.Entry<String, Map<String, Map<String, String>>> entry : this.filterRules.entrySet()) {
                if (abExpCodes.contains(entry.getKey())) {
                    rulesMap = entry.getValue();
                    break;
                }
            }

            for (Map.Entry<String, Map<String, List<Map<String, String>>>> entry : this.rankReduceConfig.entrySet()) {
                if (abExpCodes.contains(entry.getKey())) {
                    rankReduceRulesMap = entry.getValue();
                    break;
                }
            }
        }


        // 3 标签读取
        if (rulesMap != null && !rulesMap.isEmpty()) {
            RankExtractorItemTags extractorItemTags = new RankExtractorItemTags(this.redisTemplate);
            extractorItemTags.processor(rovVideos, flowVideos, douHotFlowPoolVideos);
        }
        // 6 合并结果时间卡控
        if (rulesMap != null && !rulesMap.isEmpty()) {
            RankProcessorTagFilter.processor(rovVideos, flowVideos, douHotFlowPoolVideos, rulesMap);
        }


        // if (MapUtils.isNotEmpty(rankReduceRulesMap)) {
        //     ExtractVideoMergeCate.addMergeCate(rovVideos, flowVideos);
        // }

        // 4 rov池提权功能
        RankProcessorBoost.boostByTag(rovVideos, rulesMap);

        // 5 rov池强插功能
        RankProcessorInsert.insertByTag(param, rovVideos, rulesMap);

        // 6.品类降权
        RankProcessorBoost.boostByMergeCate(rovVideos, rankReduceRulesMap);

        // 节日视频降权
        RankProcessorBoost.boostByFestive(param, rovVideos, rankReduceByFestiveConfig);

        // 7 流量池按比例强插
        List<Video> result = new ArrayList<>();
        for (int i = 0; i < param.getTopK() && i < rovVideos.size(); i++) {
            result.add(rovVideos.get(i));
        }
        double flowPoolP = getFlowPoolP(param);
        int flowPoolIndex = 0;
        int rovPoolIndex = param.getTopK();
        for (int i = 0; i < param.getSize() - param.getTopK(); i++) {
            double rand = RandomUtils.nextDouble(0, 1);
            if (rand < flowPoolP) {
                if (flowPoolIndex < flowVideos.size()) {
                    result.add(flowVideos.get(flowPoolIndex++));
                } else {
                    break;
                }
            } else if (this.isInsertDouHotFlowPoolVideo()) {
                if (flowPoolIndex < douHotFlowPoolVideos.size()) {
                    result.add(douHotFlowPoolVideos.get(flowPoolIndex++));
                } else {
                    break;
                }
            } else {
                if (rovPoolIndex < rovVideos.size()) {
                    result.add(rovVideos.get(rovPoolIndex++));
                } else {
                    break;
                }
            }
        }
        if (rovPoolIndex >= rovVideos.size()) {
            for (int i = flowPoolIndex; i < flowVideos.size() && result.size() < param.getSize(); i++) {
                result.add(flowVideos.get(i));
            }
        }
        if (flowPoolIndex >= flowVideos.size()) {
            for (int i = rovPoolIndex; i < rovVideos.size() && result.size() < param.getSize(); i++) {
                result.add(rovVideos.get(i));
            }
        }

        // 8 合并结果密度控制
        Map<String, Integer> densityRules = new HashMap<>();
        if (rulesMap != null && !rulesMap.isEmpty()) {
            for (Map.Entry<String, Map<String, String>> entry : rulesMap.entrySet()) {
                String key = entry.getKey();
                Map<String, String> value = entry.getValue();
                if (value.containsKey("density")) {
                    densityRules.put(key, Integer.valueOf(value.get("density")));
                }
            }
        }
        Set<Long> videosSet = result.stream().map(Video::getVideoId).collect(Collectors.toSet());
        List<Video> rovRecallRankNew = rovVideos.stream().filter(r -> !videosSet.contains(r.getVideoId())).collect(Collectors.toList());
        List<Video> flowPoolRankNew = flowVideos.stream().filter(r -> !videosSet.contains(r.getVideoId())).collect(Collectors.toList());
        List<Video> resultWithDensity = RankProcessorDensity.mergeDensityControl(result,
                rovRecallRankNew, flowPoolRankNew, densityRules);

        return new RankResult(resultWithDensity);
    }

    public Map<String, Map<String, String>> getVideoRedisFeature(List<String> vids, String redisKeyPrefix) {
        List<String> keys = vids.stream().map(r -> redisKeyPrefix + r).collect(Collectors.toList());
        List<String> key2Values = this.redisTemplate.opsForValue().multiGet(keys);
        Map<String, Map<String, String>> result = new HashMap<>(vids.size());
        if (key2Values != null) {
            int j = 0;
            for (String vid : vids) {
                String vF = key2Values.get(j);
                ++j;
                if (vF == null) {
                    continue;
                }
                Map<String, String> vfMap = new HashMap<>();
                try {
                    vfMap = JSONUtils.fromJson(vF, new TypeToken<Map<String, String>>() {
                    }, vfMap);
                } catch (Exception e) {
                    log.error(String.format("parse video json is wrong on redisKeyPrefix in {} with {}", this.CLASS_NAME, redisKeyPrefix));
                }
                result.put(vid, vfMap);
            }
        }
        return result;
    }

    static class Tuple4 {
        public Map<String, String> first;
        public Map<String, String> second;
        public Map<String, String> third;

        public String name;

        public Tuple4(Map<String, String> first, Map<String, String> second, Map<String, String> third, String name) {
            this.first = first;
            this.second = second;
            this.third = third;
            this.name = name;
        }

    }

    static class Tuple3 {
        public String first;
        public List<String> second;
        public Map<String, String> third;

        public Tuple3(String first, List<String> second, Map<String, String> third) {
            this.first = first;
            this.second = second;
            this.third = third;
        }
    }

    static class Tuple2 {
        public Map<String, String> first;

        public String name;

        public Tuple2(Map<String, String> first, String name) {
            this.first = first;
            this.name = name;
        }

    }

    protected double restoreScore(double score) {
        return (0.1 * score) / (1 - 0.9 * score);
    }

    protected double restoreScore(double score, double w) {
        return (w * score) / (1 - (1 - w) * score);
    }

    Map<String, double[]> bucketsMap = new HashMap<>();
    Map<String, Double> bucketsLen = new HashMap<>();

    public void readBucketFile() {
        InputStream resourceStream = RankStrategy4RegionMergeModelBasic.class.getClassLoader().getResourceAsStream("20240609_bucket_274.txt");
        if (resourceStream != null) {
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(resourceStream))) {
                Map<String, double[]> bucketsMap = new HashMap<>();
                Map<String, Double> bucketsLen = new HashMap<>();
                String line;
                while ((line = reader.readLine()) != null) {
                    // 替换空格和换行符，过滤空行
                    line = line.replace(" ", "").replaceAll("\n", "");
                    if (!line.isEmpty()) {
                        String[] rList = line.split("\t");
                        if (rList.length == 3) {
                            String key = rList[0];
                            double value1 = Double.parseDouble(rList[1]);
                            bucketsLen.put(key, value1);
                            double[] value2 = Arrays.stream(rList[2].split(","))
                                    .mapToDouble(Double::valueOf)
                                    .toArray();
                            bucketsMap.put(key, value2);
                        }
                    }
                }
                this.bucketsMap = bucketsMap;
                this.bucketsLen = bucketsLen;
            } catch (IOException e) {
                log.error("something is wrong in parse bucket file:" + e);
            }
        } else {
            log.error("no bucket file");
        }
    }

    //------------------- 对str、ros和vor做处理，通过参数控制处理方式 -------------------
    protected double handleStr(double originStr, double calcStrMode, RankItem item, Map<String, Double> mergeWeight) {
        if (originStr == 0) {
            return 0d;
        }

        double str = originStr;
        if (calcStrMode == 1d) {
            double strPower = mergeWeight.getOrDefault("str_power", 0d);
            item.getScoresMap().put("strPower", strPower);
            str = Math.pow(originStr, strPower);
        } else if (calcStrMode == 2d) {
            double modelStrCoefficient = mergeWeight.getOrDefault("model_str_coefficient", 8d);
            item.getScoresMap().put("modelStrCoefficient", modelStrCoefficient);
            str = originStr * modelStrCoefficient;
        } else if (calcStrMode == 3d) {
            double minVal = mergeWeight.getOrDefault("min", 0d);
            double maxVal = mergeWeight.getOrDefault("max", 1d);

            double newMinVal = mergeWeight.getOrDefault("newMin", 0.99);
            double newMaxVal = mergeWeight.getOrDefault("newMax", 1d);

            str = (originStr - minVal) / (maxVal - minVal) * (newMaxVal - newMinVal) + newMinVal;

            item.getScoresMap().put("minVal", minVal);
            item.getScoresMap().put("maxVal", maxVal);
            item.getScoresMap().put("newMinVal", newMinVal);
            item.getScoresMap().put("newMaxVal", newMaxVal);
        }

        return str;
    }

    protected double handleRos(double originScoreRos, double calcRosMode, RankItem item, Map<String, Double> mergeWeight) {
        if (originScoreRos == 0) {
            return 0;
        }

        double scoreRos = originScoreRos;
        if (calcRosMode == 1d) {
            double rosPower = mergeWeight.getOrDefault("le_1_ros_power", 5d);
            if (scoreRos > 1) {
                rosPower = mergeWeight.getOrDefault("gt_1_ros_poewr", 1.5d);
            }
            item.getScoresMap().put("rosPower", rosPower);
            scoreRos = Math.pow(ExtractorUtils.inverseLog(originScoreRos), rosPower);
        } else if (calcRosMode == 2d) {
            double modelRosCoefficient = mergeWeight.getOrDefault("model_ros_coefficient", 8d);
            item.getScoresMap().put("modelRosCoefficient", modelRosCoefficient);
            scoreRos = ExtractorUtils.inverseLog(originScoreRos * modelRosCoefficient);
        } else if (calcRosMode == 3d) {
            double rosPower = mergeWeight.getOrDefault("ros_power", 5d);
            item.getScoresMap().put("rosPower", rosPower);
            scoreRos = Math.pow(ExtractorUtils.inverseLog(originScoreRos), rosPower);
        }

        return scoreRos;
    }

    protected double handleVor(double originVor, double calcVorMode, RankItem item, Map<String, Double> mergeWeight) {
        double vor = originVor;
        if (calcVorMode == 1d) {
            vor = ExtractorUtils.calLog(originVor);
        } else if (calcVorMode == 2d) {
            double vorCoefficient = mergeWeight.getOrDefault("vor_coefficient", 1d);
            item.getScoresMap().put("vorCoefficient", vorCoefficient);
            vor = vorCoefficient * originVor;
        } else if (calcVorMode == 3d) {
            double vorPower = mergeWeight.getOrDefault("vor_power", 0d);
            item.getScoresMap().put("vorPower", vorPower);
            vor = Math.pow(originVor, vorPower);
        } else if (calcVorMode == 4d) {
            vor = 1.0;
        }

        return vor;
    }

    protected Map<String, String> dumpsRtFeature(List<UserSRBO> userRTShareList) {
        if (null == userRTShareList || userRTShareList.isEmpty()) {
            return null;
        }
        Map<String, String> map = new HashMap<>();
        String rtShare = JSONUtils.toJson(userRTShareList);
        if (null != rtShare && !rtShare.isEmpty()) {
            map.put("share", rtShare);
        }
        return map;
    }

    protected Map<String, String> getContextInfo(RankParam param) {
        Map<String, String> contextInfo = new HashMap<>();
        if (null != param) {
            String province = param.getProvince();
            if (null != province && !province.isEmpty()) {
                contextInfo.put("province", province.replaceAll("省$", ""));
            }
            String city = param.getCity();
            if (null != city && !city.isEmpty()) {
                contextInfo.put("city", city.replaceAll("市$", ""));
            }
            String openGId = param.getOpenGId();
            if (null != openGId && !openGId.isEmpty()) {
                contextInfo.put("openGId", openGId);
            }
        }
        return contextInfo;
    }

    /**
     * 计算MergeCate2的str+校准系数
     */
    protected double calcStrPlusCalibrationCoefficientByMergeCate2(String mergeCate2) {
        if (CollectionUtils.isEmpty(strPlusCalibrationMergeCate2) || StringUtils.isEmpty(mergeCate2)) {
            return 1d;
        }

        if (!strPlusCalibrationMergeCate2.contains(mergeCate2)) {
            return 1d;
        }

        try {

            String key = STR_PLUS_CALIBRATION_ATTRIBUTE_KEY_PREFIX + mergeCate2;
            String value = redisTemplate.opsForValue().get(key);
            if (StringUtils.isEmpty(value)) {
                return 1d;
            }

            JSONObject valueJson = JSON.parseObject(value);
            double rStrPlus = valueJson.getDoubleValue("r_str_plus");
            double pStrPlus = valueJson.getDoubleValue("p_str_plus");
            double coefficient = rStrPlus / pStrPlus;
            if (coefficient <= 0d) {
                return 1d;
            }
            return coefficient;
        } catch (Exception e) {
            log.error("calcStrPlusCalibrationCoefficientByMergeCate2 error mergeCate2: {} \n", mergeCate2, e);
        }

        return 1d;
    }

    /**
     * 获取先验地域加权
     */
    protected Map<String, Double> getPrioriVidProvinceRovn(String province, List<RankItem> items, Map<String, Map<String, Map<String, String>>> videoBaseInfoMap) {
        Map<String, Double> map = new HashMap<>();
        try {
            if (null != province && !province.isEmpty() && null != items && !items.isEmpty() && null != videoBaseInfoMap && !videoBaseInfoMap.isEmpty()) {
                List<String> vids = new ArrayList<>();
                List<String> keys = new ArrayList<>();
                for (RankItem item : items) {
                    String vid = String.valueOf(item.getVideoId());
                    Map<String, String> videoInfo = videoBaseInfoMap.getOrDefault(vid, new HashMap<>()).getOrDefault("alg_vid_feature_basic_info", new HashMap<>());
                    String attributeProvince = videoInfo.get("attribute_province");
                    if (null != attributeProvince && !attributeProvince.isEmpty()) {
                        vids.add(vid);
                        keys.add(String.format("%s:%s:%s:%s", prioriProvinceWeightPrefix, vid, province, "rovn"));
                    }
                }
                if (!keys.isEmpty()) {
                    List<String> values = redisTemplate.opsForValue().multiGet(keys);
                    if (null != values && vids.size() == values.size()) {
                        for (int i = 0; i < vids.size(); i++) {
                            String vid = vids.get(i);
                            String value = values.get(i);
                            if (null != value && !value.isEmpty()) {
                                double v = NumberUtils.toDouble(value, -1);
                                if (v > 0) {
                                    map.put(vid, v);
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            log.error("priori province weight", e);
        }
        return map;
    }

    protected double getPrioriVidProvinceWeight(double highValue, double highWeight, double lowValue, double lowWeight, String vid, Map<String, Double> map) {
        if (null != map && map.containsKey(vid)) {
            double value = map.get(vid);
            if (value > highValue) {
                return 1.0 + (value - 1.0) * 10.0;
            } else if (value <= lowValue) {
                return value * value;
            }
        }
        return 1.0;
    }

    private boolean isInsertDouHotFlowPoolVideo() {
        double rand = RandomUtils.nextDouble(0, 1);
        return rand <= newFlowPoolSelectRate;
    }
}
