package com.tzld.piaoquan.ad.engine.service.score.deprecated;


import com.tzld.piaoquan.ad.engine.commons.score.BaseThompsonSamplingScorer;
import com.tzld.piaoquan.ad.engine.commons.score.ScoreParam;
import com.tzld.piaoquan.ad.engine.commons.score.ScorerConfigInfo;
import com.tzld.piaoquan.ad.engine.commons.score.model.ThompsonSamplingModel;
import com.tzld.piaoquan.recommend.feature.domain.ad.base.*;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;


//@Service
public class VlogAdThompsonScorer extends BaseThompsonSamplingScorer {

    private static final int LOCAL_TIME_OUT = 150;
    private final static Logger LOGGER = LoggerFactory.getLogger(VlogAdThompsonScorer.class);
    private static final ExecutorService executorService = Executors.newFixedThreadPool(8);

    public VlogAdThompsonScorer(ScorerConfigInfo configInfo) {
        super(configInfo);
    }

    @Override
    public List<AdRankItem> scoring(final ScoreParam param,
                                    final UserAdFeature userFeature,
                                    final List<AdRankItem> rankItems) {

        if (userFeature == null || CollectionUtils.isEmpty(rankItems)) {
            return rankItems;
        }

        long startTime = System.currentTimeMillis();
        ThompsonSamplingModel model = (ThompsonSamplingModel) this.getModel();
        LOGGER.debug("model size: [{}]", model.getModelSize());

        List<AdRankItem> result = rankItems;
        result = rankByJava(rankItems, param.getRequestContext(), userFeature);

        LOGGER.debug("thompson sampling ctr ranker time java items size={}, time={} ", result != null ? result.size() : 0,
                System.currentTimeMillis() - startTime);

        return result;
    }

    private List<AdRankItem> rankByJava(final List<AdRankItem> items,
                                        final AdRequestContext requestContext,
                                        final UserAdFeature user) {
        long startTime = System.currentTimeMillis();
        ThompsonSamplingModel model = (ThompsonSamplingModel) this.getModel();
        LOGGER.debug("model size: [{}]", model.getModelSize());

        // 所有都参与打分，按照ctr排序
        multipleCtrScore(items, model);

        // debug log
        if (LOGGER.isDebugEnabled()) {
            for (int i = 0; i < items.size(); i++) {
                LOGGER.debug("after enter feeds model predict ctr score [{}] [{}]", items.get(i), items.get(i).getScore());
            }
        }

        LOGGER.debug("thompson ranker java execute time: [{}]", System.currentTimeMillis() - startTime);
        LOGGER.debug("[thompson ranker time java] items size={}, cost={} ", items != null ? items.size() : 0,
                System.currentTimeMillis() - startTime);
        return items;
    }


    /**
     * 计算 predict ecpm
     */
    public double calcScore(final ThompsonSamplingModel model,
                            final AdRankItem item) {


        double pctr = 0.0;
        double pcvr = 0.0;
        double ecpm = 0.0;

        try {
            pctr = model.score(item, "ctr");
            pcvr = model.score(item, "cvr");
            ecpm = item.getCpa() * item.getBid1() * item.getBid2() * pctr * pcvr;
        } catch (Exception e) {
            LOGGER.error("score error for doc={} exception={}", new Object[]{
                    item.getAdId(), ExceptionUtils.getFullStackTrace(e)});
        }
        item.setTf_ctr(pctr);
        item.setTf_cvr(pcvr);
        item.setEcpm1(ecpm);
        item.setScore(ecpm);
        return ecpm;
    }


    /**
     * 并行打分 ecpm
     * @param items
     * @param model
     */
    private void multipleCtrScore(final List<AdRankItem> items,
                                  final ThompsonSamplingModel model) {

        List<Callable<Object>> calls = new ArrayList<Callable<Object>>();
        for (int index = 0; index < items.size(); index++) {
            final int fIndex = index;
            items.get(fIndex).setScore(0.0);   //设置为原始值为0
            calls.add(new Callable<Object>() {
                @Override
                public Object call() throws Exception {
                    try {
                        calcScore(model, items.get(fIndex));
                    } catch (Exception e) {
                        LOGGER.error("thompson exception: [{}] [{}]", items.get(fIndex).adId, ExceptionUtils.getFullStackTrace(e));
                    }
                    return new Object();
                }
            });
        }

        List<Future<Object>> futures = null;
        try {
            futures = executorService.invokeAll(calls, LOCAL_TIME_OUT, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            LOGGER.error("execute invoke fail: {}", ExceptionUtils.getFullStackTrace(e));
        }

        //等待所有请求的结果返回, 超时也返回
        int cancel = 0;
        if (futures != null) {
            for (Future<Object> future : futures) {
                try {
                    if (!future.isDone() || future.isCancelled() || future.get() == null) {
                        cancel++;
                    }
                } catch (InterruptedException e) {
                    LOGGER.error("InterruptedException {},{}", ExceptionUtils.getFullStackTrace(e));
                } catch (ExecutionException e) {
                    LOGGER.error("ExecutionException {},{}", ExceptionUtils.getFullStackTrace(e));
                }
            }
        }
        LOGGER.debug("ecpm Score {}, Total: {}, Cancel: {}", new Object[]{ items.size(), cancel});
    }
}