package examples.extractor;

import java.util.*;
import java.util.stream.Collectors;

public class RankExtractorItemFeature {
    public static Map<String, String> getItemRateFeature(Map<String, String> maps) {

        double d;
        Map<String, Double> result = new HashMap<>();
        d = ExtractorUtils.division("i_1day_exp_cnt", "i_1day_click_cnt", maps);
        if (!ExtractorUtils.isDoubleEqualToZero(d)){
            result.put("i_1day_ctr",d);
        }
        d = ExtractorUtils.division("i_1day_exp_cnt", "i_1day_share_cnt", maps);
        if (!ExtractorUtils.isDoubleEqualToZero(d)){
            result.put("i_1day_str",d);
        }
        d = ExtractorUtils.division("i_1day_exp_cnt", "i_1day_return_cnt", maps);
        if (!ExtractorUtils.isDoubleEqualToZero(d)){
            result.put("i_1day_rov",d);
        }
        d = ExtractorUtils.division("i_1day_share_cnt", "i_1day_return_cnt", maps);
        if (!ExtractorUtils.isDoubleEqualToZero(d)){
            result.put("i_1day_ros",d);
        }

        d = ExtractorUtils.division("i_3day_exp_cnt", "i_3day_click_cnt", maps);
        if (!ExtractorUtils.isDoubleEqualToZero(d)){
            result.put("i_3day_ctr",d);
        }
        d = ExtractorUtils.division("i_3day_exp_cnt", "i_3day_share_cnt", maps);
        if (!ExtractorUtils.isDoubleEqualToZero(d)){
            result.put("i_3day_str",d);
        }
        d = ExtractorUtils.division("i_3day_exp_cnt", "i_3day_return_cnt", maps);
        if (!ExtractorUtils.isDoubleEqualToZero(d)){
            result.put("i_3day_rov",d);
        }
        d = ExtractorUtils.division("i_3day_share_cnt", "i_3day_return_cnt", maps);
        if (!ExtractorUtils.isDoubleEqualToZero(d)){
            result.put("i_3day_ros",d);
        }

        d = ExtractorUtils.division("i_7day_exp_cnt", "i_7day_click_cnt", maps);
        if (!ExtractorUtils.isDoubleEqualToZero(d)){
            result.put("i_7day_ctr",d);
        }
        d = ExtractorUtils.division("i_7day_exp_cnt", "i_7day_share_cnt", maps);
        if (!ExtractorUtils.isDoubleEqualToZero(d)){
            result.put("i_7day_str",d);
        }
        d = ExtractorUtils.division("i_7day_exp_cnt", "i_7day_return_cnt", maps);
        if (!ExtractorUtils.isDoubleEqualToZero(d)){
            result.put("i_7day_rov",d);
        }
        d = ExtractorUtils.division("i_7day_share_cnt", "i_7day_return_cnt", maps);
        if (!ExtractorUtils.isDoubleEqualToZero(d)){
            result.put("i_7day_ros",d);
        }

        d = ExtractorUtils.division("i_3month_exp_cnt", "i_3month_click_cnt", maps);
        if (!ExtractorUtils.isDoubleEqualToZero(d)){
            result.put("i_3month_ctr",d);
        }
        d = ExtractorUtils.division("i_3month_exp_cnt", "i_3month_share_cnt", maps);
        if (!ExtractorUtils.isDoubleEqualToZero(d)){
            result.put("i_3month_str",d);
        }
        d = ExtractorUtils.division("i_3month_exp_cnt", "i_3month_return_cnt", maps);
        if (!ExtractorUtils.isDoubleEqualToZero(d)){
            result.put("i_3month_rov",d);
        }
        d = ExtractorUtils.division("i_3month_share_cnt", "i_3month_return_cnt", maps);
        if (!ExtractorUtils.isDoubleEqualToZero(d)){
            result.put("i_3month_ros",d);
        }


        return rateFeatureChange(result);
    }

