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

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.param.RecommendRequestParam;
import com.tzld.piaoquan.ad.engine.commons.score.ScoreParam;
import com.tzld.piaoquan.ad.engine.commons.util.AbUtil;
import com.tzld.piaoquan.recommend.feature.domain.ad.base.AdRankItem;
import com.tzld.piaoquan.recommend.feature.domain.ad.base.AdRequestContext;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class RequestConvert {

    public static ScoreParam requestConvert(RecommendRequestParam request) {
        AdRequestContext context = new AdRequestContext();
        context.setApptype(request.getAppType().toString());
        context.setMachineinfoBrand(request.getMachineInfo().getBrand());
        context.setMachineinfoModel(request.getMachineInfo().getModel());
        context.setMachineinfoSdkversion(request.getMachineInfo().getSdkVersion());
        context.setMachineinfoWchatversion(request.getMachineInfo().getWeChatVersion());

        LocalDateTime date = LocalDateTime.now();
        String hourStr = date.format(DateTimeFormatter.ofPattern("HH"));
        context.setHour(hourStr);
        context.setDay(date.format(DateTimeFormatter.ofPattern("yyyyMMdd")));
        context.setWeek(String.valueOf(date.getDayOfWeek().getValue()));
        context.setRegion(request.getRegion().replace("省", ""));
        context.setCity(request.getCity().replace("市", ""));

        ScoreParam scoreParam = new ScoreParam();
        scoreParam.setAdAbGroup(request.getAdAbGroup());
        scoreParam.setRequestContext(context);
        scoreParam.setVideoId(request.getVideoId());
        scoreParam.setNewExpGroup(request.getNewExpGroup());
        scoreParam.setPqtId(request.getPqtId());
        scoreParam.setMid(request.getMid());
        scoreParam.setAppType(request.getAppType());

        Set<String> expCodeSet = AbUtil.unfoldAllExpCode(request.getAdAbExpArr());
        scoreParam.setExpCodeSet(expCodeSet);

        // 本次命中的实验
        String expCode = AbUtil.chooseAdAlgExpCode(expCodeSet, request.getAppType(), request.getNewExpGroup());
        scoreParam.setExpCode(expCode);
        if (request instanceof RankRecommendRequestParam) {
            RankRecommendRequestParam rankReq = (RankRecommendRequestParam) request;
            scoreParam.setRootSessionId(rankReq.getRootSessionId());
            scoreParam.setEngineInfo(rankReq.getEngineInfo());

            // 尾号实验：只认引擎这一层(倒数第6位/index 5)，单独存 tailExpCodeSet，与 AB 完全隔离。
            // 业务侧用 AbUtil.isInTailExp(tailExpCodeSet, code) 单独判断，不和 AB 的 expCode 混用
            Set<String> tailExpCodeSet = AbUtil.unfoldTailExpCode(rankReq.getTailExpArr(), AbUtil.ENGINE_TAIL_INDEX);
            scoreParam.setTailExpCodeSet(tailExpCodeSet);
        }
        return scoreParam;
    }

    private static String findExpCode(Set<String> expCodeSet, Long appType, Integer newExpGroup) {
        if (AbUtil.isInAbExp(expCodeSet, appType, newExpGroup, "680")) {
            return "680";
        } else if (AbUtil.isInAbExp(expCodeSet, appType, newExpGroup, "683")) {
            return "683";
        } else if (AbUtil.isInAbExp(expCodeSet, appType, newExpGroup, "687")) {
            return "687";
        } else if (AbUtil.isInAbExp(expCodeSet, appType, newExpGroup, "679")) {
            return "679";
        } else if (AbUtil.isInAbExp(expCodeSet, appType, newExpGroup, "688")) {
            return "688";
        } else {
            return "";
        }
    }

    public static List<AdRankItem> adIdConvertRankItem(List<AdPlatformCreativeDTO> creativeDTOList, RecommendRequestParam request) {
        List<AdRankItem> adRankItems = new ArrayList<>(creativeDTOList.size());
        for (AdPlatformCreativeDTO dto : creativeDTOList) {
            AdRankItem adRankItem = new AdRankItem();
            adRankItem.setAdId(dto.getCreativeId());
            adRankItem.setCreativeCode(dto.getCreativeCode());
            adRankItem.setAdVerId(dto.getAdVerId());
            adRankItem.setVideoId(request.getVideoId());
            adRankItem.setWeight(dto.getWeight());
        }
        return adRankItems;
    }
}
