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

import cn.com.ctop.common.module.entity.CtopOauthToken;
import cn.com.ctop.common.module.utils.HttpUtils;
import cn.com.ctop.toutiao.modules.adlab.constants.GetProjectUrlConstant;
import cn.com.ctop.toutiao.modules.adlab.service.IGetProjectService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;

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

@Service
public class GetProjectServiceImpl implements IGetProjectService {

    @Override
    public JSONObject getGroup(CtopOauthToken oauthToken, Long accountId, Long groupId) {
        //请求头**
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("Access-Token", oauthToken.getAccessToken());
        //请求参数
        JSONObject params = new JSONObject();
        params.put("advertiser_id", accountId);
        params.put("group_id", groupId);

        return JSONObject.parseObject(HttpUtils.httpGetRequest(GetProjectUrlConstant.ADLAB_GROUP_GET, headers, params));
    }

    @Override
    public JSONObject getMaterialInfo(CtopOauthToken oauthToken, JSONObject requestBody) {
        //请求头**
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("Access-Token", oauthToken.getAccessToken());
        //请求参数
        JSONObject params = new JSONObject();
        params.put("advertiser_id", requestBody.getLong("accountId"));
        params.put("group_id", requestBody.getString("groupId"));

        return JSONObject.parseObject(HttpUtils.httpGetRequest(GetProjectUrlConstant.ADLAB_GROUP_MATERIAL_GET, headers, params));
    }

    @Override
    public JSONObject getGroups(CtopOauthToken oauthToken, JSONObject requestBody) {

        //请求头**
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("Access-Token", oauthToken.getAccessToken());
        //请求参数
        JSONObject params = new JSONObject();
        params.put("advertiser_id", requestBody.getLong("accountId"));
        JSONObject filtering = new JSONObject();
        filtering.put("campaign_ids", requestBody.getJSONArray("campaignIds"));
        filtering.put("combo_status", requestBody.getString("comboStatus"));
        filtering.put("group_ids", requestBody.getJSONArray("groupIds"));
        filtering.put("group_name", requestBody.getString("groupName"));
        filtering.put("landing_types", requestBody.getString("landingTypes"));
        params.put("filtering", filtering);
        //TODO 设置  pageSize pageNo
        return JSONObject.parseObject(HttpUtils.httpGetRequest(GetProjectUrlConstant.ADLAB_GROUPS_GET, headers, params));
    }

    @Override
    public JSONObject getAds(CtopOauthToken oauthToken, JSONObject requestBody) {
        //请求头**
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("Access-Token", oauthToken.getAccessToken());
        //请求参数
        JSONObject params = new JSONObject();
        params.put("advertiser_id", requestBody.getLong("accountId"));
        params.put("ad_ids", requestBody.getString("adIds"));

        return JSONObject.parseObject(HttpUtils.httpGetRequest(GetProjectUrlConstant.ADLAB_ADS_GET, headers, params));

    }
}