    public static Map<String, String> getItemRealtimeTrend(Map<String, Map<String, Double>> maps, String date, String hour){
        Map<String, Double> result1 = new HashMap<>();
        Map<String, Double> result2 = new HashMap<>();
        if (date.isEmpty() || hour.isEmpty()){
            return rateFeatureChange(result1);
        }
        int N = 6;

        List<String> hourStrs = ExtractorUtils.generateHourStrings(date + hour, N);

        String key;

        key = "share_uv_list_1day";
        if (maps.containsKey(key)){
            Map<String, Double> fList = maps.get(key);
            List<Double> arrs = hourStrs.stream().map(r -> fList.getOrDefault(r, 0.0D)).collect(Collectors.toList());
            Collections.reverse(arrs);
            result1.put(key+"_"+N+"_avg", ExtractorUtils.calculateAverage(arrs));
            result1.put(key+"_"+N+"_var", ExtractorUtils.calculateVariance(arrs));

            List<Double> arrsDiff = ExtractorUtils.calculateDifferences(arrs);
            result2.put(key+"_diff_"+N+"_avg", ExtractorUtils.calculateAverage(arrsDiff));
            result2.put(key+"_diff_"+N+"_var", ExtractorUtils.calculateVariance(arrsDiff));
        }

        key = "return_uv_list_1day";
        if (maps.containsKey(key)){
            Map<String, Double> fList = maps.get(key);
            List<Double> arrs = hourStrs.stream().map(r -> fList.getOrDefault(r, 0.0D)).collect(Collectors.toList());
            Collections.reverse(arrs);
            result1.put(key+"_"+N+"_avg", ExtractorUtils.calculateAverage(arrs));
            result1.put(key+"_"+N+"_var", ExtractorUtils.calculateVariance(arrs));

            List<Double> arrsDiff = ExtractorUtils.calculateDifferences(arrs);
            result2.put(key+"_diff_"+N+"_avg", ExtractorUtils.calculateAverage(arrsDiff));
            result2.put(key+"_diff_"+N+"_var", ExtractorUtils.calculateVariance(arrsDiff));
        }

        key = "share_uv_list_1h";
        if (maps.containsKey(key)){
            Map<String, Double> fList = maps.get(key);
            List<Double> arrs = hourStrs.stream().map(r -> fList.getOrDefault(r, 0.0D)).collect(Collectors.toList());
            Collections.reverse(arrs);
            result1.put(key+"_"+N+"_avg", ExtractorUtils.calculateAverage(arrs));
            result1.put(key+"_"+N+"_var", ExtractorUtils.calculateVariance(arrs));

            List<Double> arrsDiff = ExtractorUtils.calculateDifferences(arrs);
            result2.put(key+"_diff_"+N+"_avg", ExtractorUtils.calculateAverage(arrsDiff));
            result2.put(key+"_diff_"+N+"_var", ExtractorUtils.calculateVariance(arrsDiff));
        }

        key = "return_uv_list_1h";
        if (maps.containsKey(key)){
            Map<String, Double> fList = maps.get(key);
            List<Double> arrs = hourStrs.stream().map(r -> fList.getOrDefault(r, 0.0D)).collect(Collectors.toList());
            Collections.reverse(arrs);
            result1.put(key+"_"+N+"_avg", ExtractorUtils.calculateAverage(arrs));
            result1.put(key+"_"+N+"_var", ExtractorUtils.calculateVariance(arrs));

            List<Double> arrsDiff = ExtractorUtils.calculateDifferences(arrs);
            result2.put(key+"_diff_"+N+"_avg", ExtractorUtils.calculateAverage(arrsDiff));
            result2.put(key+"_diff_"+N+"_var", ExtractorUtils.calculateVariance(arrsDiff));
        }
        Map<String, String> r1 = cntFeatureChange4Double(result1);
        Map<String, String> r2 = rateFeatureChange(result2);
        r1.putAll(r2);

        return r1;
    }


