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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ConvertV2 {
    public static JSONObject getFeature(Map<String, String> record, List<Map<String, String>> videoSeq, int scale) {
        Map<String, Double> featMap = new HashMap<>();

        // origin info
        String ts = record.get("ts");
        long currentMs = Long.parseLong(ts) * 1000;
        String vid = record.get("vid");
        Map<String, String> headInfo = ConvertUtils.getRecordCol(record, "v2_feature");
        Map<String, String> rankInfo = ConvertUtils.getRecordCol(record, "v1_feature");
        Map<String, Map<String, String>> userOriginInfo = getUserOriginInfo(record);
        Map<String, Map<String, Map<String, String>>> videoOriginInfo = getVideoOriginInfo(record);

        // parse info
        Map<String, String> c9Map = userOriginInfo.get("alg_recsys_feature_user_share_return_stat");
        UserShareReturnProfile userProfile = JSON.parseObject(ConvertUtils.toJson(c9Map), UserShareReturnProfile.class);
        Map<String, Map<String, String>> historyVideoMap = ConvertUtils.list2Map(videoSeq);

        Map<String, Map<String, String[]>> c7Map = FeatureTransformV2.parseUCFScore(userOriginInfo.get("alg_mid_feature_sharecf"));
        Map<String, Map<String, String[]>> c8Map = FeatureTransformV2.parseUCFScore(userOriginInfo.get("alg_mid_feature_returncf"));

        // context feature
        FeatureTransformV2.getContextFeature(currentMs, featMap);

        // head video feature
        FeatureTransformV2.getVideoBaseFeature("h", currentMs, headInfo, featMap);

        // user feature
        FeatureTransformV2.getUserFeature(userOriginInfo, featMap);
        FeatureTransformV2.getUserProfileFeature(userProfile, record, featMap);

        // user & video feature
        FeatureTransformV2.getUserTagsCrossVideoFeature("c5", rankInfo, userOriginInfo.get("alg_mid_feature_return_tags"), featMap);
        FeatureTransformV2.getUserTagsCrossVideoFeature("c6", rankInfo, userOriginInfo.get("alg_mid_feature_share_tags"), featMap);
        FeatureTransformV2.getUserCFFeature("c7", vid, c7Map, featMap);
        FeatureTransformV2.getUserCFFeature("c8", vid, c8Map, featMap);

        // rank video feature
        FeatureTransformV2.getVideoBaseFeature("r", currentMs, rankInfo, featMap);
        FeatureTransformV2.getVideoFeature(vid, videoOriginInfo, featMap);

        // head&rank cross feature
        FeatureTransformV2.getHeadRankVideoCrossFeature(headInfo, rankInfo, featMap);

        // user profile & rank cross
        FeatureTransformV2.getProfileVideoCrossFeature(currentMs, userProfile, rankInfo, historyVideoMap, featMap);

        return ConvertUtils.filterAndTruncate(featMap, scale);
    }

    private static Map<String, Map<String, String>> getUserOriginInfo(Map<String, String> record) {
        Map<String, Map<String, String>> map = new HashMap<>();
        map.put("mid_global_feature_20250212", ConvertUtils.getRecordCol(record, "c1_feature"));
        map.put("mid_u2u_friend_index_feature_20250212", ConvertUtils.getRecordCol(record, "c4_feature"));
        map.put("alg_mid_feature_return_tags", ConvertUtils.getRecordCol(record, "c5_feature"));
        map.put("alg_mid_feature_share_tags", ConvertUtils.getRecordCol(record, "c6_feature"));
        map.put("alg_mid_feature_sharecf", ConvertUtils.getRecordCol(record, "c7_feature"));
        map.put("alg_mid_feature_returncf", ConvertUtils.getRecordCol(record, "c8_feature"));
        map.put("alg_recsys_feature_user_share_return_stat", ConvertUtils.getRecordCol(record, "c9_feature"));
        return map;
    }

    private static Map<String, Map<String, Map<String, String>>> getVideoOriginInfo(Map<String, String> record) {
        Map<String, Map<String, String>> map = new HashMap<>();
        map.put("alg_recsys_feature_video_clean_stat", ConvertUtils.getRecordCol(record, "b0_feature"));
        map.put("alg_vid_global_feature_20250212", ConvertUtils.getRecordCol(record, "b1_feature"));
        map.put("alg_vid_recommend_exp_feature_20250212", ConvertUtils.getRecordCol(record, "b2_feature"));
        map.put("alg_vid_recommend_flowpool_exp_feature_20250212", ConvertUtils.getRecordCol(record, "b3_feature"));
        map.put("alg_vid_apptype_recommend_exp_feature_20250212", ConvertUtils.getRecordCol(record, "b4_feature"));
        map.put("alg_vid_province_recommend_exp_feature_20250212", ConvertUtils.getRecordCol(record, "b5_feature"));
        map.put("alg_vid_brand_recommend_exp_feature_20250212", ConvertUtils.getRecordCol(record, "b6_feature"));
        map.put("alg_vid_hotsencetype_recommend_exp_feature_20250212", ConvertUtils.getRecordCol(record, "b7_feature"));
        map.put("alg_merge_cate1_recommend_exp_feature_20250212", ConvertUtils.getRecordCol(record, "b8_feature"));
        map.put("alg_merge_cate2_recommend_exp_feature_20250212", ConvertUtils.getRecordCol(record, "b9_feature"));
        map.put("alg_channel_recommend_exp_feature_20250212", ConvertUtils.getRecordCol(record, "b10_feature"));
        map.put("alg_festive_recommend_exp_feature_20250212", ConvertUtils.getRecordCol(record, "b11_feature"));
        //map.put("alg_vid_long_period_recommend_exp_feature_20250212", ConvertUtils.getRecordCol(record, "b12_feature"));
        map.put("alg_video_unionid_recommend_exp_feature_20250212", ConvertUtils.getRecordCol(record, "b13_feature"));
        map.put("scene_type_vid_cf_feature_20250212", ConvertUtils.getRecordCol(record, "d1_feature"));
        map.put("vid_click_cf_feature_20250212", ConvertUtils.getRecordCol(record, "d2_feature"));
        map.put("alg_recsys_feature_cf_i2i_v2", ConvertUtils.getRecordCol(record, "d3_feature"));
        // 特征
        map.put("mid_merge_cate1_feature_20250212", ConvertUtils.getRecordCol(record, "c2_feature"));
        map.put("mid_merge_cate2_feature_20250212", ConvertUtils.getRecordCol(record, "c3_feature"));

        Map<String, Map<String, Map<String, String>>> allMap = new HashMap<>();
        String vid = record.get("vid");
        allMap.put(vid, map);
        return allMap;
    }
}
