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

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.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.CommonCollectionUtils;
import com.tzld.piaoquan.recommend.server.util.JSONUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.RandomUtils;
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 class RankStrategy4RegionMergeModelBasic extends RankService {
    @ApolloJsonValue("${rank.score.merge.weight:}")
    private Map<String, Double> mergeWeight;
    @ApolloJsonValue("${RankStrategy4DensityFilterV2:}")
    private Map<String, Map<String, Map<String, String>>> filterRules = new HashMap<>();
    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 List<Video> mergeAndRankRovRecall(RankParam param) {
        List<Video> result = new ArrayList<>();
        return result;
    }

    public double calNewVideoScore(Map<String, String> itemBasicMap) {
        double existenceDays = Double.valueOf(itemBasicMap.getOrDefault("existence_days", "30"));
        if (existenceDays > 5) {
            return 0.0;
        }
        double score = 1.0 / (existenceDays + 10.0);
        return score;
    }

    public double calTrendScore(List<Double> data) {
        double sum = 0.0;
        int size = data.size();
        for (int i = 0; i < size - 4; ++i) {
            sum += data.get(i) - data.get(i + 4);
        }
        if (sum * 10 > 0.6) {
            sum = 0.6;
        } else {
            sum = sum * 10;
        }
        if (sum > 0) {
            // 为了打断点
            sum = sum;
        }
        return sum;
    }
    public Double calScoreWeightNoTimeDecay(List<Double> data) {
        Double up = 0.0;
        Double down = 0.0;
        for (int i = 0; i < data.size(); ++i) {
            up += 1.0 * data.get(i);
            down += 1.0;
        }
        return down > 1E-8 ? up / down : 0.0;
    }

    public List<Double> getRateData(List<Double> ups, List<Double> downs, Double up, Double down) {
        List<Double> data = new LinkedList<>();
        for (int i = 0; i < ups.size(); ++i) {
            if (ExtractorUtils.isDoubleEqualToZero(downs.get(i) + down)) {
                data.add(0.0);
            } else {
                data.add(
                        (ups.get(i) + up) / (downs.get(i) + down)
                );
            }
        }
        return data;
    }

    public List<Double> getStaticData(Map<String, Map<String, Double>> itemRealMap,
                                      List<String> datehours, String key) {
        List<Double> views = new LinkedList<>();
        Map<String, Double> tmp = itemRealMap.getOrDefault(key, new HashMap<>());
        for (String dh : datehours) {
            views.add(tmp.getOrDefault(dh, 0.0D) +
                    (views.isEmpty() ? 0.0 : views.get(views.size() - 1))
            );
        }
        return views;
    }

    public List<RankItem> model(List<Video> videos, RankParam param,
                                List<String> rtFeaPart) {
        List<RankItem> result = new ArrayList<>();
        if (videos.isEmpty()) {
            return result;
        }


        List<RankItem> rankItems = CommonCollectionUtils.toList(videos, RankItem::new);
        List<Long> videoIds = CommonCollectionUtils.toListDistinct(videos, Video::getVideoId);

        // 2-2: item 实时特征处理
        List<String> videoRtKeys2 = videoIds.stream().map(r -> "item_rt_fea_1h_" + r)
                .collect(Collectors.toList());
        List<String> videoRtFeatures = this.redisTemplate.opsForValue().multiGet(videoRtKeys2);


        if (videoRtFeatures != null) {
            int j = 0;
            for (RankItem item : rankItems) {
                String vF = videoRtFeatures.get(j);
                ++j;
                if (vF == null) {
                    continue;
                }
                Map<String, String> vfMap = new HashMap<>();
                Map<String, Map<String, Double>> vfMapNew = new HashMap<>();
                try {
                    vfMap = JSONUtils.fromJson(vF, new TypeToken<Map<String, String>>() {
                    }, vfMap);

                    for (Map.Entry<String, String> entry : vfMap.entrySet()) {
                        String value = entry.getValue();
                        if (value == null) {
                            continue;
                        }
                        String[] var1 = value.split(",");
                        Map<String, Double> tmp = new HashMap<>();
                        for (String var2 : var1) {
                            String[] var3 = var2.split(":");
                            tmp.put(var3[0], Double.valueOf(var3[1]));
                        }
                        vfMapNew.put(entry.getKey(), tmp);
                    }
                    item.setItemRealTimeFeature(vfMapNew);
                } catch (Exception e) {
                    log.error(String.format("parse video item_rt_fea_1h_ json is wrong in {} with {}", this.CLASS_NAME, e));
                }
            }
        }

        return rankItems;
    }

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

        //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 根据实验号解析阿波罗参数。
        String abCode = param.getAbCode();
        Map<String, Map<String, String>> rulesMap = this.filterRules.getOrDefault(abCode, new HashMap<>(0));

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

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

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

        //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 (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 void getVideoFeatureFromRedis(List<RankItem> items){
        List<Long> videoIds = CommonCollectionUtils.toListDistinct(items, RankItem::getVideoId);
        List<String> videoKeys = videoIds.stream().map(r -> "item_rt_fea_1hrootall_" + r)
                .collect(Collectors.toList());
        List<String> videoRtFeatures = this.redisTemplate.opsForValue().multiGet(videoKeys);
        int j = 0;
        if (videoRtFeatures != null) {
            for (RankItem item : items) {
                String vF = videoRtFeatures.get(j);
                ++j;
                if (vF == null) {
                    continue;
                }
                Map<String, String> vfMap = new HashMap<>();
                Map<String, Map<String, Double>> vfMapNew = new HashMap<>();
                try {
                    vfMap = JSONUtils.fromJson(vF, new TypeToken<Map<String, String>>() {
                    }, vfMap);
                    for (Map.Entry<String, String> entry : vfMap.entrySet()) {
                        String value = entry.getValue();
                        if (value == null) {
                            continue;
                        }
                        String[] var1 = value.split(",");
                        Map<String, Double> tmp = new HashMap<>();
                        for (String var2 : var1) {
                            String[] var3 = var2.split(":");
                            tmp.put(var3[0], Double.valueOf(var3[1]));
                        }
                        vfMapNew.put(entry.getKey(), tmp);
                    }
                    item.setItemRealTimeRootFeature(vfMapNew);
                } catch (Exception e) {
                    log.error(String.format("parse video item_rt_fea_1hrootall_ json is wrong in {} with {}", this.CLASS_NAME, e));
                }
            }
        }
    }

    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);
    }

    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");
        }
    }


    public static void main(String[] args) {

    }

    List<Double> help(String s, String date, Integer h) {
        Map<String, Double> maps = Arrays.stream(s.split(",")).map(pair -> pair.split(":"))
                .collect(Collectors.toMap(
                        arr -> arr[0],
                        arr -> Double.valueOf(arr[1])
                ));
        List<String> datehours = new LinkedList<>(); // 时间是倒叙的
        List<Double> result = new ArrayList<>();
        for (int i = 0; i < h; ++i) {
            Double d = (result.isEmpty() ? 0.0 : result.get(result.size() - 1));
            result.add(d + maps.getOrDefault(date, 0D));
            datehours.add(date);
            date = ExtractorUtils.subtractHours(date, 1);
        }
        return result;
    }
    public double calVovScore(RankItem item, Map<String, Map<String, String>> vid2VovFeatureMap){
        String id = item.getVideoId() + "";
        Map<String, String> featureMap = vid2VovFeatureMap.getOrDefault(id, new HashMap<>());
        double numerator = 0D;
        final Set<String> ups = new HashSet<>(Arrays.asList(
                "1_vovh0分子", "2_vovh1分子", "3_vovh2分子", "4_vovh3分子", "7_vovh6分子", "13_vovh12分子", "25_vovh24分子", "2_vovd1分子"
        ));
        for (String key: ups){
            numerator += Double.parseDouble(featureMap.getOrDefault(key, "0"));
        }
        double denominator = 0D;
        final Set<String> downs = new HashSet<>(Arrays.asList(
                "1_vovh分母", "2_vovh分母", "3_vovh分母", "4_vovh分母", "7_vovh分母", "13_vovh分母", "25_vovh分母", "2_vovd分母"
        ));
        for (String key: downs){
            denominator += Double.parseDouble(featureMap.getOrDefault(key, "0"));
        }
        item.getScoresMap().put("numerator", numerator);
        item.getScoresMap().put("denominator", denominator);
        return denominator != 0.0? numerator / denominator: 0.0;
    }

}