    public static Map<String, String> rateFeatureChange(Map<String, Double> maps){
        Map<String, String> result = new HashMap<>();
        for (Map.Entry<String, Double> entry : maps.entrySet()){
            int value = ExtractorUtils.ceilLogRate(entry.getValue());
            result.put(entry.getKey(), String.valueOf(value));
        }
        return result;
    }
    public static Map<String, String> cntFeatureChange4Double(Map<String, Double> maps){
        Map<String, String> result = new HashMap<>();
        for (Map.Entry<String, Double> entry : maps.entrySet()){
            int value = ExtractorUtils.bucketCnt(Double.valueOf(entry.getValue()));
            result.put(entry.getKey(), String.valueOf(value));
        }
        return result;
    }

    public static Map<String, String> cntFeatureChange(Map<String, String> maps,
                                                       Set<String> names){
        Map<String, String> result = new HashMap<>();
        for (Map.Entry<String, String> entry : maps.entrySet()){
            if (!names.contains(entry.getKey())){
                continue;
            }
            int value = ExtractorUtils.bucketCnt(Double.valueOf(entry.getValue()));
            result.put(entry.getKey(), String.valueOf(value));
        }
        return result;
    }

    public static Map<String, String> getItemRealtimeCnt(Map<String, Map<String, Double>> maps,
                                                         Set<String> names,
                                                         String date, String hour){
        Map<String, String> result = new HashMap<>();
        if (date.isEmpty() || hour.isEmpty()){
            return result;
        }
        String dateHour = ExtractorUtils.subtractHours(date + hour, 0);
        for (Map.Entry<String, Map<String, Double>> entry : maps.entrySet()){
            if (!names.contains(entry.getKey())){
                continue;
            }
            Double num = entry.getValue().getOrDefault(dateHour, 0.0);
            if (!ExtractorUtils.isDoubleEqualToZero(num)){
                result.put(entry.getKey(), String.valueOf(ExtractorUtils.bucketCnt(num)));
            }
        }
        return result;
    }

