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

import com.tzld.piaoquan.recommend.feature.domain.video.base.UserFeature;
import com.tzld.piaoquan.recommend.server.common.base.RankItem;
import com.tzld.piaoquan.recommend.server.service.score.model.XGBRegressionModel;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.concurrent.*;

public class NorXGBRegressionScorer extends XGBRegressionModelScorer {
    private static final int LOCAL_TIME_OUT = 150;
    private final static Logger LOGGER = LoggerFactory.getLogger(NorXGBRegressionScorer.class);
    private static final ExecutorService executorService = Executors.newFixedThreadPool(128);


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

    @Override
    public List<RankItem> scoring(final ScoreParam param,
                                  final UserFeature userFeature,
                                  final List<RankItem> rankItems) {
        throw new NoSuchMethodError();
    }

    @Override
    public List<RankItem> scoring(final Map<String, Float> sceneFeatureMap,
                                  final Map<String, Float> userFeatureMap,
                                  final List<RankItem> rankItems) {
        if (CollectionUtils.isEmpty(rankItems)) {
            return rankItems;
        }

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

        List<RankItem> result = rankByJava(
                sceneFeatureMap, userFeatureMap, rankItems
        );

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

        return result;
    }

    private List<RankItem> rankByJava(final Map<String, Float> sceneFeatureMap,
                                      final Map<String, Float> userFeatureMap,
                                      final List<RankItem> items) {
        long startTime = System.currentTimeMillis();
        XGBRegressionModel model = (XGBRegressionModel) this.getModel();

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

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

        Collections.sort(items);

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

    private void multipleCtrScore(final List<RankItem> items,
                                  final Map<String, Float> userFeatureMap,
                                  final Map<String, Float> sceneFeatureMap,
                                  final XGBRegressionModel model) {

        List<Callable<Object>> calls = new ArrayList<Callable<Object>>();
        for (int index = 0; index < items.size(); index++) {
            final int fIndex = index;
            calls.add(new Callable<Object>() {
                @Override
                public Object call() throws Exception {
                    try {
                        calcScore(model, items.get(fIndex), userFeatureMap, sceneFeatureMap);
                    } catch (Exception e) {
                        LOGGER.error("nor exception: [{}] [{}]", items.get(fIndex).videoId, 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++;
                    }
                    if (!future.isDone() && !future.isCancelled()) {
                        future.cancel(true);
                    }
                } catch (InterruptedException e) {
                    LOGGER.error("InterruptedException {},{}", ExceptionUtils.getFullStackTrace(e));
                } catch (ExecutionException e) {
                    LOGGER.error("ExecutionException {},{}", sceneFeatureMap.size(),
                            ExceptionUtils.getFullStackTrace(e));
                }
            }
        }
    }

    public double calcScore(final XGBRegressionModel model,
                            final RankItem item,
                            final Map<String, Float> userFeatureMap,
                            final Map<String, Float> sceneFeatureMap) {
        Map<String, Float> featureMap = new HashMap<>();
        if (MapUtils.isNotEmpty(item.getNorFeatureMap())) {
            featureMap.putAll(item.getNorFeatureMap());
        }
        if (MapUtils.isNotEmpty(userFeatureMap)) {
            featureMap.putAll(userFeatureMap);
        }
        if (MapUtils.isNotEmpty(sceneFeatureMap)) {
            featureMap.putAll(sceneFeatureMap);
        }

        double pro = 0.0;
        if (MapUtils.isNotEmpty(featureMap)) {
            try {
                pro = model.score(featureMap);
                // LOGGER.info("fea : {}, score:{}", JSONUtils.toJson(featureMap), pro);
            } catch (Exception e) {
                LOGGER.error("score error for doc={} exception={}", item.getVideoId(), ExceptionUtils.getFullStackTrace(e));
            }
        }
        if (pro < 1E-8) {
            pro = 0;
        } else if (pro > 80) {
            pro = 80;
        }
        item.getScoresMap().put("NorXGBScore", pro);
        return pro;
    }
}
