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

import com.tzld.piaoquan.ad.engine.commons.dto.AdPlatformCreativeDTO;
import com.tzld.piaoquan.ad.engine.commons.param.RankRecommendRequestParam;
import com.tzld.piaoquan.ad.engine.commons.score.ScoreParam;
import com.tzld.piaoquan.ad.engine.commons.util.ObjUtil;
import com.tzld.piaoquan.ad.engine.service.feature.Feature;
import com.tzld.piaoquan.recommend.feature.domain.ad.base.AdRankItem;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Component
public class FeaturePrinterStrategy extends RankStrategyBasic {
    @Override
    public List<AdRankItem> adItemRank(RankRecommendRequestParam request, ScoreParam scoreParam) {
        Set<String> noApiAdVerIds = getNoApiAdVerIds();
        long ts = System.currentTimeMillis() / 1000;

        String brand = scoreParam.getRequestContext().getMachineinfoBrand();
        if (StringUtils.isNotEmpty(brand)) {
            scoreParam.getRequestContext().setMachineinfoBrand(brand + "-n");
        }

        Feature feature = this.getFeature(scoreParam, request);
        Map<String, String> reqFeature = this.getReqFeature(scoreParam, request);
        Map<String, String> sceneFeatureMap = this.handleSceneFeature(ts);

        AdPlatformCreativeDTO dto = request.getAdIdList().get(0);
        AdRankItem adRankItem = new AdRankItem();
        adRankItem.setAdId(dto.getCreativeId());
        adRankItem.setCreativeCode(dto.getCreativeCode());
        adRankItem.setAdVerId(dto.getAdVerId());
        adRankItem.setVideoId(request.getVideoId());
        adRankItem.setCpa(dto.getCpa());
        adRankItem.setId(dto.getAdId());
        adRankItem.setCampaignId(dto.getCampaignId());
        adRankItem.setCpm(ObjUtil.nullOrDefault(dto.getCpm(), 90).doubleValue());
        adRankItem.setSkuId(dto.getSkuId());
        if (noApiAdVerIds != null && noApiAdVerIds.contains(dto.getAdVerId())) {
            adRankItem.getExt().put("isApi", "0");
        } else {
            adRankItem.getExt().put("isApi", "1");
        }
        adRankItem.getExt().put("recallsources", dto.getRecallSources());
        fillAdRankItemExt(adRankItem, dto);
        putMetaFeature(adRankItem, feature, reqFeature, sceneFeatureMap, request);
        return Collections.singletonList(adRankItem);
    }
}
