package cn.com.ctop.toutiao.modules.tool.service.impl;

import cn.com.ctop.common.module.entity.CtopOauthToken;
import cn.com.ctop.common.module.utils.HttpUtils;
import cn.com.ctop.toutiao.modules.tool.constants.QueryToolUrlConstant;
import cn.com.ctop.toutiao.modules.tool.service.IByteDanceQueryToolService;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

/**
 *  Created by JQ.bi on 2020.07.17
 */
@Slf4j
@Service
public class ByteDanceQueryToolServiceImpl implements IByteDanceQueryToolService {

    //TODO 查询受众预估结果
    @Override
    public Map<String, Object> queryEstimateAudience(CtopOauthToken token,Long advertiserId,JSONObject requestBody) {

        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("Access-Token", token.getAccessToken());

        JSONObject params=new JSONObject();
        if(requestBody.getString("retargetingType")!=null){
            params.put("retargetingType",requestBody.getString("retargetingType"));
        }
        if(requestBody.getJSONArray("retargetingType")!=null){
            params.put("retargetingTags",requestBody.getString("retargetingTags"));
        }
        if(requestBody.getJSONArray("retargetingTagsInclude")!=null){
            params.put("retargetingTagsInclude",requestBody.getString("retargetingTagsInclude"));
        }
        if(requestBody.getJSONArray("retargetingTagsExclude")!=null){
            params.put("retargetingTagsExclude",requestBody.getString("retargetingTagsExclude"));
        }
        if(requestBody.getString("gender")!=null){
            params.put("gender",requestBody.getString("gender"));
        }
        if(requestBody.getJSONArray("age")!=null){
            params.put("age",requestBody.getString("age"));
        }
        JSONObject jsonObject = JSONObject.parseObject(HttpUtils.httpPostRequest(QueryToolUrlConstant.ESTIMATE_AUDIENCE_URL, params, headers));
        return null;
    }

    @Override
    public Map<String, Object> queryAppSearch(CtopOauthToken token,Long advertiserId,JSONObject requestBody) {

        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("Access-Token", token.getAccessToken());

        JSONObject params=new JSONObject();
        params.put("advertiser_id",advertiserId);
        if("APP_NAME".equals(requestBody.getString("searchBy"))){
            params.put("advertiser_id",requestBody.getString("advertiserId"));
            params.put("app_name",requestBody.getString("appName"));
        }else {
            params.put("advertiser_id",requestBody.getString("advertiserId"));
            params.put("app_id",requestBody.getLong("appId"));
        }
        JSONObject jsonObject = JSONObject.parseObject(HttpUtils.httpPostRequest(QueryToolUrlConstant.APP_SEARCH_URL, params, headers));
        Map<String,Object> resultMap=new HashMap<>();
        if(jsonObject.getInteger("code")!=0){
            log.error("查询应用信息失败==》accountId:{},message:{}", token.getAccountId(), jsonObject.getString("message"));
            resultMap.put("code", -1);
            resultMap.put("message", jsonObject.getString("message"));
            return resultMap;
        }
        resultMap.put("code", 0);
        return resultMap;
    }

    //TODO 建议日预算及预期成本
    @Override
    public Map<String, Object> queryBidSuggest(CtopOauthToken token,JSONObject requestBody) {

        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("Access-Token", token.getAccessToken());

        JSONObject params=new JSONObject();
        JSONObject jsonObject = JSONObject.parseObject(HttpUtils.httpPostRequest(QueryToolUrlConstant.BID_SUGGEST_URL, params, headers));
        Map<String,Object> resultMap=new HashMap<>();
        if(jsonObject.getInteger("code")!=0){
            log.error("建议日预算及预期成本失败==》accountId:{},message:{}", token.getAccountId(), jsonObject.getString("message"));
            resultMap.put("code", -1);
            resultMap.put("message", jsonObject.getString("message"));
            return resultMap;
        }
        resultMap.put("code", 0);
        return resultMap;
    }

    @Override
    public Map<String, Object> queryAdQualityGet(CtopOauthToken token, Long advertiserId, List<Long> adIds) {

        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("Access-Token", token.getAccessToken());

        JSONObject params=new JSONObject();
        params.put("advertiser_id",advertiserId);
        params.put("ad_ids",adIds);
        JSONObject jsonObject = JSONObject.parseObject(HttpUtils.httpPostRequest(QueryToolUrlConstant.AD_QUALITY_URL, params, headers));
        Map<String,Object> resultMap=new HashMap<>();
        if(jsonObject.getInteger("code")!=0){
            log.error("查询广告质量度失败==》accountId:{},message:{}", token.getAccountId(), jsonObject.getString("message"));
            resultMap.put("code", -1);
            resultMap.put("message", jsonObject.getString("message"));
            return resultMap;
        }
        resultMap.put("code", 0);
        return resultMap;
    }


    @Override
    public Map<String, Object> queryAdvertiserStoreSearch(CtopOauthToken token, Long advertiserId, String type,String page,String pageSize) {

        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("Access-Token", token.getAccessToken());

        JSONObject params=new JSONObject();
        params.put("advertiser_id",advertiserId);
        params.put("type",type);
        params.put("page",page);
        params.put("page_size",pageSize);
        JSONObject jsonObject = JSONObject.parseObject(HttpUtils.httpPostRequest(QueryToolUrlConstant.ADVERTISER_STORY_SEARCH, params, headers));
        Map<String,Object> resultMap=new HashMap<>();
        if(jsonObject.getInteger("code")!=0){
            log.error("查询门店失败==》accountId:{},message:{}", token.getAccountId(), jsonObject.getString("message"));
            resultMap.put("code", -1);
            resultMap.put("message", jsonObject.getString("message"));
            return resultMap;
        }
        resultMap.put("code", 0);
        return resultMap;
    }

    @Override
    public Map<String, Object> queryProceduralPackageGet(CtopOauthToken token, Long advertiserId,JSONObject requestBody) {

        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("Access-Token", token.getAccessToken());

        JSONObject params=new JSONObject();
        params.put("advertiser_id",advertiserId);
        if(requestBody.getLong("adId")!=null){
            params.put("ad_id",requestBody.getLong("adId"));
        }
        if(requestBody.getString("keyword")!=null){
            params.put("keyword",requestBody.getLong("keyword"));
        }
        params.put("page",requestBody.getInteger("page"));
        params.put("page_size",requestBody.getInteger("pageSize"));
        JSONObject jsonObject = JSONObject.parseObject(HttpUtils.httpPostRequest(QueryToolUrlConstant.PROCEDURAL_PACKAGE, params, headers));
        Map<String,Object> resultMap=new HashMap<>();
        if(jsonObject.getInteger("code")!=0){
            log.error("查询程序化创意包失败==》accountId:{},message:{}", token.getAccountId(), jsonObject.getString("message"));
            resultMap.put("code", -1);
            resultMap.put("message", jsonObject.getString("message"));
            return resultMap;
        }
        resultMap.put("code", 0);
        return resultMap;
    }

    @Override
    public Map<String, Object> queryAdvertiserStoreSearchPack(CtopOauthToken token) {
        return null;
    }
}
