package cn.com.ctop.toutiao.modules.tool.controller;

import cn.com.ctop.common.module.service.ICtopOauthTokenService;
import cn.com.ctop.toutiao.modules.tool.service.IByteDanceConvertToolService;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/ctop/queryTool")
public class ByteDanceQueryToolController {

    @Autowired
    private IByteDanceConvertToolService byteDanceConvertToolService;

    @Autowired
    ICtopOauthTokenService ctopOauthTokenService;

    /**
     *  查询转化目标接口
     */
    @PostMapping(value="/queryConvert")
    public Result<Map<String,Object>> queryConvertResult(Long accountId, String landingType, String appType,
                                                                String downloadType,String externalUrl,String toutiaoPackage,
                                                                String downloadUrl){

        Result<Map<String,Object>> result=new Result<>();
        Map<String, Object> convertQueryMap = byteDanceConvertToolService.queryAdConvert(ctopOauthTokenService.getTokenByAccountId(accountId), accountId,
                landingType, appType, downloadType, externalUrl, toutiaoPackage, downloadUrl);
        if(!convertQueryMap.get("code").equals(0)){
            result.error500("查询转化目标结果失败");
        }else{
            ((List)((JSONObject)convertQueryMap.get("data")).get("list")).forEach(it->{
                if(((Map)it).get("convert_type")!=null){
                    ((Map)it).put("name",getName(((Map)it).get("convert_type").toString()));
                }
            });
            result.setSuccess(true);
            result.setResult(convertQueryMap);
        }
        return result;
    }

    private String getName(String convertType){
        String convertName="";
        switch (convertType) {
            case "AD_CONVERT_SOURCE_TYPE_INTERNAL":
                convertName = "线索通";
                break;
            case "AD_CONVERT_SOURCE_TYPE_JS":
                convertName = "代码检测转化";
                break;
            case "AD_CONVERT_SOURCE_TYPE_XPATH":
                convertName = "路径转化";
                break;
            case "AD_CONVERT_SOURCE_TYPE_APP_DOWNLOAD":
                convertName = "应用下载API";
                break;
            case "AD_CONVERT_SOURCE_TYPE_H5_API":
                convertName = "落地页API（H5）";
                break;
            case "AD_CONVERT_SOURCE_TYPE_SDK":
                convertName = "应用下载SDK";
                break;
            case "AD_CONVERT_SOURCE_TYPE_OPEN_URL":
                convertName = "应用直达API（应用直达链接）";
                break;
            case "AD_CONVERT_SOURCE_TYPE_NORMAL_APP_DOWNLOAD":
                convertName = "普通应用下载";
                break;
            case "AD_CONVERT_SOURCE_TYPE_CONFIG":
                convertName = "无转化跟踪";
                break;
            case "AD_CONVERT_SOURCE_TYPE_CPS_GAME":
                convertName = "内部游戏cps应用下载";
                break;
            case "AD_CONVERT_SOURCE_TYPE_APP_API_TEMAI":
                convertName = "落地页API(支持特卖)";
                break;
        }
        return convertName;
    }
}
