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

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
import com.tzld.piaoquan.recommend.feature.domain.video.feature.BytesGroup;
import com.tzld.piaoquan.recommend.feature.domain.video.feature.BytesUtils;
import com.tzld.piaoquan.recommend.feature.model.sample.BaseFeature;
import com.tzld.piaoquan.recommend.feature.model.sample.FeatureGroup;
import com.tzld.piaoquan.recommend.feature.model.sample.LRSamples;
import java.util.Map;
import java.util.*;
import com.tzld.piaoquan.recommend.feature.model.sample.GroupedFeature;

public class OfflineVlogShareLRFeatureExtractor {

    public ListMultimap<FeatureGroup, BaseFeature> featureMap = ArrayListMultimap.create();

    final private BytesUtils utils;
    final private int groupCount = OfflineVlogFeatureGroup.values().length;
    public OfflineVlogShareLRFeatureExtractor() {
        BytesGroup[] groups = new BytesGroup[OfflineVlogFeatureGroup.values().length];
        OfflineVlogFeatureGroup[] var2 = OfflineVlogFeatureGroup.values();
        int var3 = var2.length;

        for(int var4 = 0; var4 < var3; ++var4) {
            OfflineVlogFeatureGroup g = var2[var4];
            groups[g.ordinal()] = new BytesGroup(g.ordinal(), g.getGroupName(), g.getGroupNameBytes());
        }
        this.utils = new BytesUtils(groups);
    }
    public void makeFeature(Map<String, Object> maps){
        for (Map.Entry<String, Object> entry : maps.entrySet()){
            OfflineVlogFeatureGroup ovf = OfflineVlogFeatureGroup.valueOf(entry.getKey());
            Object value = entry.getValue();
            if (value instanceof String){
                this.makeFea(ovf, ((String)value).getBytes());
            }else if (value instanceof Double){
                this.makeFea(ovf, String.valueOf((Double)value).getBytes());
            }else if (value instanceof Integer){
                //todo
            }else{
                //todo
                this.makeFea(ovf, ((String)value).getBytes());
            }
        }
    }

    private FeatureGroup makeGroup(OfflineVlogFeatureGroup group) {
        FeatureGroup.Builder g = FeatureGroup.newBuilder();
        g.setType("1");
        g.setName(group.getGroupName());
        g.setId(group.ordinal());
        return g.build();
    }
    void makeFea(OfflineVlogFeatureGroup group, byte[] value) {
        FeatureGroup featureGroup = this.makeGroup(group);
        BaseFeature feature = this.utils.makeFea(group.ordinal(), value);
        this.featureMap.put(featureGroup, feature);
    }

    public synchronized LRSamples single(Map<String, byte[]> userBytesFeature,
                                         Map<String, byte[]> videoBytesFeature,
                                         Map<String, byte[]> sceneFeatureMapByte) {
        featureMap.clear();
        // extract features todo zhangbo
        for (Map.Entry<String, byte[]> entry : userBytesFeature.entrySet()){
            makeFea(OfflineVlogFeatureGroup.valueOf(entry.getKey()), entry.getValue());
        }
        for (Map.Entry<String, byte[]> entry : videoBytesFeature.entrySet()){
            makeFea(OfflineVlogFeatureGroup.valueOf(entry.getKey()), entry.getValue());
        }
        for (Map.Entry<String, byte[]> entry : sceneFeatureMapByte.entrySet()){
            makeFea(OfflineVlogFeatureGroup.valueOf(entry.getKey()), entry.getValue());
        }

        LRSamples.Builder lr = LRSamples.newBuilder();
        lr.setGroupNum(groupCount);
        List<FeatureGroup> keys = new ArrayList<>(featureMap.keySet());
        int count = 0;
        for(FeatureGroup group : keys) {
            List<BaseFeature> fea = featureMap.get(group);
            GroupedFeature.Builder gf = GroupedFeature.newBuilder();
            gf.setGroup(group);
            gf.setCount(fea.size());
            gf.addAllFeatures(fea);
            count += fea.size();
            lr.addFeatures(gf);
        }
        lr.setCount(count);
        return lr.build();
    }
}
