package com.aliyun.odps.spark.examples.myUtils;

import examples.utils.SimilarityUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FeatureUtils {
    public static final String cate1Attr = "cate1_list";
    public static final String cate2Attr = "cate2";
    public static final String festive1Attr = "festive_label1";
    private static final Map<String, Integer> cate1MAP = new HashMap<>();
    private static final Map<String, Integer> cate2MAP = new HashMap<>();
    private static final Map<String, Integer> festive1Map = new HashMap<>();
    public static final double twoMonthMs = 2 * 30 * 24 * 3600 * 1000.0;

    private static final String goodMorningRegex = "(早安|早上好|早晨好|上午好)";
    private static final String goodAfternoonRegex = "(午安|中午好|下午好)";
    private static final String goodEveningRegex = "(晚安|晚上好)";
    private static final Pattern goodMorningPattern = Pattern.compile(goodMorningRegex);
    private static final Pattern goodAfternoonPattern = Pattern.compile(goodAfternoonRegex);
    private static final Pattern goodEveningPattern = Pattern.compile(goodEveningRegex);

    static {
        cate1MAP.put("情感", 1);
        cate1MAP.put("音乐", 2);
        cate1MAP.put("搞笑", 3);
        cate1MAP.put("生活记录", 4);
        cate1MAP.put("医疗健康", 5);
        cate1MAP.put("文化", 6);
        cate1MAP.put("旅行", 7);
        cate1MAP.put("美食", 8);
        cate1MAP.put("历史", 9);
        cate1MAP.put("科普", 10);
        cate1MAP.put("艺术", 11);
        cate1MAP.put("综艺", 12);
        cate1MAP.put("时政", 13);
        cate1MAP.put("三农", 14);
        cate1MAP.put("舞蹈", 15);

        cate2MAP.put("人生忠告", 1);
        cate2MAP.put("知识科普", 2);
        cate2MAP.put("祝福音乐", 3);
        cate2MAP.put("生活小妙招", 4);
        cate2MAP.put("节日祝福", 5);
        cate2MAP.put("搞笑段子", 6);
        cate2MAP.put("怀念时光", 7);
        cate2MAP.put("早中晚好", 8);
        cate2MAP.put("风景实拍", 9);
        cate2MAP.put("动物萌宠", 10);
        cate2MAP.put("美食教程", 11);
        cate2MAP.put("国家力量", 12);
        cate2MAP.put("中国历史影像", 13);
        cate2MAP.put("杂技柔术", 14);
        cate2MAP.put("健康知识", 15);
        cate2MAP.put("传统文化", 16);
        cate2MAP.put("长寿知识", 17);
        cate2MAP.put("社会风气", 18);
        cate2MAP.put("大型集体艺术", 19);
        cate2MAP.put("省份城市亮点", 20);
        cate2MAP.put("民生政策", 21);
        cate2MAP.put("健身操", 22);
        cate2MAP.put("亲情音乐", 23);
        cate2MAP.put("天气变化", 24);
        cate2MAP.put("人财诈骗", 25);
        cate2MAP.put("正能量剧情", 26);

        festive1Map.put("节假日", 1);
        festive1Map.put("问候语", 2);
        festive1Map.put("祝福语", 3);
        festive1Map.put("节气", 4);
        festive1Map.put("热点", 5);
        festive1Map.put("人物", 6);
    }

    public static int getAttrId(String cate, String value) {
        switch (cate) {
            case cate1Attr:
                return cate1MAP.getOrDefault(value, 0);
            case cate2Attr:
                return cate2MAP.getOrDefault(value, 0);
            case festive1Attr:
                return festive1Map.getOrDefault(value, 0);
            default:
                return 0;
        }
    }

    public static int judgeVideoTimeType(String s) {
        if (null != s && !s.isEmpty()) {
            Matcher morning = goodMorningPattern.matcher(s);
            if (morning.find()) {
                return 1;
            }
//            Matcher afternoon = goodAfternoonPattern.matcher(s);
//            if (afternoon.find()) {
//                return 2;
//            }
//            Matcher evening = goodEveningPattern.matcher(s);
//            if (evening.find()) {
//                return 3;
//            }
        }
        return 0;
    }

    public static double getTimeDiff(long currentMs, long historyMs) {
        double diff = (currentMs - historyMs) / twoMonthMs;
        if (diff > 1.0) {
            diff = 1.0;
        }
        return diff;
    }

    public static double log1(double data) {
        if (data <= 0) {
            return 0D;
        }
        return Math.log(data + 1.0);
    }

    public static double log1(double data, double scale) {
        if (data <= 0) {
            return 0D;
        }
        return Math.log(data + 1.0) / scale;
    }

    public static double plusSmooth(double a, double b, double plus) {
        if (a == 0 || b == 0) {
            return 0D;
        }
        return a / (b + plus);
    }

    public static double wilsonScore(double click, double exposure) {
        if (exposure <= 0) {
            return 0.0;
        }

        double z = 1.96;
        double ctr = click / exposure;
        if (ctr > 1.0) {
            ctr = 1.0;
        }

        double numerator_1 = ctr + Math.pow(z, 2) / (2 * exposure);
        double numerator_2 = z * Math.sqrt((ctr * (1 - ctr)) / exposure + Math.pow(z / (2 * exposure), 2));
        double denominator = 1.0 + Math.pow(z, 2) / exposure;
        return (numerator_1 - numerator_2) / denominator;
    }

    public static boolean greaterThanZero(double value) {
        return value > 1E-8;
    }

    public static Double[] funcC34567ForTagsNew(String tags, String title) {
        String[] tagsList = tags.split(",");
        int d1 = 0;
        List<String> d2 = new ArrayList<>();
        double d3 = 0.0;
        double d4 = 0.0;

        for (String tag : tagsList) {
            if (title.contains(tag)) {
                d1++;
                d2.add(tag);
            }
            double score = SimilarityUtils.word2VecSimilarity(tag, title);
            if (score > d3) {
                d3 = score;
            }
            d4 += score;
        }

        d4 = (tagsList.length > 0) ? d4 / tagsList.length : d4;

        // 使用数组来返回多个值
        Double[] result = {(double) d1, d3, d4};
        return result;
    }
}
