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.PlanAnalyzeReport;
import cn.com.ctop.toutiao.modules.adlab.service.IPlanAnalyzeReportService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;

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

@Service
public class PlanAnalyzeReportServiceImpl implements IPlanAnalyzeReportService {

    @Override
    public JSONObject updateAdBid(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_id", requestBody.getString("adId"));
        //给计划设置的新出价，0表示跟项目出价保持一致
        params.put("bid", requestBody.getFloat("bid"));

        return JSONObject.parseObject(HttpUtils.httpPostRequest(PlanAnalyzeReport.ADLAB_AD_BID_UPDATE, params, headers));
    }

    @Override
    public JSONObject updateAdBudget(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"));
        //设置的追加预算金额
        params.put("budget", requestBody.getFloat("budget"));
        //需要追加预算的计划列表，这些计划会共享这个追加的预算，并且必须属于group_id对应项目.
        params.put("ad_ids", requestBody.getJSONArray("adIds"));

        return JSONObject.parseObject(HttpUtils.httpPostRequest(PlanAnalyzeReport.ADLAB_AD_BUDGET_UPDATE, params, headers));
    }

    @Override
    public JSONObject updateAdBudgetCancel(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_id", requestBody.getString("adId"));

        return JSONObject.parseObject(HttpUtils.httpPostRequest(PlanAnalyzeReport.ADLAB_AD_BUDGET_CANCEL, params, headers));
    }

    @Override
    public JSONObject getBudget(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"));

        return JSONObject.parseObject(HttpUtils.httpGetRequest(PlanAnalyzeReport.ADLAB_BUDGET_GET, headers, params));
    }

    @Override
    public JSONObject getGroupAds(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.getLong("groupId"));

        return JSONObject.parseObject(HttpUtils.httpGetRequest(PlanAnalyzeReport.ADLAB_BUDGET_GET, headers, params));
    }

}
