package com.tzld.piaoquan.ad.engine.commons.score.model;

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 CreativeCalibrationModel extends Model {

    private static final Logger LOGGER = LoggerFactory.getLogger(CreativeCalibrationModel.class);

    private Map<Long, Double> creativeDiffRateMap = new HashMap<>();

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

    @Override
    public boolean loadFromStream(InputStreamReader in) throws Exception {
        Map<Long, Double> initMap = new HashMap<>();
        try (BufferedReader input = new BufferedReader(in)) {
            String line;
            while ((line = input.readLine()) != null) {
                String[] items = line.split("\t");
                if (items.length < 2) {
                    continue;
                }

                long cid = Long.parseLong(items[0]);
                double diffRate = Double.parseDouble(items[1]);

                initMap.put(cid, diffRate);

            }
            this.creativeDiffRateMap = initMap;
            LOGGER.info("[CreativeCalibrationModel] model load over and size {}", this.creativeDiffRateMap.size());
        } catch (Exception e) {
            LOGGER.info("[CreativeCalibrationModel] model load error ", e);
        } finally {
            in.close();

        }
        return true;
    }

    public double getDiffRate(long cid) {
        return this.creativeDiffRateMap.getOrDefault(cid, 0d);
    }
}
