package com.tzld.piaoquan.recommend.server.service.score.model;

import com.google.common.reflect.TypeToken;
import com.tzld.piaoquan.recommend.server.util.JSONUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

public class VovH24WeightModel extends Model {
    private static final Logger LOGGER = LoggerFactory.getLogger(VovH24WeightModel.class);

    // <小时, <Key, weight>>
    private Map<String, Map<String, Double>> model = new HashMap<>();

    @Override
    public int getModelSize() {
        return model.size();
    }

    @Override
    public boolean loadFromStream(InputStreamReader in) throws Exception {
        Map<String, Map<String, Double>> initModel = new HashMap<>();
        try (BufferedReader reader = new BufferedReader(in)) {
            StringBuilder buffer = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                buffer.append(line).append("\n");
            }
            initModel = JSONUtils.fromJson(buffer.toString(), new TypeToken<Map<String, Map<String, Double>>>() {
            }, initModel);
            this.model = initModel;
        } catch (Exception e) {
            LOGGER.error("read vovh24 weight file error: ", e);
            return false;
        }
        return true;
    }

    public Map<String, Double> getWeight(String key) {
        return model.get(key);
    }
}