    public static Map<String, String> getItemRealtimeRate(Map<String, Map<String, Double>> maps,
                                                         String date, String hour){
        Map<String, Double> result = new HashMap<>();
        if (date.isEmpty() || hour.isEmpty()){
            return rateFeatureChange(result);
        }
        String dateHour = ExtractorUtils.subtractHours(date + hour, 0);

        double d, d1, d2;
        String k1, k2;

        k1 = "view_pv_list_1day";
        k2 = "play_pv_list_1day";
        if (maps.containsKey(k1) && maps.containsKey(k2)){
            d1 = maps.get(k1).getOrDefault(dateHour, 0.0);
            d2 = maps.get(k2).getOrDefault(dateHour, 0.0);
            d = ExtractorUtils.divisionDouble(d1, d2);
            if (!ExtractorUtils.isDoubleEqualToZero(d)){
                result.put("i_1day_ctr_rt", d);
            }
        }

        k1 = "view_pv_list_1day";
        k2 = "share_pv_list_1day";
        if (maps.containsKey(k1) && maps.containsKey(k2)){
            d1 = maps.get(k1).getOrDefault(dateHour, 0.0);
            d2 = maps.get(k2).getOrDefault(dateHour, 0.0);
            d = ExtractorUtils.divisionDouble(d1, d2);
            if (!ExtractorUtils.isDoubleEqualToZero(d)){
                result.put("i_1day_str_rt", d);
            }
        }

        k1 = "share_pv_list_1day";
        k2 = "return_uv_list_1day";
        if (maps.containsKey(k1) && maps.containsKey(k2)){
            d1 = maps.get(k1).getOrDefault(dateHour, 0.0);
            d2 = maps.get(k2).getOrDefault(dateHour, 0.0);
            d = ExtractorUtils.divisionDouble(d1, d2);
            if (!ExtractorUtils.isDoubleEqualToZero(d)){
                result.put("i_1day_ros_rt", d);
            }
        }

        k1 = "view_pv_list_1day";
        k2 = "return_uv_list_1day";
        if (maps.containsKey(k1) && maps.containsKey(k2)){
            d1 = maps.get(k1).getOrDefault(dateHour, 0.0);
            d2 = maps.get(k2).getOrDefault(dateHour, 0.0);
            d = ExtractorUtils.divisionDouble(d1, d2);
            if (!ExtractorUtils.isDoubleEqualToZero(d)){
                result.put("i_1day_rov_rt", d);
            }
        }

        //---
        k1 = "view_pv_list_1h";
        k2 = "play_pv_list_1h";
        if (maps.containsKey(k1) && maps.containsKey(k2)){
            d1 = maps.get(k1).getOrDefault(dateHour, 0.0);
            d2 = maps.get(k2).getOrDefault(dateHour, 0.0);
            d = ExtractorUtils.divisionDouble(d1, d2);
            if (!ExtractorUtils.isDoubleEqualToZero(d)){
                result.put("i_1h_ctr_rt", d);
            }
        }

        k1 = "view_pv_list_1h";
        k2 = "share_pv_list_1h";
        if (maps.containsKey(k1) && maps.containsKey(k2)){
            d1 = maps.get(k1).getOrDefault(dateHour, 0.0);
            d2 = maps.get(k2).getOrDefault(dateHour, 0.0);
            d = ExtractorUtils.divisionDouble(d1, d2);
            if (!ExtractorUtils.isDoubleEqualToZero(d)){
                result.put("i_1h_str_rt", d);
            }
        }

        k1 = "share_pv_list_1day";
        k2 = "return_uv_list_1h";
        if (maps.containsKey(k1) && maps.containsKey(k2)){
            d1 = maps.get(k1).getOrDefault(dateHour, 0.0);
            d2 = maps.get(k2).getOrDefault(dateHour, 0.0);
            d = ExtractorUtils.divisionDouble(d1, d2);
            if (!ExtractorUtils.isDoubleEqualToZero(d)){
                result.put("i_1h_ros_rt", d);
            }
        }

        k1 = "view_pv_list_1h";
        k2 = "return_uv_list_1h";
        if (maps.containsKey(k1) && maps.containsKey(k2)){
            d1 = maps.get(k1).getOrDefault(dateHour, 0.0);
            d2 = maps.get(k2).getOrDefault(dateHour, 0.0);
            d = ExtractorUtils.divisionDouble(d1, d2);
            if (!ExtractorUtils.isDoubleEqualToZero(d)){
                result.put("i_1h_rov_rt", d);
            }
        }


        return rateFeatureChange(result);
    }

    public static void main(String[] args) {
        String s1 = "share_uv_list_1day";
        String s2 = "2024011300:2,2024011301:2,2024011304:2,2024011309:3,2024011311:3,2024011314:4,2024011315:4,2024011321:1,2024011323:1,2024011400:1,2024011401:1,2024011404:1,2024011406:1,2024011407:1,2024011408:1,2024011410:1,2024011423:1,2024011302:2,2024011305:2,2024011312:4,2024011313:4,2024011317:4,2024011318:4,2024011319:3,2024011320:1,2024011403:1,2024011409:1,2024011411:1,2024011419:1,2024011420:1,2024011422:1,2024011303:2,2024011306:2,2024011307:2,2024011308:2,2024011310:3,2024011316:4,2024011322:1,2024011402:1,2024011405:1,2024011421:1";
        Map<String, Double> m1 = new HashMap<>();
        Map<String, Map<String, Double>> maps = new HashMap<>();
        for (String s : s2.split(",")){
            String s3 = s.split(":")[0];
            String s4 = s.split(":")[1];
            m1.put(s3, Double.valueOf(s4));
        }
        maps.put(s1, m1);

        String date = "20240114";
        String hour = "20";
        System.out.println(getItemRealtimeTrend(maps, date, hour));
    }
}
