package com.tzld.piaoquan.recommend.server.framework.recaller.provider;

import com.alibaba.fastjson.JSONObject;
import com.google.common.reflect.TypeToken;
import com.tzld.piaoquan.recommend.server.util.JSONUtils;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Data
public class Index {
    private String indexName = null;
    private List<IndexEntry> indexEntryList = new ArrayList<>();


    // TODO
    // redis中一路召回队列key 的结果 转化到Index中
    public Index(String indexName, String indexValue) {
        if (StringUtils.isBlank(indexName) || StringUtils.isBlank(indexValue)) {
            return;
        }
        this.indexName = indexName;
        JSONObject jsonObject = JSONObject.parseObject(indexValue);
        String valueList = jsonObject.getString("value_list");
        valueList = "[" + valueList + "]";
        List<List<String>> videoScores = JSONUtils.fromJson(valueList, new TypeToken<List<List<String>>>() {
        }, Collections.emptyList());

        videoScores.stream().forEach(itemAndScore -> {
                    IndexEntry indexEntry = new IndexEntry();
                    if (itemAndScore.size() >= 2) {
                        indexEntry.setItemId(itemAndScore.get(0));
                        indexEntry.setScore(Double.valueOf(itemAndScore.get(1)));
                        this.indexEntryList.add(indexEntry);
                    }
                }
        );

    }

}
