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


import com.tzld.piaoquan.ad.engine.commons.score.BaseLRModelScorer;
import com.tzld.piaoquan.ad.engine.commons.score.ScoreParam;
import com.tzld.piaoquan.ad.engine.commons.score.ScorerConfigInfo;
import com.tzld.piaoquan.recommend.feature.domain.ad.base.AdRankItem;
import com.tzld.piaoquan.recommend.feature.domain.ad.base.UserAdFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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


//@Service
public class VlogMergeEcpmScorer extends BaseLRModelScorer {

    private final static Logger LOGGER = LoggerFactory.getLogger(VlogMergeEcpmScorer.class);
    private static final ExecutorService executorService = Executors.newFixedThreadPool(8);
    private static final int LOCAL_TIME_OUT = 150;
    public VlogMergeEcpmScorer(ScorerConfigInfo configInfo) {
        super(configInfo);
    }


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


        long startTime = System.currentTimeMillis();
        List<AdRankItem> result = mergeEcpm(rankItems);
        LOGGER.debug("ctr ranker time java items size={}, time={} ", result != null ? result.size() : 0,
                System.currentTimeMillis() - startTime);

        return result;
    }




    public List<AdRankItem> mergeEcpm(List<AdRankItem> items) {
        CountDownLatch countDownLatch = new CountDownLatch(items.size());
        for (AdRankItem item : items) {
            executorService.execute(() -> {
                callMergeScore(item,countDownLatch);
            });
        }
        try {
            countDownLatch.await(200, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            LOGGER.error("mergetEcpm countDownLatch error ...", e);
        }
        Collections.sort(items);
        return items;
    }


//    public void callMergeScore(AdRankItem item,CountDownLatch countDownLatch){
//        try {
////            boolean isTfType=item.getItemFeature().getCreative_7day_cnt_features().getAdView()<5000;
////            double pctr = isTfType?item.getTf_ctr():item.getCtr();
////            double pcvr = isTfType?item.getTf_cvr():item.getCvr();
//            double cpa = item.getCpa();
//            double bid1 = item.getBid1();
//            double bid2 = item.getBid2();
//            double pctr = item.getCtr();
//            double pcvr = item.getCvr();
////            item.setScore_type( isTfType?1:0);
//            item.setScore_type( 0);
//            //todo
//            double ecpm;
////            if(isTfType){
////                ecpm = cpa * bid1 * bid2 * pcvr * pctr * 1000d;
////            }else {
//            cpa=cpa*item.getPidLambda();
//            if(cpa<0.5){
//                cpa=0.5;
//            }
//            ecpm = cpa* bid1 * bid2 * pcvr * pctr * 1000d;
////            }
////            ecpm = cpa * bid1 * bid2 * pcvr * pctr * 1000d;
//            item.setEcpm1(ecpm/1000d);
//            item.setScore(ecpm/1000d);
//        } catch (Exception e) {
//            LOGGER.error("merge Ecpm Score: {} error", "");
//            item.setScore(0.0);
//        }finally {
//            countDownLatch.countDown();
//        }
//    }


    public void callMergeScore(AdRankItem item,CountDownLatch countDownLatch){
        try {
//            boolean isTfType=item.getItemFeature().getCreative_7day_cnt_features().getAdView()<5000;
//            double pctr = isTfType?item.getTf_ctr():item.getCtr();
//            double pcvr = isTfType?item.getTf_cvr():item.getCvr();
            double cpa = item.getCpa();
            double bid1 = item.getBid1();
            double bid2 = item.getBid2();
            double pctr = item.getCtr();
            double pcvr = item.getCvr();
            LOGGER.info("VlogMergeEcmpScore.pcvr: {}", pcvr);
//            item.setScore_type( isTfType?1:0);
            item.setScore_type( 0);
            //todo
            double ecpm;
//            if(isTfType){
//                ecpm = cpa * bid1 * bid2 * pcvr * pctr * 1000d;
//            }else {
            cpa=cpa*item.getPidLambda();
            if(cpa<0.1){
                cpa=0.1;
            }
            ecpm = cpa* bid1 * bid2 * pcvr * pctr * 1000d;
//            ecpm = item.getPidLambda()* bid1 * bid2 * pcvr * pctr * 1000d;
//            ecpm2 = item.getPidLambda()* bid1 * bid2 * pcvr * pctr * 1000d;
//            }
//            ecpm = cpa * bid1 * bid2 * pcvr * pctr * 1000d;
            item.setEcpm1(ecpm/1000d);
            item.setScore(ecpm/1000d);
            item.setEcpm2(ecpm/1000d);
        } catch (Exception e) {
            LOGGER.error("merge Ecpm Score: {} error", "");
            item.setScore(0.0);
        }finally {
            countDownLatch.countDown();
        }
    }
}