package com.tzld.piaoquan.recommend.server.framework.model;

import com.tzld.piaoquan.recommend.server.common.base.RankItem;
import com.tzld.piaoquan.recommend.server.common.base.VideoActionFeature;
import org.apache.commons.math3.distribution.BetaDistribution;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;


public class ThompsonSamplingModel extends Model {
    protected static final int MODEL_FIRST_LOAD_COUNT = 1 << 25;  // 32M
    private static final Logger LOGGER = LoggerFactory.getLogger(ThompsonSamplingModel.class);

    // key = videoid, value = < push, exp, play, realplay, share, retures >
    private Map<Long, VideoActionFeature> thompsonSamplingModel;

    private static final int alpha = 20;
    private static final int beta_returns = 100;

    public ThompsonSamplingModel() {
        //配置不同环境的hdfs conf
        this.thompsonSamplingModel = new HashMap<>();
    }

    public Map<Long, VideoActionFeature> getThompsonSamplingModel() {
        return this.thompsonSamplingModel;
    }




    @Override
    public boolean loadFromStream(InputStreamReader in) throws IOException {
        Map<Long, VideoActionFeature> initModel = new HashMap<>();
        BufferedReader input = new BufferedReader(in);
        String line = null;
        int cnt = 0;
        while ((line = input.readLine()) != null) {
            String[] items = line.split("\t");
            if (items.length < 3) {
                continue;
            }
            Long videoId = new BigInteger(items[0].trim()).longValue();
            VideoActionFeature videoFeature = new VideoActionFeature();
            videoFeature.setView(Double.valueOf(items[1].trim()));
            videoFeature.setPlay(Double.valueOf(items[2].trim()));
            videoFeature.setShare(Double.valueOf(items[3].trim()));
            videoFeature.setReturns(Double.valueOf(items[5].trim()));
            initModel.put(videoId, videoFeature);
        }

        this.thompsonSamplingModel = initModel;
        input.close();
        in.close();
        return true;
    }

    @Override
    public int getModelSize() {
        if (this.thompsonSamplingModel == null)
            return 0;
        int sum = this.thompsonSamplingModel.size();
        return sum;
    }

    public double score(RankItem rankItem) {
        double score = 0.0f;
        VideoActionFeature videoActionFeature = this.thompsonSamplingModel.getOrDefault(rankItem.getVideoId(), new VideoActionFeature());

        int alpha = (int) videoActionFeature.getReturns() + ThompsonSamplingModel.alpha;
        int beta = beta_returns + (int) videoActionFeature.getView();
        score = this.betaSampler(alpha, beta);
        return score;
    }

    public double betaSampler(double alpha, double beta) {
        BetaDistribution betaSample = new BetaDistribution(alpha, beta);
        return betaSample.sample();
    }


}
